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

Post on 17-Jan-2018

218 views 0 download

description

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

Transcript of 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

Computer Architecture CSE 3322Lecture 4

Assignment: 2.4.1, 2.4.4, 2.6.1, 2.10.4, 2.10.6Due 2/10/09

http://crystal.uta.edu/~cse3322

Case / Switch Statement

switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break}

Case / Switch Statement

switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break }

if k < 0, then Exitslt 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; breakcase 1: statement 1; breakcase 2: statement 2; break }

if k < 0, then Exitslt set on less than slt rd, rs, rt

means if rs < rt, rd = 1, else rd=0So, if k ~ $s0

slt $t0, $s0, $zero # $t0 = 1 if k < 0bne $t0, $zero, Exit # goto Exit if k < 0

Case / Switch Statement

switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break }

if k < 0, then Exitslt set on less than slt rd, rs, rt

means if rs < rt, rd = 1, else rd=0So, if k ~ $s0

slt $t0, $s0, $zero # $t0 = 1 if k < 0bne $t0, $zero, Exit # goto Exit if k < 0

And the test for k > 2 is, assuming $s1 = 3slt $t0, $s0, $s1 # $t0 = 1 if k < 3beq $t0, $zero, Exit # goto Exit if k >=3

Case / Switch Statement

switch ( k ) {case 0: statement 0; breakcase 1: statement 1; breakcase 2: statement 2; break }

JumpTable[ k ] addr of statement 2addr of statement 1addr 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; breakcase 1: statement 1; breakcase 2: statement 2; break }

JumpTable[ k ] addr of statement 2addr of statement 1addr of statement 0

load JumpTable[ k ] in a register and jump to it

jr jump register jr rsmeans go to address in register rs

Case / Switch Statementswitch ( k ) {

case 0: statement 0; break k is in $s0, case 1: statement 1; break Start of JumpTable iscase 2: statement 2; break } in $t1

slt $t0, $s0, $zero # $t0 = 1 if k < 0bne $t0, $zero, Exit # goto Exit if k < 0slt $t0, $s0, $s1 # $t0 = 1 if k < 3beq $t0, $zero, Exit # goto Exit if k >=3

Exit:

Case / Switch Statementswitch ( k ) {

case 0: statement 0; break k is in $s0, case 1: statement 1; break Start of JumpTable iscase 2: statement 2; break } in $t1

slt $t0, $s0, $zero # $t0 = 1 if k < 0bne $t0, $zero, Exit # goto Exit if k < 0slt $t0, $s0, $s1 # $t0 = 1 if k < 3beq $t0, $zero, Exit # goto Exit if k >=3add $t0, $s0, $s0 # $t0 = 2 * kadd $t0, $t0, $t0 # $t0 = 4 * kadd $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 instructionsInstructions supported by the Assembler butnot implemented in hardware.Ex: move

multiplybranch less than, less than or equal,

greater than, greater than or equal

MIPS Immediate AddressingVery 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 AddressingVery 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

1. Place parameters where the procedure can access them

Procedure Calls

1. Place parameters where the procedure can access them2. Transfer control to the procedure

Procedure Calls

1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure

Procedure Calls

1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access

them

Procedure Calls

1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access

them5. Return control to the point of the call

Procedure Calls1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access

them5. 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

Procedure Calls1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access

them5. 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

Procedure Calls1. Place parameters where the procedure can access them2. Transfer control to the procedure3. Perform the task of the procedure4. Place the results where the calling program can access

them5. 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 ProcedureAddressHow 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 + hadd $t1, $a2, $a3 # Temp $t1 = i + jsub $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 + hadd $t1, $a2, $a3 # Temp $t1 = i + jsub $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 CallsHow can we preserve “saved registers” of the calling procedure ?

What if there are not enough registers allocated to pass parameters and values ?

Procedure CallsStore the registers in memory using a stack.

High

Low

$sp $sp$sp

push $s0

contents of $s0

pop $s0

Stack

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

High

Low

$sp $sp

$sp

push $s2, $s1, and $s0

contents of $s2

addi $sp, $sp, -12 # adjust stack pointer 3 wordssw $s2, 8($sp) # store $s2 at $sp + 8sw $s1, 4($sp) # store $s1 at $sp + 4sw $s0, 0($sp) # store $s0 at $sp

contents of $s1contents of $s0

Pop off the Stack

High

Low

$sp $sp

$sp

pop $s0, $s1, and $s2

contents of $s2

lw $s0, 0($sp) # restore $s0 from $splw $s1, 4($sp) # restore $s1 from $sp + 4lw $s2, 8($sp) # restore $s2 from $sp + 8 addi $sp, $sp, 12 # adjust stack pointer 3 words

contents of $s1contents of $s0

Procedure Call

High

Low

$sp $sp

$sp

push $s2, $s1, and $s0

contents of $s2contents of $s1contents of $s0

pop $s0, $s1, and $s2

1. Save the registers used by the procedure by pushing on the stack at the start

Procedure Call

High

Low

$sp $sp

$sp

push $s2, $s1, and $s0

contents of $s2contents of $s1contents 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 bypopping off the stack at the end

Procedure Call

High

Low

$sp $sp

$sp

push $s2, $s1, and $s0

contents of $s2contents of $s1contents of $s0

pop $s0, $s1, and $s2

Also data and results can be transferred between theprocedure and calling program using the stack

Procedure Call ConventionsBy agreement the following registers are preserved:• Saved Registers: $s0 - $s7• Return Address: $ra

Which means that the called routine must return to thecalling program with these registers unchanged. If the called routine changes any of these ( includes callinga routine) it must first save them on the stack and restore them upon return.

Procedure Call ConventionsBy agreement the following registers are preserved:• Saved Registers: $s0 - $s7• Return Address: $ra

Which means that the called routine must return to thecalling program with these registers unchanged. If the called routine changes any of these ( includes callinga 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 sameacross the procedure call.

Procedure Call ConventionsBy 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 ofthese 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 ConventionsName 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 yesRegisters not listed are reserved for Assembler and OS