Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture.

Similar presentations


Presentation on theme: "CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture."— Presentation transcript:

1 CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture slide contents

2 CS224 Fall 2011 Chapter 2b Conditional Operations  Branch to a labeled instruction if a condition is true Otherwise, continue sequentially  beq rs, rt, L1 if (rs == rt) branch to instruction labeled L1;  bne rs, rt, L1 if (rs != rt) branch to instruction labeled L1;  j L1 unconditional jump to instruction labeled L1 §2.7 Instructions for Making Decisions

3 CS224 Fall 2011 Chapter 2b Compiling If Statements  C code: if (i==j) f = g+h; else f = g-h; f, g, … in $s0, $s1, …  Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: … Assembler calculates addresses

4 CS224 Fall 2011 Chapter 2b Compiling Loop Statements  C code: while (save[i] == k) i += 1; i in $s3, k in $s5, address of save in $s6  Hand “compiled” MIPS code: Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: …  A real compiler will reduce this loop from 6 instructions to 3 (50% speedup). How? Bottom test_&_branching (back up, or out) saves 1 t1<- t1+4 (instead of sll, add, & addi) saves 2

5 CS224 Fall 2011 Chapter 2b Basic Blocks  A basic block is a sequence of instructions with No embedded branches (except at end) No branch targets (except at beginning)  A compiler identifies basic blocks for optimization  An advanced processor can accelerate execution of basic blocks

6 CS224 Fall 2011 Chapter 2b  MIPS conditional branch instructions: bne $s0, $s1, Ll#go to Ll if $s0  $s1 beq $s0, $s1, Ll#go to Ll if $s0=$s1 Ex: if (i==j) h = i + j; bne $s0, $s1, L1 add $s3, $s0, $s1 L1:... MIPS Control Flow Instructions  Instruction Format (I format): 0x05 16 17 16 bit offset  How is the branch destination address specified?

7 CS224 Fall 2011 Chapter 2b Specifying Branch Destinations  Use a register (like in lw and sw) added to the 16-bit offset which register? Instruction Address Register (the PC) -its use is automatically implied by instruction -PC gets updated (PC+4) during the fetch cycle so that it holds the address of the next instruction limits the branch distance to -2 15 to +2 15 -1 (word) instructions from the (instruction after the) branch instruction, but most branches are local anyway PC Add 32 offset 16 32 00 sign-extend from the low order 16 bits of the branch instruction branch dest address ? Add 4 32

8 CS224 Fall 2011 Chapter 2b  We have beq, bne, but what about other kinds of branches (e.g., branch-if-less-than)? For this, we need yet another instruction, slt  Set on less than instruction: slt $t0, $s0, $s1 # if $s0 < $s1 then # $t0 = 1else # $t0 = 0  Instruction format (R format):  Alternate versions of slt slti $t0, $s0, 25# if $s0 < 25 then $t0=1... sltu $t0, $s0, $s1# if $s0 < $s1 then $t0=1... sltiu $t0, $s0, 25# if $s0 < 25 then $t0=1... 2 In Support of Branch Instructions 0 16 17 8 0x24

9 CS224 Fall 2011 Chapter 2b Signed vs. Unsigned  Signed comparison: slt, slti  Unsigned comparison: sltu, sltui  Example $s0 = 1111 1111 1111 1111 1111 1111 1111 1111 $s1 = 0000 0000 0000 0000 0000 0000 0000 0001 slt $t0, $s0, $s1 # signed -–1 < +1  $t0 = 1 sltu $t0, $s0, $s1 # unsigned -+4,294,967,295 > +1  $t0 = 0

10 CS224 Fall 2011 Chapter 2b Aside: More Branch Instructions  Can use slt, beq, bne, and the fixed value of 0 in register $zero to create other conditions less than blt $s1, $s2, Label less than or equal to ble $s1, $s2, Label greater than bgt $s1, $s2, Label great than or equal to bge $s1, $s2, Label slt $at, $s1, $s2#$at set to 1 if bne $at, $zero, Label#$s1 < $s2  Such branches are included in the instruction set as pseudo-instructions. These are recognized (and expanded) by the assembler Its why the assembler needs a reserved register ( $at )

