Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.

Similar presentations


Presentation on theme: "Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11."— Presentation transcript:

1 Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11

2 Computer Architecture CSE 3322 Graduate Teaching Assistant Pramod Kumar Office Hours: Tues – Thurs 10 – 1 pm 114 Engr Annex West pxk 3008@exchange.uta.edu 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.

3 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

4 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 ?

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

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

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

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

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

10 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

11 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

12 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

13 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

14 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

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

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

17 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 ?

18 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 used by Assembler and OS

19 Procedure Check List 1.Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops.

20 Procedure Check List 1.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.

21 Procedure Check List 1.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.

22 Procedure Check List 1.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 across the call must be saved before the call and restored upon returned

23 Calculate n factorial = n(n-1)(n-2)(n-3)...1

24 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); }

25 Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int 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?

26 Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int 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 ?

27 Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact:# push $a0 and $ra addi $sp, $sp, -8# adjust $sp for 2 words sw $ra, 4($sp)# save return address sw $a0, 0($sp)# save the argument n $sp $ra $a0

28 Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1

29 Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1 addi $v0, $zero, 1 # return value of 1

30 Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1 addi $v0, $zero, 1 # return value of 1 Return to calling procedure Pop $a0, $ra ?

31 Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1 addi $v0, $zero, 1 # return value of 1 addi $sp, $sp, 8 # adjust $sp for 2 words jr $ra # return

32 Calculate n * fact ( n – 1 ) L1:addi $a0, $a0, -1 # argument is ( n – 1) jal fact # call fact with ( n – 1)

33 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)!

34 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 $ra Calculate n * (n-1)! Return to calling procedure

35 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 n lw $ra, 4($sp) # restore return address addi $sp, $sp, 8 # adjust $sp for 2 words $sp $ra $a0 $sp

36 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 n lw $ra, 4($sp) # restore return address addi $sp, $sp, 8 # adjust $sp for 2 words mul $v0, $a0, $v0 # return value of n * fact(n–1) jr $ra # return to caller mul, the multiply instruction will be covered later

37 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1 addi $v0, $zero, 1 # return value of 1 addi $sp, $sp, 8 # adjust $sp for 2 words jr $ra # return 8 for n<1 or n*5 instructions otherwise (n+1)*2 memory accesses

38 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 n lw $ra, 4($sp) # restore return address addi $sp, $sp, 8 # adjust $sp for 2 words mul $v0, $a0, $v0 # return value of n * fact(n–1) jr $ra # return to caller n*7 instructions n* 2 memory accesses

39 n*7 instructions n* 2 memory accesses 8 or n*5 instructions (n+1)*2 memory accesses Total 8+n*12 instructions (2n+1)*2 memory accesses

40 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 ) }

41 Addressing in Jumps op address 2address jump j Label go to Label 6 bits 26 bits

42 Addressing in Jumps op address 2address 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, PC jump uses word addresses This is Pseudodirect Addressing. Note: 256 MB word boundaries address * 4 = address:00

43 Branch Addressing op rs rt address 4 1718 op rs rt address 5 1718 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

44 Branch Addressing op rs rt address 4 1718 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

45 Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i - - - Some Code - - - beq $t0, $zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: 48000 0 17 17 9 0 32 48004 48032 4 8 0 ? 48036 48040 48044

46 Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i - - - Some Code - - - beq $t0, $zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: 48000 0 17 17 9 0 32 48004 48032 4 8 0 ? 48036 48040 48044 (PC+4)+?*4

47 Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i - - - Some Code - - - beq $t0, #zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: 48000 0 17 17 9 0 32 48004 48032 4 8 0 2 48036 48040 48044

48 Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i - - - Some Code - - - beq $t0, #zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: 48000 0 17 17 9 0 32 48004 48032 4 8 0 2 48036 0 17 18 17 0 32 48040 2 ? 48044

49 Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i - - - Some Code - - - beq $t0, #zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: 48000 0 17 17 9 0 32 48004 48032 4 8 0 2 48036 0 17 18 17 0 32 48040 2 ? 48044 ?*4

50 Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i - - - Some Code - - - beq $t0, #zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: 48000 0 17 17 9 0 32 48004 48032 4 8 0 2 48036 0 17 18 17 0 32 48040 2 12000 48044


Download ppt "Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11."

Similar presentations


Ads by Google