From C to MIPS David E Culler CS61CL Sept 16 2009 Lecture 4 UCB CS61CL F09 Lec 4 9 16 09 1 Review Arrays Structs and Pointers allow you define sophisticated data structures Compiler protects you by enforcing type system Avoid dropping beneath the abstraction and munging the bits All map into untyped storage ints and addresses Executing program has a specific structure Code Static Data Stack and Heap Mapped into address space Holes allow stack and heap to grow Compiler defines what the bits mean by enforcing type Chooses which operations to perform Poor coding practices bugs and architecture limitations lead to vulnerabilities 9 16 09 UCB CS61CL F09 Lec 4 2 Today int main 9 16 09 data A word 5 text main lw a0 x jal decr move a0 v0 UCB CS61CL F09 Lec 4 3 Elements of the Language Basic Data Types char int float double Type Constructors array struct pointer Variables Expressions Sequence of statements Conditionals Iteration Functions 9 16 09 UCB CS61CL F09 Lec 4 4 What does the machine do Instruction Execution Cycle Instruction Fetch Decode Operand Fetch Execute Result Store Next Instruction 9 16 09 UCB CS61CL F09 Lec 4 5 Instruction Cycle 000 0 n main 0B20 FFF F 32 2 3 1 Instruction Fetch add 1 2 3 Decode 40 Operand Execute 61 101 Result Next 9 16 09 PC UCB CS61CL F09 Lec 4 0B20 0B24 6 Instruction Cycle again 000 0 93 n main 0B20 FFF F 35 2 1 00 Instruction Fetch lw 2 1 00 Decode 40 Operand Execute 93 61 101 Result Next 9 16 09 PC UCB CS61CL F09 Lec 4 0B24 0B28 7 What does the machine do Instruction Execution Cycle Instruction Fetch 9 16 09 Register Transfers inst mem PC Decode op rd rs rt inst Operand Fetch A reg rs B reg rt Execute R A B Result Store reg rd A B Next Instruction PC PC 4 UCB CS61CL F09 Lec 4 8 MIPS Assembly Language MAL segments label opcode 9 16 09 values data A word 5 word 6 text main la t0 A lw a0 4 t0 jal decr move a0 v0 decr addi v0 a0 1 jr ra UCB CS61CL F09 Lec 4 operands registers literals 9 MIPS Instruction Format add 1 2 3 op 6 rs 5 rt 5 lw 3 24 2 9 16 09 r1 r2 r3 rd 5 shamt 5 immediate 16 funct 6 r3 mem r2 24 UCB CS61CL F09 Lec 4 10 MIPS register conventions Name zero at v0 v1 a0 a3 t0 t7 s0 s7 t8 t9 k0 k1 gp sp fp ra 9 16 09 Number 0 1 2 3 4 7 8 15 16 23 24 25 26 27 28 29 30 31 Use constant 0 assembler temporary returns values function arguments temporaries saved temporaries temporaries reserved for OS kernel global pointer stack pointer frame pointer return address UCB CS61CL F09 Lec 4 Callee must preserve N A No No No No Yes No No Yes Yes Yes N A 11 Administration Calendar with links on the home page Readers will be in lab 1 hour per week HW3R Resubmit as hw3r permitted till 11 59 pm Saturday as you learn don t be afraid to make a fresh start HW4 out all future HW will be W W less work than hw3 little reliance on Th F Lab start right away Continues C concepts plus basic MAL Project 1 posted Mid Term 1 shifted to Wed 10 7 in class time alternative Monday 10 5 4 pm 9 16 09 UCB CS61CL F09 Lec 4 12 Elements of the Language Basic Data Types char int float double Type Constructors array struct pointer Variables Expressions Sequence of statements Conditionals Iteration Functions 9 16 09 UCB CS61CL F09 Lec 4 13 Expressions y1 b sqrt b b 4 a c 2 a y2 b sqrt b b 4 a c 2 a t1 t2 t3 t4 t5 t6 t7 t8 y1 t9 y2 b b b 4 a t3 c t2 t4 sqrt t5 t1 t6 2 a t7 t8 t1 t6 t9 t8 9 16 09 t1 t2 t3 t3 a0 v0 t7 t8 y1 t9 y2 b b b 4 a t3 c t2 t3 sqrt a0 t1 v0 2 a t7 t8 t1 v0 t9 t8 UCB CS61CL F09 Lec 4 lw sub mult lw mult lw mult sub jal add mult div sw sub div sw t0 b t1 0 t2 t0 t0 a t3 t0 t4 c t3 t3 a0 t2 sqrt t7 t1 t8 t0 t0 t7 t0 y1 t9 t1 t0 t9 t0 y2 t0 t0 4 t4 t3 v0 2 t8 v0 t8 14 y1 b sqrt b b 4 a c 2 a y2 b sqrt b b 4 a c 2 a t1 b t2 b b t3 4 a t3 t3 c a0 v0 t7 t8 y1 t2 t3 sqrt a0 t1 v0 2 a t7 t8 t9 t1 v0 y2 t9 t8 9 16 09 lw sub mult lw mult lw mult sub jal add mult div sw sub div sw UCB CS61CL F09 Lec 4 t0 b t1 0 t2 t0 t0 a t3 t0 t4 c t3 t3 a0 t2 sqrt t7 t1 t8 t0 t0 t7 t0 y1 t9 t1 t0 t9 t0 y2 t0 t0 4 t4 t3 v0 2 t8 v0 t8 15 Variables Can be held in Registers Temporary variables Internal to expression evaluation Local to a function and no references to them Arguments and return values Or in memory Global or static variables externals Local variables on the stack Values in the heap Memory is usually accessed indirectly through a special register stack pointer global pointer heap pointer 9 16 09 UCB CS61CL F09 Lec 4 16 Variable examples int ext int foo int n int loc int A 8 struct int x int y point int dyn malloc 10 sizeof int return loc n 9 16 09 UCB CS61CL F09 Lec 4 17 Conditionals Human C code if condition true clause else false clause Machine level C code if condition goto Ltrue false clause goto Ldone Ltrue true clause Ldone Machine level Assembly code BR condition Ltrue code for false clause jmp Ldone Ltrue code for true clause Ldone 9 16 09 UCB CS61CL F09 Lec 4 18 Jumps and Branches Jumps unconditional control transfers direct or indirect calls are a special Jump and link saves the return address computes target address and loads PC Branches conditional control transfers tests a condition and branches if true otherwise falls through sequentially MIPS provides simple conditions on registers BEQ BNE BGZ 9 16 09 UCB CS61CL F09 Lec 4 19 Loops Human C code while condition loop body Machine level C code Ltop if condition goto Ldone loop body goto Ltop Ldone Machine level Assembly code Ltop BR condition Ltrue jmp Ldone Ltrue code for loop body jmp Ltop Ldone 9 16 09 UCB CS61CL F09 Lec 4 20 Functions basic Human C code int foo int arg1 int arg2 declarations function body return val res foo param1 param2 Machine level …
View Full Document
Unlocking...