11 CS224 Fall 2011 Chapter 2b Branch Instruction Design  Why not include blt, bge, etc in the MIPS ISA?  Hardware for <, ≥, … slower than =, ≠ Combining with branch involves more work per instruction, requiring a slower clock All instructions would be penalized!  beq and bne are the common case (Principle #3)  This is a good design compromise (Principle #4)

12 CS224 Fall 2011 Chapter 2b Bounds Check Shortcut  Treating signed numbers as if they were unsigned gives a low cost way of checking if 0 ≤ x < y (index out of bounds for arrays) sltu $t0, $s1, $t2 # $t0 = 0 if # $s1 > $t2 (max)# or $s1 < 0 (min) beq $t0, $zero, IOOB# go to IOOB if# $t0 = 0  The key is that negative integers in two’s complement look like large numbers in unsigned notation. Thus, an unsigned comparison of x < y also checks if x is negative as well as if x is less than y.

13 CS224 Fall 2011 Chapter 2b  MIPS procedure call instruction: jalProcedureAddress#jump and link  Saves PC+4 in register $ra to have a link to the next instruction for the procedure return  Machine format (J format):  Then can do procedure return with a jr$ra#return  Instruction format (R format): Instructions for Accessing Procedures 0x03 26 bit address 0 31 0x08 §2.8 Supporting Procedures in Computer Hardware

14 CS224 Fall 2011 Chapter 2b Six Steps in Execution of a Procedure 1. Main routine (caller) places parameters in a place where the procedure (callee) can access them $a0 - $a3 : four argument registers 2. Caller transfers control to the callee ( jal Dest) 3. Callee acquires the storage resources needed 4. Callee performs the desired task 5. Callee places the result value in a place where the caller can access it $v0 - $v1 : two value registers for result values 6. Callee returns control to the caller ( jr $ra) $ra : one return address register to return to the point of origin

15 CS224 Fall 2011 Chapter 2b Register Usage  $a0 – $a3: arguments (reg’s 4 – 7)  $v0, $v1: result values (reg’s 2 and 3)  $t0 – $t9: temporaries Can be overwritten by callee  $s0 – $s7: saved Must be saved/restored by callee !  $gp: global pointer for static data (reg 28)  $sp: stack pointer (reg 29)  $fp: frame pointer (reg 30)  $ra: return address (reg 31)  $gp, $sp, $fp, $ra must be saved/restored by callee !

16 CS224 Fall 2011 Chapter 2b Procedure Call Instructions  Procedure call: jump and link jal ProcedureLabel Address of following instruction put in $ra Jumps to target address  Procedure return: jump register jr $ra Copies $ra to program counter  Can also be used for computed jumps e.g., for case/switch statements

17 CS224 Fall 2011 Chapter 2b Leaf Procedure Example  C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } Arguments g, …, j in $a0, …, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0

18 CS224 Fall 2011 Chapter 2b Leaf Procedure Example  MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Restore $s0 Result Return

19 CS224 Fall 2011 Chapter 2b Non-Leaf Procedures  Procedures that call other procedures  For nested call, caller needs to save on the stack: Its return address Any arguments and temporaries needed after the call  Restore from the stack after the call

20 CS224 Fall 2011 Chapter 2b Non-Leaf Procedure Example  C code: int fact (int n) { if (n < 1) return 1; else return n * fact(n - 1); } Argument n in $a0 Result in $v0

21 CS224 Fall 2011 Chapter 2b Non-Leaf Procedure Example  MIPS code: fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and return L1: addi $a0, $a0, -1 # else decrement n jal fact # recursive call lw $a0, 0($sp) # restore original n lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return

22 CS224 Fall 2011 Chapter 2b Aside: Spilling Registers  What if the callee needs to use more registers than allocated to argument and return values? callee uses a stack – a last-in-first-out queue low addr high addr $sp  One of the general registers, $sp ( $29 ), is used to address the stack (which “grows” from high address to low address) add data onto the stack – push $sp = $sp – 4, then put data on stack at new $sp remove data from the stack – pop get data from stack at $sp, then $sp = $sp + 4 top of stack

23 CS224 Fall 2011 Chapter 2b Stacking of Subroutine Environments  Procedures are nested, there are multiple calls and returns  C is a “leaf” procedure, but B is a nested (non-leaf) procedure  The stack grows downward at each new call, shrinks upward at each return A: A CALL B B: A B CALL C A B C C: RET A B A

24 CS224 Fall 2011 Chapter 2b Aside: Allocating Space on the Stack  The segment of the stack containing a procedure’s saved registers and local variables is its procedure frame (aka activation record) The frame pointer ( $fp ) points to the first word of the frame of a procedure – providing a stable “base” register for the procedure -$fp is initialized using $sp on a call and $sp is restored using $fp on a return low addr high addr $sp Saved argument regs (if any) Saved return addr Saved local regs (if any) Local arrays & structures (if any) $fp

