Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15:...
-
Upload
julius-tucker -
Category
Documents
-
view
217 -
download
1
Transcript of Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15:...
Computer Architecture CSE 3322Lecture 4
crystal.uta.edu/~jpatters/cse3322
Assignments due 9/15: 3.7, 3.9, 3.11
Computer Architecture CSE 3322Graduate Teaching Assistant
Pramod Kumar
Office Hours: Tues – Thurs 10 – 1 pm
114 Engr Annex West
Send email to Pramod Kumar with the names and emails of your Four Project team members by Mon Sept 15. If not on a team, send your email address to Pramod.
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 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.
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 $s2
contents 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 $s2
contents 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 $s2
contents 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 Conventions
By 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 Conventions
By 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 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 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 used by Assembler and OS
Procedure Check List1. Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops.
Procedure Check List1. Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops.2. To call a routine, the return address. $ra, must be saved before the call (jal) and restored upon return.
Procedure Check List1. Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops.2. To call a routine, the return address. $ra, must be saved before the call (jal) and restored upon return. Exception: Main program that cannot be called.3. Any saved registers, $s0 - $s7, that are used must be saved and restored upon return to the contents at the
time the procedure was called.
Procedure Check List1. Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops.2. To call a routine, the return address. $ra, must be saved before the call (jal) and restored upon return. Exception: Main program that cannot be called.3. Any saved registers, $s0 - $s7, that are used must be saved and restored upon return to the contents at the
time the procedure was called.4. Any temporary, argument or value register, $t0 - $t9,
$a0 - $a3, $v0 - $v1, that need to be preserved acrossthe call must be saved before the call and restoredupon returned
Calculate n factorial = n(n-1)(n-2)(n-3) ...1
Calculate n factorial = n(n-1)(n-2)(n-3) ...1 A recursive C procedure that calculates factorialint fact (int n ){ if ( n<1 ) return (1);
else return ( n * fact (n – 1)); }
Calculate n factorial = n(n-1)(n-2)(n-3) ...1 A recursive C procedure that calculates factorialint fact (int n ){ if ( n<1 ) return (1);
else return ( n * fact (n – 1)); }Assign n to $a0 and return value to $v0
What do we need to save?
Calculate n factorial = n(n-1)(n-2)(n-3) ...1 A recursive C procedure that calculates factorialint fact (int n ){ if ( n<1 ) return (1);
else return ( n * fact (n – 1)); }Assign n to $a0 and return value to $v0
What do we need to save?$a0 ?$v0 ?$ra ?
Calculate n factorial = n(n-1)(n-2)(n-3) ...1 A recursive C procedure that calculates factorialint fact (int n ){ if ( n<1 ) return (1);
else return ( n * fact (n – 1)); }Assign n to $a0 and return value to $v0fact: # push $a0 and $ra
addi $sp, $sp, -8 # adjust $sp for 2 wordssw $ra, 4($sp) # save return addresssw $a0, 0($sp) # save the argument n
$sp
$sp$ra
$a0
Calculate n factorial = n(n-1)(n-2)(n-3) ...1 A recursive C procedure that calculates factorialint fact (int n ){ if ( n<1 ) return (1);
else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0fact:
addi $sp, $sp, -8 # adjust $sp for 2 wordssw $ra, 4($sp) # save return addresssw $a0, 0($sp) # save the argument nslti $t0, $a0, 1 # test for n < 1beq $t0, $zero, L1 # if n >= 1 go to L1
Calculate n factorial = n(n-1)(n-2)(n-3) ...1 A recursive C procedure that calculates factorialint fact (int n ){ if ( n<1 ) return (1);
else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0fact:
addi $sp, $sp, -8 # adjust $sp for 2 wordssw $ra, 4($sp) # save return addresssw $a0, 0($sp) # save the argument nslti $t0, $a0, 1 # test for n < 1beq $t0, $zero, L1 # if n >= 1 go to L1addi $v0, $zero, 1 # return value of 1
Calculate n factorial = n(n-1)(n-2)(n-3) ...1 A recursive C procedure that calculates factorialint fact (int n ){ if ( n<1 ) return (1);
else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0fact:
addi $sp, $sp, -8 # adjust $sp for 2 wordssw $ra, 4($sp) # save return addresssw $a0, 0($sp) # save the argument nslti $t0, $a0, 1 # test for n < 1beq $t0, $zero, L1 # if n >= 1 go to L1addi $v0, $zero, 1 # return value of 1
Return to calling procedurePop $a0, $ra ?
Calculate n factorial = n(n-1)(n-2)(n-3) ...1 A recursive C procedure that calculates factorialint fact (int n ){ if ( n<1 ) return (1);
else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0fact:
addi $sp, $sp, -8 # adjust $sp for 2 wordssw $ra, 4($sp) # save return addresssw $a0, 0($sp) # save the argument nslti $t0, $a0, 1 # test for n < 1beq $t0, $zero, L1 # if n >= 1 go to L1addi $v0, $zero, 1 # return value of 1addi $sp, $sp, 8 # adjust $sp for 2 wordsjr $ra # return
Calculate n * fact ( n – 1 )L1: addi $a0, $a0, -1 # argument is ( n – 1)
jal fact # call fact with ( n – 1)
Calculate n * fact ( n – 1 )L1: addi $a0, $a0, -1 # argument is ( n – 1)
jal fact # call fact with ( n – 1)
Returns to this procedure with$v0 = (n-1)!
Calculate n * fact ( n – 1 )L1: addi $a0, $a0, -1 # argument is ( n – 1)
jal fact # call fact with ( n – 1)
Returns to this procedure with$v0 = (n-1)!
Restore $a0 and $raCalculate n * (n-1)!Return to calling procedure
Calculate n * fact ( n – 1 )L1: addi $a0, $a0, -1 # argument is ( n – 1)
jal fact # call fact with ( n – 1) # pop $a0 and $ra
lw $a0, 0($sp) # return from jal; restore to nlw $ra, 4($sp) # restore return addressaddi $sp, $sp, 8 # adjust $sp for 2 words
$sp$ra
$a0
$sp
Calculate n * fact ( n – 1 )L1: addi $a0, $a0, -1 # argument is ( n – 1)
jal fact # call fact with ( n – 1) # pop $a0 and $ra
lw $a0, 0($sp) # return from jal; restore to nlw $ra, 4($sp) # restore return addressaddi $sp, $sp, 8 # adjust $sp for 2 wordsmul $v0, $a0, $v0 # return value of n * fact(n–1)jr $ra # return to caller
mul, the multiply instruction will be covered later
fact:addi $sp, $sp, -8 # adjust $sp for 2 wordssw $ra, 4($sp) # save return addresssw $a0, 0($sp) # save the argument nslti $t0, $a0, 1 # test for n < 1beq $t0, $zero, L1 # if n >= 1 go to L1addi $v0, $zero, 1 # return value of 1addi $sp, $sp, 8 # adjust $sp for 2 wordsjr $ra # return
8 for n<1 or n*5 instructions otherwise(n+1)*2 memory accesses
L1: addi $a0, $a0, -1 # argument is ( n – 1)jal fact # call fact with ( n – 1)
# pop $a0 and $ralw $a0, 0($sp) # return from jal; restore to nlw $ra, 4($sp) # restore return addressaddi $sp, $sp, 8 # adjust $sp for 2 wordsmul $v0, $a0, $v0 # return value of n * fact(n–1)jr $ra # return to caller
n*7 instructionsn* 2 memory accesses
n*7 instructionsn* 2 memory accesses
8 or n*5 instructions(n+1)*2 memory accesses
Total8+n*12 instructions(2n+1)*2 memory accesses
Calculate n factorial = n(n-1)(n-2)(n-3) ...1
int fact (int n, int k){ k=1;
for ( i = 1; i <= n; i = i + 1) {k = k * i
}return( k )
}
Addressing in Jumps
op address
2 address
jump j Label go to Label
6 bits 26 bits
Addressing in Jumps
op address
2 address
jump j Label go to Label
6 bits 26 bits
The complete 32 bit address is :
address 00
4 bits 26 bits 2 bits
Upper 4 bits of the Program Counter, PCjump uses word addresses
This is Pseudodirect Addressing. Note: 256 MB word boundaries
address * 4 = address:00
Branch Addressing
op rs rt address
4 17 18
op rs rt address
5 17 18
address
address
branch on equal beq $s1, $s2, Label if ($s1 = =$s2) go to Label
branch on not equal bne $s1, $s2, Label if ($s1 != $s2) go to Label
Branch Addressing
op rs rt address
4 17 18 address
beq $s1, $s2, Label if ($s1 = =$s2) go to Label
6 bits 5 bits 5 bits 16 bits
effective 32 bit address = ( PC + 4 ) + ( address * 4 )
Next Instruction address is the relative number of instructions
This is PC-relative addressing
address * 4 = address : 00
Branch AddressingLoop: add $t1, $s1, $s1 # $t1 = 2 * i - - - Some Code - - -
beq $t0, $zero, Exit # if A[i]=0 goto Exitadd $s1, $s1, $s2 # i = i + jj Loop # goto Loop
Exit:
48000 0 17 17 9 0 3248004 •48032 4 8 0 ?4803648040 48044
Branch AddressingLoop: add $t1, $s1, $s1 # $t1 = 2 * i - - - Some Code - - -
beq $t0, $zero, Exit # if A[i]=0 goto Exitadd $s1, $s1, $s2 # i = i + jj Loop # goto Loop
Exit:
48000 0 17 17 9 0 3248004 •48032 4 8 0 ? 4803648040 48044
(PC+4)+?*4
Branch AddressingLoop: add $t1, $s1, $s1 # $t1 = 2 * i - - - Some Code - - -
beq $t0, #zero, Exit # if A[i]=0 goto Exitadd $s1, $s1, $s2 # i = i + jj Loop # goto Loop
Exit:
48000 0 17 17 9 0 3248004 •48032 4 8 0 24803648040 48044
Branch AddressingLoop: add $t1, $s1, $s1 # $t1 = 2 * i - - - Some Code - - -
beq $t0, #zero, Exit # if A[i]=0 goto Exitadd $s1, $s1, $s2 # i = i + jj Loop # goto Loop
Exit:
48000 0 17 17 9 0 3248004 •48032 4 8 0 248036 0 17 18 17 0 3248040 2 ?48044
Branch AddressingLoop: add $t1, $s1, $s1 # $t1 = 2 * i - - - Some Code - - -
beq $t0, #zero, Exit # if A[i]=0 goto Exitadd $s1, $s1, $s2 # i = i + jj Loop # goto Loop
Exit:
48000 0 17 17 9 0 3248004 •48032 4 8 0 248036 0 17 18 17 0 3248040 2 ?48044 ?*4
Branch AddressingLoop: add $t1, $s1, $s1 # $t1 = 2 * i - - - Some Code - - -
beq $t0, #zero, Exit # if A[i]=0 goto Exitadd $s1, $s1, $s2 # i = i + jj Loop # goto Loop
Exit:
48000 0 17 17 9 0 3248004 •48032 4 8 0 248036 0 17 18 17 0 3248040 2 1200048044