Computer Architecture CSE 3322 Lecture 4 Assignment 2

  • Slides: 39
Download presentation
Computer Architecture CSE 3322 Lecture 4 Assignment: 2. 4. 1, 2. 4. 4, 2.

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/~cse 3322

Case / Switch Statement switch ( k ) { case 0: statement 0; break

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 / 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 / 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 ~ $s 0 slt $t 0, $s 0, $zero # $t 0 = 1 if k < 0 bne $t 0, $zero, Exit # goto Exit if k < 0

Case / Switch Statement switch ( k ) { case 0: statement 0; 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 So, if k ~ $s 0 slt $t 0, $s 0, $zero # $t 0 = 1 if k < 0 bne $t 0, $zero, Exit # goto Exit if k < 0 And the test for k > 2 is, assuming $s 1 = 3 slt $t 0, $s 1 # $t 0 = 1 if k < 3 beq $t 0, $zero, Exit # goto Exit if k >=3

Case / Switch Statement switch ( k ) { case 0: statement 0; break

Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } Jump. Table[ k ] addr of statement 2 addr of statement 1 addr of statement 0 For a given k, place Jump. Table[ k ] in register $t 0 using lw instruction

Case / Switch Statement switch ( k ) { case 0: statement 0; break

Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } Jump. Table[ k ] addr of statement 2 addr of statement 1 addr of statement 0 load Jump. Table[ 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

Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } slt bne slt beq Exit: k is in $s 0, Start of Jump. Table is in $t 1 $t 0, $s 0, $zero # $t 0 = 1 if k < 0 $t 0, $zero, Exit # goto Exit if k < 0 $t 0, $s 1 # $t 0 = 1 if k < 3 $t 0, $zero, Exit # goto Exit if k >=3

Case / Switch Statement switch ( k ) { case 0: statement 0; break

Case / Switch Statement switch ( k ) { case 0: statement 0; break case 1: statement 1; break case 2: statement 2; break } slt bne slt beq add add lw jr Exit: k is in $s 0, Start of Jump. Table is in $t 1 $t 0, $s 0, $zero # $t 0 = 1 if k < 0 $t 0, $zero, Exit # goto Exit if k < 0 $t 0, $s 1 # $t 0 = 1 if k < 3 $t 0, $zero, Exit # goto Exit if k >=3 $t 0, $s 0 # $t 0 = 2 * k $t 0, $t 0 # $t 0 = 4 * k $t 0, $t 1 # $t 0 = addr of Jump. Table[k] $t 2, 0( $t 0) # $t 2 = Jump. Table[k] $t 2 # jump to addr in $t 2

MIPS Assembly Instructions Pseudo instructions Instructions supported by the Assembler but not implemented in

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 or equal

MIPS Immediate Addressing Very common to use a constant in arithmetic operations. Examples? Make

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 $s 1, $s 2, constant op rs rt $s 1 = $s 2 + constant 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

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 $t 0, $s 2, constant rt $t 0 = 1 if $s 2 < constant else $t 0 = 0 op rs 10 18 8 constant 6 5 5 16 I type of format immediate

Procedure Calls 1. Place parameters where the procedure can access them

Procedure Calls 1. Place parameters where the procedure can access them

Procedure Calls 1. Place parameters where the procedure can access them 2. Transfer control

Procedure Calls 1. Place parameters where the procedure can access them 2. Transfer control to the procedure

Procedure Calls 1. Place parameters where the procedure can access them 2. Transfer control

Procedure Calls 1. Place parameters where the procedure can access them 2. Transfer control to the procedure 3. Perform the task of the procedure

Procedure Calls 1. 2. 3. 4. Place parameters where the procedure can access them

Procedure Calls 1. 2. 3. 4. 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 1. 2. 3. 4. Place parameters where the procedure can access them

Procedure Calls 1. 2. 3. 4. 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 1. 2. 3. 4. Place parameters where the procedure can access them

Procedure Calls 1. 2. 3. 4. 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 Allocate registers to hold data for procedure calls $a 0 - $a 3 : four registers to pass parameters $v 0 - $v 1 : two registers to return values $ra : one return address register

Procedure Calls 1. 2. 3. 4. Place parameters where the procedure can access them

Procedure Calls 1. 2. 3. 4. 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 Allocate registers to hold data for procedure calls $a 0 - $a 3 : four registers to pass parameters $v 0 - $v 1 : two registers to return values $ra : one return address register Need jump-and-link instruction : jal Procedure. Address means : save return address in $ra and jumps to Procedure. Address

Procedure Calls 1. 2. 3. 4. Place parameters where the procedure can access them

Procedure Calls 1. 2. 3. 4. 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 Allocate registers to hold data for procedure calls $a 0 - $a 3 : four registers to pass parameters $v 0 - $v 1 : two registers to return values $ra : one return address register Need jump-and-link instruction : jal Procedure. Address means : save return address in $ra and jumps to Procedure. Address How do you return?

Compiling a “leaf” Procedure (Does not Call another Procedure) int leaf_example ( int g,

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,

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 $a 0, h to $a 1, i to $a 2, j to $a 3 and f to $v 0.

Compiling a “leaf” Procedure (Does not Call another Procedure) int leaf_example ( int g,

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 $a 0, h to $a 1, i to $a 2, j to $a 3 and f to $v 0. Leaf_example: add $t 0, $a 1 # Temp $t 0 = g + h add $t 1, $a 2, $a 3 # Temp $t 1 = i + j sub $v 0, $t 0 , $t 1 # $v 0 = (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,

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 $a 0, h to $a 1, i to $a 2, j to $a 3 and f to $v 0. Leaf_example: add $t 0, $a 1 # Temp $t 0 = g + h add $t 1, $a 2, $a 3 # Temp $t 1 = i + j sub $vo, $t 0 , $t 1 # $v 0 = (g+h) – (i+j) jr $ra # jump back to calling routine What if the calling procedure uses $t 0 and $t 1?

Procedure Calls How can we preserve “saved registers” of the calling procedure ? What

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 $s 0 High

Procedure Calls Store the registers in memory using a stack. push $s 0 High pop $s 0 Stack $sp Low $sp contents of $s 0 $sp

Stack Processes • A stack is a last-in-first-out queue

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,

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,

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,

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 $s 0, $s 1, and $s 2, first reduce $sp three words and then save the registers.

Push on the Stack addi $sp, -12 sw $s 2, 8($sp) sw $s 1,

Push on the Stack addi $sp, -12 sw $s 2, 8($sp) sw $s 1, 4($sp) sw $s 0, 0($sp) High $sp Low # adjust stack pointer 3 words # store $s 2 at $sp + 8 # store $s 1 at $sp + 4 # store $s 0 at $sp push $s 2, $s 1, and $s 0 contents of $s 2 contents of $s 1 $sp contents of $s 0 $sp

Pop off the Stack lw lw lw addi $s 0, 0($sp) $s 1, 4($sp)

Pop off the Stack lw lw lw addi $s 0, 0($sp) $s 1, 4($sp) $s 2, 8($sp) $sp, 12 # restore $s 0 from $sp # restore $s 1 from $sp + 4 # restore $s 2 from $sp + 8 # adjust stack pointer 3 words pop $s 0, $s 1, and $s 2 High $sp Low contents of $s 2 contents of $s 1 $sp contents of $s 0 $sp

Procedure Call 1. Save the registers used by the procedure by pushing on the

Procedure Call 1. Save the registers used by the procedure by pushing on the stack at the start High $sp Low push $s 2, $s 1, and $s 0 pop $s 0, $s 1, and $s 2 contents of $s 1 $sp contents of $s 0 $sp

Procedure Call 1. Save the registers used by the procedure by pushing on the

Procedure Call 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 High $sp Low push $s 2, $s 1, and $s 0 pop $s 0, $s 1, and $s 2 contents of $s 1 $sp contents of $s 0 $sp

Procedure Call Also data and results can be transferred between the procedure and calling

Procedure Call Also data and results can be transferred between the procedure and calling program using the stack High $sp Low push $s 2, $s 1, and $s 0 pop $s 0, $s 1, and $s 2 contents of $s 1 $sp contents of $s 0 $sp

Procedure Call Conventions By agreement the following registers are preserved: • Saved Registers: $s

Procedure Call Conventions By agreement the following registers are preserved: • Saved Registers: $s 0 - $s 7 • 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: $s

Procedure Call Conventions By agreement the following registers are preserved: • Saved Registers: $s 0 - $s 7 • 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:

Procedure Call Conventions By agreement the following registers are not preserved: • Temporary Registers: $t 0 - $t 9 • Argument Registers: $a 0 - $a 3 • Return Value Registers: $v 0 - $v 1 • 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

MIPS Register Conventions Name Register Usage Preserved Number Across Call $zero 0 constant 0 na $v 0 -$v 1 2 -3 values for results no $a 0 -$a 3 4 -7 arguments no $t 0 -$t 7 8 -15 temporaries no $s 0 -$s 7 16 -23 saved yes $t 8 -$t 9 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