25 CS224 Fall 2011 Chapter 2b Local Data on the Stack  Local data allocated by callee e.g., C automatic variables  Procedure frame (activation record) Used by some compilers to manage stack storage

26 CS224 Fall 2011 Chapter 2b Memory Layout  Text: program code  Static data: global variables e.g., static variables in C, constant arrays and strings $gp initialized to address allowing ±offsets into this segment  Dynamic data: heap E.g., malloc in C, new in Java  Stack: automatic storage

27 CS224 Fall 2011 Chapter 2b Aside: Allocating Space on the Heap  Static data segment for constants and other static variables (e.g., arrays)  Dynamic data segment (aka heap) for structures that grow and shrink (e.g., linked lists) Allocate space on the heap with malloc() and free it with free() in C Memory 0x 0000 0000 Text (Your code) Reserved Static data 0x 0040 0000 0x 1000 0000 0x 1000 8000 0x 7f f f f f f c Stack Dynamic data (heap) $sp $gp PC

28 CS224 Fall 2011 Chapter 2b Using Registers to Implement Procedures  Caller Save caller-saved registers $a0-$a3, $t0-$t9 Load arguments in $a0-$a3, rest on stack above $fp Execute jal instruction  Callee Setup Allocate memory in frame ($sp = $sp - frame) Save callee-saved registers $s0-$s7, $fp, $ra Create frame ($fp = $sp + frame size - 4)  Callee Return Place return value in $v0 and $v1 Restore any callee-saved registers ($fp, $ra,$s0-$s7…) Pop stack ($sp = $sp + frame size) Return by jr $ra

29 CS224 Fall 2011 Chapter 2b Calling Convention: Steps FP SP ra old FP $s0-$s7 FP SP First four arguments passed in registers Before call: Callee setup; step 1 FP SP Callee setup; step 2 ra old FP $s0-$s7 FP SP Callee setup; step 3 Adjust SP Save registers as needed Adjust FP

30 CS224 Fall 2011 Chapter 2b Compiling a Recursive Procedure  A procedure for calculating factorial int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); }  A recursive procedure (one that calls itself!) fact (0) = 1 fact (1) = 1 * 1 = 1 fact (2) = 2 * 1 * 1 = 2 fact (3) = 3 * 2 * 1 * 1 = 6 fact (4) = 4 * 3 * 2 * 1 * 1 = 24...  Assume n is passed in $a0 ; result returned in $v0

31 CS224 Fall 2011 Chapter 2b Compiling a Recursive Procedure fact:addi$sp, $sp, -8#adjust stack pointer sw$ra, 4($sp)#save return address sw$a0, 0($sp)#save argument n slti$t0, $a0, 1#test for n < 1 beq$t0, $zero, L1#if n >=1, go to L1 addi$v0, $zero, 1#else return 1 in $v0 addi$sp, $sp, 8#adjust stack pointer jr$ra#return to caller L1:addi$a0, $a0, -1#n >=1, so decrement n jalfact#call fact with (n-1) #bk f is where fact returns bk_f:lw$a0, 0($sp)#restore argument n lw$ra, 4($sp)#restore return address addi$sp, $sp, 8#adjust stack pointer mul$v0, $a0, $v0#$v0 = n * fact(n-1) jr$ra#return to caller

32 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 1  $sp $ra $a0 $v0 old TOS  Stack state after execution of first encounter with the jal instruction (which is the second call to fact routine, with $a0 now holding 1) save return address to caller routine (i.e., location in the main routine where first call to fact is made) on the stack save original value of $a0 on the stack

33 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 1  $sp $ra $a0 $v0  $sp caller rt addr $a0 = 2 21 bk_f old TOS  Stack state after execution of first encounter with the jal instruction (which is the second call to fact routine, with $a0 now holding 1) saved return address to caller routine (i.e., location in the main routine where first call to fact is made) on the stack saved original value of $a0 on the stack

34 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 2  $sp $ra $a0 $v0 old TOS  Stack state after execution of second encounter with the jal instruction (which is the third call to fact routine, with $a0 now holding 0) save return address of instruction in caller routine (instruction after jal ) on the stack save previous value of $a0 on the stack

