390 likes | 411 Views
Computer Architecture CSE 3322. Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, 2.10.4, 2.10.6 Due 2/10/09 http://crystal.uta.edu/~cse3322. Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break}.
E N D
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, 2.10.4, 2.10.6 Due 2/10/09 http://crystal.uta.edu/~cse3322
Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break}
Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } if k < 0, then Exit slt set on less than slt rd, rs, rt means if rs < rt, rd = 1, else rd=0
Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } if k < 0, then Exit slt set on less than slt rd, rs, rt means if rs < rt, rd = 1, else rd=0 So, if k ~ $s0 slt $t0, $s0, $zero # $t0 = 1 if k < 0 bne $t0, $zero, Exit # goto Exit if k < 0
Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } if k < 0, then Exit slt set on less than slt rd, rs, rt means if rs < rt, rd = 1, else rd=0 So, if k ~ $s0 slt $t0, $s0, $zero # $t0 = 1 if k < 0 bne $t0, $zero, Exit # goto Exit if k < 0 And the test for k > 2 is, assuming $s1 = 3 slt $t0, $s0, $s1 # $t0 = 1 if k < 3 beq $t0, $zero, Exit # goto Exit if k >=3
Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } JumpTable[ k ] addr of statement 2 addr of statement 1 addr of statement 0 For a given k, place JumpTable[ k ] in register $t0 using lw instruction
Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } JumpTable[ k ] addr of statement 2 addr of statement 1 addr of statement 0 load JumpTable[ k ] in a register and jump to it jr jump register jr rs means go to address in register rs
Case / Switch Statement switch ( k ) { case 0: statement 0; break k is in $s0, case 1: statement 1; break Start of JumpTable is case 2: statement 2; break } in $t1 slt $t0, $s0, $zero # $t0 = 1 if k < 0 bne $t0, $zero, Exit # goto Exit if k < 0 slt $t0, $s0, $s1 # $t0 = 1 if k < 3 beq $t0, $zero, Exit # goto Exit if k >=3 Exit:
Case / Switch Statement switch ( k ) { case 0: statement 0; break k is in $s0, case 1: statement 1; break Start of JumpTable is case 2: statement 2; break } in $t1 slt $t0, $s0, $zero # $t0 = 1 if k < 0 bne $t0, $zero, Exit # goto Exit if k < 0 slt $t0, $s0, $s1 # $t0 = 1 if k < 3 beq $t0, $zero, Exit # goto Exit if k >=3 add $t0, $s0, $s0 # $t0 = 2 * k add $t0, $t0, $t0 # $t0 = 4 * k add $t0, $t0, $t1 # $t0 = addr of JumpTable[k] lw $t2, 0( $t0) # $t2 = JumpTable[k] jr $t2 # jump to addr in $t2 Exit:
MIPS Assembly Instructions Pseudo instructions Instructions supported by the Assembler but not implemented in hardware. Ex: move multiply branch less than, less than or equal, greater than, greater than or equal
MIPS Immediate Addressing Very common to use a constant in arithmetic operations. Examples? Make it faster to access small constants. Keep the constant in the instruction. add immediate addi $s1, $s2, constant $s1 = $s2 + constant op rs rt immediate 8 18 17 constant 6 5 5 16 I type of format The constant can be negative!
MIPS Immediate Addressing Very common to use a constant in comparison operations. Examples? Make it faster to do comparisons. Keep the constant in the instruction slt immediate slti $t0, $s2, constant $t0 = 1 if $s2 < constant else $t0 = 0 op rs rt immediate 10 18 8 constant 6 5 5 16 I type of format
Procedure Calls • Place parameters where the procedure can access them
Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure
Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure
Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them
Procedure Calls • Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them • 5. Return control to the point of the call
Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them • 5. Return control to the point of the call Procedure Calls Allocate registers to hold data for procedure calls $a0 - $a3 : four registers to pass parameters $v0 - $v1 : two registers to return values $ra : one return address register
Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them • 5. Return control to the point of the call Procedure Calls Allocate registers to hold data for procedure calls $a0 - $a3 : four registers to pass parameters $v0 - $v1 : two registers to return values $ra : one return address register Need jump-and-link instruction : jal ProcedureAddress means :save return address in $ra and jumps to ProcedureAddress
Place parameters where the procedure can access them • Transfer control to the procedure • Perform the task of the procedure • Place the results where the calling program can access • them • 5. Return control to the point of the call Procedure Calls Allocate registers to hold data for procedure calls $a0 - $a3 : four registers to pass parameters $v0 - $v1 : two registers to return values $ra : one return address register Need jump-and-link instruction : jal ProcedureAddress means :save return address in $ra and jumps to ProcedureAddress How do you return?
Compiling a “leaf” Procedure (Does not Call another Procedure) int leaf_example ( int g, int h, int i, int j) { int f ; f = ( g + h ) – ( i + j ) ; return f ;}
Compiling a “leaf” Procedure (Does not Call another Procedure) int leaf_example ( int g, int h, int i, int j) { int f ; f = ( g + h ) – ( i + j ) ; return f ;} Assign g to $a0, h to $a1, i to $a2, j to $a3 and f to $v0.
Compiling a “leaf” Procedure (Does not Call another Procedure) int leaf_example ( int g, int h, int i, int j) { int f ; f = ( g + h ) – ( i + j ) ; return f ;} Assign g to $a0, h to $a1, i to $a2, j to $a3 and f to $v0. Leaf_example: add $t0, $a0, $a1 # Temp $t0 = g + h add $t1, $a2, $a3 # Temp $t1 = i + j sub $v0, $t0 , $t1 # $v0 = (g+h) – (i+j) jr $ra # jump back to calling routine
Compiling a “leaf” Procedure (Does not Call another Procedure) int leaf_example ( int g, int h, int i, int j) { int f ; f = ( g + h ) – ( i + j ) ; return f ;} Assign g to $a0, h to $a1, i to $a2, j to $a3 and f to $v0. Leaf_example: add $t0, $a0, $a1 # Temp $t0 = g + h add $t1, $a2, $a3 # Temp $t1 = i + j sub $vo, $t0 , $t1 # $v0 = (g+h) – (i+j) jr $ra # jump back to calling routine What if the calling procedure uses $t0 and $t1?
Procedure Calls How can we preserve “saved registers” of the calling procedure ? What if there are not enough registers allocated to pass parameters and values ?
Procedure Calls Store the registers in memory using a stack. push $s0 pop $s0 High Stack $sp $sp contents of $s0 $sp Low
Stack Processes • A stack is a last-in-first-out queue
Stack Processes • A stack is a last-in-first-out queue • The stack pointer, $sp, points to the most recently • allocated address.
Stack Processes • A stack is a last-in-first-out queue • The stack pointer, $sp, points to the most recently • allocated address. • By convention, stacks grow from higher addresses • to lower addresses.
Stack Processes • A stack is a last-in-first-out queue • The stack pointer, $sp, points to the most recently • allocated address. • By convention, stacks grow from higher addresses • to lower addresses. • To push $s0, $s1, and $s2, first reduce $sp three words • and then save the registers.
Push on the Stack addi $sp, $sp, -12 # adjust stack pointer 3 words sw $s2, 8($sp) # store $s2 at $sp + 8 sw $s1, 4($sp) # store $s1 at $sp + 4 sw $s0, 0($sp) # store $s0 at $sp push $s2, $s1, and $s0 High $sp $sp contents of $s2 contents of $s1 contents of $s0 $sp Low
Pop off the Stack lw $s0, 0($sp) # restore $s0 from $sp lw $s1, 4($sp) # restore $s1 from $sp + 4 lw $s2, 8($sp) # restore $s2 from $sp + 8 addi $sp, $sp, 12 # adjust stack pointer 3 words pop $s0, $s1, and $s2 High $sp $sp contents of $s2 contents of $s1 contents of $s0 $sp Low
Procedure Call • Save the registers used by the procedure by • pushing on the stack at the start push $s2, $s1, and $s0 pop $s0, $s1, and $s2 High $sp $sp contents of $s2 contents of $s1 contents of $s0 $sp Low
Procedure Call • Save the registers used by the procedure by • pushing on the stack at the start • 2. Restore the registers used by the procedure by • popping off the stack at the end push $s2, $s1, and $s0 pop $s0, $s1, and $s2 High $sp $sp contents of $s2 contents of $s1 contents of $s0 $sp Low
Procedure Call • Also data and results can be transferred between the • procedure and calling program using the stack push $s2, $s1, and $s0 pop $s0, $s1, and $s2 High $sp $sp contents of $s2 contents of $s1 contents of $s0 $sp Low
Procedure Call Conventions • By agreement the following registers are preserved: • Saved Registers: $s0 - $s7 • Return Address: $ra • Which means that the called routine must return to the • calling program with these registers unchanged. • If the called routine changes any of these ( includes calling • a routine) it must first save them on the stack and restore • them upon return.
Procedure Call Conventions • By agreement the following registers are preserved: • Saved Registers: $s0 - $s7 • Return Address: $ra • Which means that the called routine must return to the • calling program with these registers unchanged. • If the called routine changes any of these ( includes calling • a routine) it must first save them on the stack and restore • them upon return. • The stack must be kept correct, so the Stack Pointer, $sp, • and the Stack above the Stack Pointer must be the same • across the procedure call.
Procedure Call Conventions • By agreement the following registers are not preserved: • Temporary Registers: $t0 - $t9 • Argument Registers: $a0 - $a3 • Return Value Registers: $v0 - $v1 • Stack below the stack pointer • Which means that the calling routine must push any of • these registers on the stack that are needed after the call. • Why not just push all the registers on the stack ? • When would this be necessary ?
MIPS Register Conventions Name Register Usage Preserved Number Across Call $zero 0 constant 0 na $v0-$v1 2-3 values for results no $a0-$a3 4-7 arguments no $t0-$t7 8-15 temporaries no $s0-$s7 16-23 saved yes $t8-$t9 24-25 more temporaries no $gp 28 global pointer yes $sp 29 stack pointer yes $fp 30 frame pointer yes $ra 31 return address yes Registers not listed are reserved for Assembler and OS