Download presentation
Presentation is loading. Please wait.
Published byLinette Wiggins Modified over 9 years ago
1
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
2
Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break}
3
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
4
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
5
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
6
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
7
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
8
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:
9
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:
10
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
11
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!
12
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
13
Procedure Calls 1.Place parameters where the procedure can access them
14
Procedure Calls 1.Place parameters where the procedure can access them 2.Transfer control to the procedure
15
Procedure Calls 1.Place parameters where the procedure can access them 2.Transfer control to the procedure 3.Perform the task of the procedure
16
Procedure Calls 1.Place parameters where the procedure can access them 2.Transfer control to the procedure 3.Perform the task of the procedure 4.Place the results where the calling program can access them
17
Procedure Calls 1.Place parameters where the procedure can access them 2.Transfer control to the procedure 3.Perform the task of the procedure 4.Place the results where the calling program can access them 5. Return control to the point of the call
18
Procedure Calls 1.Place parameters where the procedure can access them 2.Transfer control to the procedure 3.Perform the task of the procedure 4.Place the results where the calling program can access them 5. Return control to the point of the call 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
19
Procedure Calls 1.Place parameters where the procedure can access them 2.Transfer control to the procedure 3.Perform the task of the procedure 4.Place the results where the calling program can access them 5. Return control to the point of the call 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
20
Procedure Calls 1.Place parameters where the procedure can access them 2.Transfer control to the procedure 3.Perform the task of the procedure 4.Place the results where the calling program can access them 5. Return control to the point of the call 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?
21
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 ;}
22
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.
23
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
24
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?
25
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 ?
26
Procedure Calls Store the registers in memory using a stack. High Low $sp push $s0 contents of $s0 pop $s0 Stack
27
Stack Processes A stack is a last-in-first-out queue
28
Stack Processes A stack is a last-in-first-out queue The stack pointer, $sp, points to the most recently allocated address.
29
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.
30
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.
31
Push on the Stack High Low $sp push $s2, $s1, and $s0 contents of $s2 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 contents of $s1 contents of $s0
32
Pop off the Stack High Low $sp pop $s0, $s1, and $s2 contents of $s2 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 contents of $s1 contents of $s0
33
Procedure Call High Low $sp push $s2, $s1, and $s0 contents of $s2 contents of $s1 contents of $s0 pop $s0, $s1, and $s2 1.Save the registers used by the procedure by pushing on the stack at the start
34
Procedure Call High Low $sp push $s2, $s1, and $s0 contents of $s2 contents of $s1 contents of $s0 pop $s0, $s1, and $s2 1.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
35
Procedure Call High Low $sp push $s2, $s1, and $s0 contents of $s2 contents of $s1 contents of $s0 pop $s0, $s1, and $s2 Also data and results can be transferred between the procedure and calling program using the stack
36
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.
37
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.
38
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 ?
39
MIPS Register Conventions Name Register Usage Preserved Number Across Call $zero 0 constant 0na $v0-$v1 2-3values for resultsno $a0-$a3 4-7argumentsno $t0-$t78-15temporariesno $s0-$s7 16-23savedyes $t8-$t9 24-25more temporariesno $gp 28global pointeryes $sp 29stack pointeryes $fp 30frame pointeryes $ra 31return addressyes Registers not listed are reserved for Assembler and OS
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.