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

39
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

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

Page 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

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

Page 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

Case / Switch Statement

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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:

Page 9: 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

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:

Page 10: 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

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

Page 11: 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

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!

Page 12: 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

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

Page 13: 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

Procedure Calls

1. Place parameters where the procedure can access them

Page 14: 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

Procedure Calls

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

Page 15: 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

Procedure Calls

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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?

Page 21: 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

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 ;}

Page 22: 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

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.

Page 23: 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

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

Page 24: 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

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?

Page 25: 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

Procedure CallsHow can we preserve “saved registers” of the calling procedure ?

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

Page 26: 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

Procedure CallsStore the registers in memory using a stack.

High

Low

$sp $sp$sp

push $s0

contents of $s0

pop $s0

Stack

Page 27: 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

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

Page 28: 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

Stack Processes• A stack is a last-in-first-out queue• The stack pointer, $sp, points to the most recently allocated address.

Page 29: 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

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.

Page 30: 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

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.

Page 31: 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

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

Page 32: 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

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

Page 33: 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

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

Page 34: 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

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

Page 35: 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

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

Page 36: 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

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.

Page 37: 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

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.

Page 38: 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

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 ?

Page 39: 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

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