35 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 2 $ra $a0 $v0  $sp caller rt addr $a0 = 2 10 bk_f old TOS  $sp $a0 = 1 bk_f  Stack state after execution of second encounter with the jal instruction (which is the third call to fact routine, with $a0 now holding 0) saved return address of instruction in caller routine (instruction after jal ) on the stack saved previous value of $a0 on the stack

36 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 3  $sp $ra $a0 $v0 old TOS  Stack state after execution of the only encounter with the first jr instruction (return from fact(0) routine, with $v0 set to 1) stack pointer updated and re- updated to point to place it held at third call to fact

37 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 3 $ra $a0 $v0  $sp bk_f $a0 = 1 0 old TOS  $sp $a0 = 0 bk_f $a0 = 2 caller rt addr 1  $sp  Stack state after execution of the only encounter with the first jr instruction (return from fact(0) routine, with $v0 set to 1) stack pointer updated and re- updated to point to place it held at third call to fact

38 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 4  $sp $ra $a0 $v0 old TOS  Stack state after execution of first encounter with the second jr instruction (return from fact(1) routine, after updating $v0 to 1 * 1) return address to caller routine ( bk_f in fact routine) restored to $ra from the stack previous value of $a0 restored from the stack stack pointer re-updated to point to place it held at second call to fact

39 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 4 $ra $a0 $v0  $sp bk_f $a0 = 1 0 old TOS $a0 = 0 bk_f $a0 = 2 caller rt addr 1  $sp 1 bk_f 1 * 1  Stack state after execution of the first encounter with the second jr instruction (return from fact(1) routine, after updating $v0 to 1 * 1) return address to caller routine ( bk_f in fact routine) restored to $ra from the stack previous value of $a0 restored from the stack stack pointer re-updated to point to place it held at second call to fact

40 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 5  $sp $ra $a0 $v0 old TOS  Stack state after execution of first encounter with the second jr instruction (return from fact(2) routine after updating $v0 to 2 * 1 * 1) return address to caller routine ( bk_f in fact routine) restored to $ra from the stack previous value of $a0 restored from the stack stack pointer re-updated to point to place it held at first call to fact

41 CS224 Fall 2011 Chapter 2b A Look at the Stack for $a0 = 2, Part 5 $ra $a0 $v0  $sp bk_f $a0 = 1 1 old TOS $a0 = 0 bk_f $a0 = 2 caller rt addr 1 * 1  $sp 2 bk_f 2 * 1 * 1  Stack state after execution of second encounter with the second jr instruction (return from fact(2) routine, after updating $v0 to 2 * 1 * 1) return address to caller routine (main routine) restored to $ra from the stack original value of $a0 restored from the stack stack pointer re-updated to point to place it held at first call to fact Caller rt addr

42 CS224 Fall 2011 Chapter 2b Character Data  Byte-encoded character sets ASCII: 128 characters -95 graphic, 33 control Latin-1: 256 characters -ASCII, +96 more graphic characters  Unicode: 32-bit character set Used in Java, C++ wide characters, … Most of the world’s alphabets, plus symbols UTF-8, UTF-16: variable-length encodings §2.9 Communicating with People

43 CS224 Fall 2011 Chapter 2b Byte/Halfword Operations  Could use bitwise operations  MIPS byte/halfword load/store String processing is a common case lb rt, offset(rs) lh rt, offset(rs) Sign extend to 32 bits in rt lbu rt, offset(rs) lhu rt, offset(rs) Zero extend to 32 bits in rt sb rt, offset(rs) sh rt, offset(rs) Store just rightmost byte/halfword

44 CS224 Fall 2011 Chapter 2b String Copy Example  C code (naive): Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='\0') i += 1; } Addresses of x, y in $a0, $a1 i in $s0

45 CS224 Fall 2011 Chapter 2b String Copy Example  MIPS code: strcpy: addi $sp, $sp, -4 # adjust stack for 1 item sw $s0, 0($sp) # save $s0 add $s0, $zero, $zero # i = 0 L1: add $t1, $s0, $a1 # addr of y[i] in $t1 lbu $t2, 0($t1) # $t2 = y[i] add $t3, $s0, $a0 # addr of x[i] in $t3 sb $t2, 0($t3) # x[i] = y[i] beq $t2, $zero, L2 # exit loop if y[i] == 0 addi $s0, $s0, 1 # i = i + 1 j L1 # next iteration of loop L2: lw $s0, 0($sp) # restore saved $s0 addi $sp, $sp, 4 # pop 1 item from stack jr $ra # and return


Download ppt "CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture."

Similar presentations


Ads by Google