Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Architecture Procedure Calls

Similar presentations


Presentation on theme: "Computer Architecture Procedure Calls"— Presentation transcript:

1 Computer Architecture Procedure Calls
Dr. Hadi AL Saadi Faculty Of Information Technology University of Petra 4/4/2019 Dr. Hadi Hassan Computer architecture

2 Dr. Hadi Hassan Computer architecture
Outline of Lecture Procedure calls Saving and restoring registers Run Time Stack 4/4/2019 Dr. Hadi Hassan Computer architecture

3 Dr. Hadi Hassan Computer architecture
What is a procedure? Procedure – a reusable chunk of code in your program used to do the same thing in different places (reuse) used to logically organize your program (decomposition) like a method in Java, or a procedure/function in C Can make a distinction between: procedure – does not return a result function – does return a result (but don’t worry too much about that) Procedures can call other procedures including themselves! (recursion) 4/4/2019 Dr. Hadi Hassan Computer architecture

4 Dr. Hadi Hassan Computer architecture
Procedure Calls Caller vs. callee caller the code that calls the procedure callee the code that implements the procedure A procedure of a subroutine is like an “agent” which needs certain information to perform a certain job. In MIPS a calller calls a procedure (callee). When the callee is executed, the following steps are taken: Place parameters in a place where the callee can access them; Transfer control to the callee. Acquire the storage resources needed for the callee. Perform the task. Place result value in a place where the caller can access it. Return the control to the caller 4/4/2019 Dr. Hadi Hassan Computer architecture

5 Dr. Hadi Hassan Computer architecture
Procedure Calls The following registers are used during the procedure call: In most cases, passing parameters is straightforward, following the MIPS convention: $a0 # 1st parameter $a1 # 2nd parameter $a2 # 3rd parameter $a3 # 4th parameter $v0 — $v1 (two value registers (return value registers )) $ra (return address register) 4/4/2019 Dr. Hadi Hassan Computer architecture

6 Instructions for Procedures
JAL (Jump-and-Link) used as the call instruction Save return address in $ra = PC+4 and jump to procedure Register $ra = $31 is used by JAL as the return address JR (Jump Register) used to return from a procedure Jump to instruction whose address is in register Rs (PC = Rs) JALR (Jump-and-Link Register) Save return address in Rd = PC+4, and Jump to procedure whose address is in register Rs (PC = Rs) Can be used to call methods (addresses known only at runtime) Instruction Meaning Format jal label $31=PC+4, jump op6 = 3 imm26 jr Rs PC = Rs op6 = 0 rs5 8 jalr Rd, Rs Rd=PC+4, PC=Rs rd5 9 4/4/2019 Dr. Hadi Hassan Computer architecture

7 Arguments and return values
Procedure use # Pseudocode: # c = sumOfSquares(3,5) # Registers: c => $t2 li $a0, # (set up arguments) li $a1, 5 jal sumOfSquares # (call procedure) move $a0, $v0 # (get result) li $v0,1 syscall li $v0,10 Procedure definition # int sumOfSquares(int a, int b) { # return a*a + b*b # } # Registers: a => $a0, b => $a1, res => $v0 sumOfSquares: mult $a0, $a0 # tmp1 = a*a Mflo $t0 mult $a1, $a1 # tmp2 = b*b mflo $t1 add $v0, $t0, $t1 # res = tmp1 + tmp2 jr $ra # return res 4/4/2019 Dr. Hadi Hassan Computer Organization

8 Dr. Hadi Hassan Computer architecture
Procedure example 1 Consider the following swap procedure (written in C) Translate this procedure to MIPS assembly language void swap(int v[], int k) { int temp; temp = v[k] v[k] = v[k+1]; v[k+1] = temp; } swap: sll $t0,$a1,2 # $t0=k*4 add $t0,$t0,$a0 # $t0=v+k*4 lw $t1,0($t0) # $t1=v[k] lw $t2,4($t0) # $t2=v[k+1] sw $t2,0($t0) # v[k]=$t2 sw $t1,4($t0) # v[k+1]=$t1 jr $ra # return Parameters: $a0 = Address of v[] $a1 = k, and Return address is in $ra 4/4/2019 Dr. Hadi Hassan Computer architecture

9 Dr. Hadi Hassan Computer architecture
Call / Return Sequence Suppose we call procedure swap as: swap(a,10) Pass address of array a and 10 as arguments Call the procedure swap saving return address in $31 = $ra Execute procedure swap Return control to the point of origin (return address) Callee swap: sll $t0,$a1,2 add $t0,$t0,$a0 lw $t1,0($t0) lw $t2,4($t0) sw $t2,0($t0) sw $t1,4($t0) jr $ra Registers Caller . . . la $a0, a li $a1, 10 jal swap # return here . . . $a0=$4 addr a $a1=$5 10 . . . $ra=$31 ret addr 4/4/2019 Dr. Hadi Hassan Computer architecture

10 Details of JAL and JR Address Instructions Assembly Language
lui $1, 0x1001 la $a0, a ori $4, $1, 0 ori $5, $0, 10 ori $a1,$0,10 C jal 0x10000f jal swap # return here swap: C sll $8, $5, 2 sll $t0,$a1,2 add $8, $8, $4 add $t0,$t0,$a0 lw $9, 0($8) lw $t1,0($t0) lw $10,4($8) lw $t2,4($t0) C sw $10,0($8) sw $t2,0($t0) sw $9, 4($8) sw $t1,4($t0) jr $31 jr $ra Pseudo-Direct Addressing PC = imm26<<2 0x10000f << 2 = 0x C 0x $31 Register $31 is the return address register 4/4/2019 Dr. Hadi Hassan Computer architecture

11 Dr. Hadi Hassan Computer architecture
Procedure Example 2 ######################################################################### # Returns largest value in an array of integers. # Pre: $a0 points to the first array element # $a1 is the number of elements in the array # Post: $a0 points one past the end of the array # $a1 is unchanged # $v0 is the largest value in the array # Uses: $t0, $t1 # Calls: none FindMax: # label to jump to (proc name) sll $t0, $a1, # calculate offset to end of array add $t0, $a0, $t # calculate stop address lw $v0, 0($a0) # initial max value is 0-th element addi $a0, $a0, # step pointer to next element fmaxLoop: bge $a0, $t0, fmaxDone # see if we're past the array end lw $t1, 0($a0) # get next array element bge $v0, $t1, noNewMax # no new max move $v0, $t # reset max noNewMax: j fmaxLoop # return to loop test fmaxDone: jr $ra # return to caller 4/4/2019 Dr. Hadi Hassan Computer architecture

12 Dr. Hadi Hassan Computer architecture
Procedure Call Example 2 We need to prepare the parameters, and use jal to make the call: . . . .data Size: .word 10 List: .word 78, 23, 41, 55, 18, 37, 81, 49, 74, 89 .text main: la $a0, List # $a0 is a pointer to the array lw $a1, Size # $a1 is the array size jal FindMax # call FindMax procedure 4/4/2019 Dr. Hadi Hassan Computer architecture

13 Dr. Hadi Hassan Computer architecture
Procedure-related Conventions By convention, the caller will use: - registers $s0 - $s7 for values it expects to be preserved across any procedure calls it makes - registers $t0 - $t9 for values it does not expect to be preserved It is the responsibility of the called procedure to make sure that if it uses any of the registers $s0 - $s7 it backs them up on the system stack first, and restores them before returning. Obviously, the called procedure also takes responsibility to: - allocate any needed space on the stack for local data - place the return value onto the stack, if necessary - ensure the value of the stack pointer is the same after the call as it was before the call In some situations, it is useful for the caller to also maintain the value that $sp held when the call was made, called the frame pointer. The register $fp would be used for this purpose. 4/4/2019 Dr. Hadi Hassan Computer architecture

14 Dr. Hadi Hassan Computer architecture
MIPS Memory Organization In addition to memory for static data and the program text (machine code), MIPS provides space for the run-time stack (data local to procedures, etc.) and for dynamically-allocated data: Stack Dynamic data Static data Text Reserved $sp # last word alloc on stack $gp # ptr into global data $pc # ptr to next instruction Dynamic data is accessed via pointers held by the program being executed, with addresses returned by the memory allocator in the underlying operating system. 4/4/2019 Dr. Hadi Hassan Computer architecture

15 Dr. Hadi Hassan Computer architecture
The System Stack The stack a place in memory composed of stack frames each frame stores stuff specific to one procedure call each call can generate a new stack frame stack is extensible! Note that the stack may contain many frames for the same procedure if it is called multiple times! 4/4/2019 Dr. Hadi Hassan Computer architecture

16 Dr. Hadi Hassan Computer architecture
The System Stack MIPS provides a special register, $sp, which holds the address of the most recently allocated word on a stack that user programs can employ to hold various values: Note that this run-time stack is "upside-down". That is, $sp, decreases when a value is added to the stack and increases when a value is removed. So, you decrement the stack pointer by 4 when pushing a new value onto the stack and increment it by 4 when popping a value off of the stack. 4/4/2019 Dr. Hadi Hassan Computer architecture

17 Dr. Hadi Hassan Computer architecture
Using the System Stack MIPS programs use the runtime stack to hold: - parameters to be passed to a called procedure - register values that need to be preserved during the execution of a called procedure and restored after the return - saved procedure return address, if necessary - local arrays and structures, if any activation record or stack frame for called procedure 4/4/2019 Dr. Hadi Hassan Computer architecture

18 Dr. Hadi Hassan Computer architecture
Example 3 Since the registers are used by various procedures, their old values can be written by new values, so they must be saved. Look at the following code: int leaf_example(int g, int h, int i, int j) { int f; f = (g+h) - (i+j) return f; } Suppose $a0 <— g $a1 <— h $a2 <— i $a3 <— j 4/4/2019 Dr. Hadi Hassan Computer architecture

19 Dr. Hadi Hassan Computer architecture
Then the MIPS code for the instruction f = (g+h) - (i+j) would look like this add $t0, $a0, $a1 #register $t0 contains g+h add $t1, $a2, $a3 #register $t1 contains i+j sub $s0, $t0, $t1 #register f = $t0 - $t1 to return the value of f, we copy it into a return value register add $v0, $s0, $zero # return f ($v0 = $s0 +0) 4/4/2019 Dr. Hadi Hassan Computer architecture

20 Dr. Hadi Hassan Computer architecture
Saving Register In this code, we use temporary registers. Suppose their old values must be saved and then restored. To do so, we use stack. Before executing the above code, we execute the following code addi $sp, $sp, #adjust stack for 3 items sw $t1, 8($sp) #save register $t1 sw $t0, 4($sp) #save register $t0 sw $s0, 0($sp) #save register $S0 4/4/2019 Dr. Hadi Hassan Computer architecture

21 Dr. Hadi Hassan Computer architecture
Restoring Register After calculating the value of f, we must restore the old values of temporary registers. For this purpose, we execute the following code lw $s0, 0($sp) #restore register $s0 lw $t0, 4($sp) #restore register $t0 lw $t1, 8($sp) #restore register $t1 addi $sp, $sp, #adjust stack for 3 items # Do not forget to add jr $ra # jump back to the caller Note: PUSH and POP can be done for $s0 only, since $t0, and $t1 are temporary registers. This will save time. 4/4/2019 Dr. Hadi Hassan Computer architecture

22 Dr. Hadi Hassan Computer architecture
Stack Frame Stack frame is the segment of the stack containing … Saved arguments, registers, and local data structures (if any) Called also the activation frame or activation record Frames are pushed and popped by adjusting … Stack pointer $sp = $29 and Frame pointer $fp = R30 Decrement $sp to allocate stack frame, and increment to free $fp arguments saved $ra saved registers local data structures or variables $sp Frame f() Stack stack grows downwards $fp $sp Frame f() Stack allocate stack frame Frame g() $fp $sp Frame f() Stack free stack frame $fp $sp f calls g 4/4/2019 Dr. Hadi Hassan Computer architecture

23 Dr. Hadi Hassan Computer Organization
Selection Sort first last Array Unsorted first last Array max value last value max Locate Max first last Array max value last value max Swap Max with Last first last Array max value Decrement Last Example 3 1 5 2 4 last max first 3 1 4 2 5 3 1 2 4 5 3 1 2 4 5 last first max 2 1 3 4 5 2 1 3 4 5 last first max 1 2 3 4 5 3 1 4 2 5 last max first 4/4/2019 Dr. Hadi Hassan Computer Organization

24 Selection Sort Procedure
# Objective: Sort array using selection sort algorithm # Input: $a0 = pointer to first, $a1 = pointer to last # Output: array is sorted in place ########################################################## sort: addiu $sp, $sp, -4 # allocate one word on stack sw $ra, 0($sp) # save return address on stack top: jal max # call max procedure lw $t0, 0($a1) # $t0 = last value sw $t0, 0($v0) # swap last and max values sw $v1, 0($a1) addiu $a1, $a1, -4 # decrement pointer to last bne $a0, $a1, top # more elements to sort lw $ra, 0($sp) # pop return address addiu $sp, $sp, 4 jr $ra # return to caller 4/4/2019 Dr. Hadi Hassan Computer Organization

25 Dr. Hadi Hassan Computer Organization
Max Procedure # Objective: Find the address and value of maximum element # Input: $a0 = pointer to first, $a1 = pointer to last # Output: $v0 = pointer to max, $v1 = value of max ########################################################## max: move $v0, $a0 # max pointer = first pointer lw $v1, 0($v0) # $v1 = first value beq $a0, $a1, ret # if (first == last) return move $t0, $a0 # $t0 = array pointer loop: addi $t0, $t0, 4 # point to next array element lw $t1, 0($t0) # $t1 = value of A[i] ble $t1, $v1, skip # if (A[i] <= max) then skip move $v0, $t0 # found new maximum move $v1, $t1 skip: bne $t0, $a1, loop # loop back if more elements ret: jr $ra 4/4/2019 Dr. Hadi Hassan Computer Organization

26 Dr. Hadi Hassan Computer architecture
Register Spilling • The technique of saving register is called register spilling. • Register spilling can generate a lot of work. • To avoid that, MIPS offers two classes of registers. • $t0 — $t9: 10 temporary registers that are not preserved by the callee. • $s0 — $s7: 8 registers that must be saved by the callee. • This convention reduces register spilling. • In the above example, $t0 and $t1 need not be saved. • This reduces unnecessary work. 4/4/2019 Dr. Hadi Hassan Computer architecture

27 Famous Fibonacci Function
1 fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) 0, 1, 1, 2, 3, 5, 8, 13, 21,… int fib(n) { If (n==0) return (0) elseif (n==1) else return (fib(n-1)+fib(n-2)) } li $a0, # call fib(10) jal fib # move $s0, $v0 # $s0 = fib(10) fib is a recursive procedure with one argument $a0 need to store argument $a0, temporary register $s0 for intermediate results, and return address $ra 4/4/2019 Dr. Hadi Hassan Computer architecture

28 Dr. Hadi Hassan Computer Organization
Some observations about fib To summarize fib fib calls another function (itself), so we will have to save $ra. We need to save the argument n across the first recursive call. We need a temporary register for the result of the first call. A recursive function also acts as both caller and callee. Calling the same function guarantees that the same registers will be used, and overwritten if we’re not careful. So to be careful, we have to save every register that is used. int fib(int n ) { if (n <= 1) return n; Else return fib(n-1) + fib(n-2); } This is easy to convert into a C program 4/4/2019 Dr. Hadi Hassan Computer Organization

29 Dr. Hadi Hassan Computer Organization
The base case int fib(int n ) { if (n <= 1) return n; Else return fib(n-1) + fib(n-2); } ƒThe base case of the recursion is easy. If $a0 is less than 1, then we just return it. (We won’t worry about testing for valid inputs here.) ƒThis part of the code does not involve any function calls, so there’s no need to preserve any registers. fib: bgt $a0, 1,recurse move $v0, $a0 jr $ra 4/4/2019 Dr. Hadi Hassan Computer Organization

30 Dr. Hadi Hassan Computer Organization
recurse: addi $sp, $sp,-12 sw $ra, 0($sp) sw $a0, 4($sp) First save $ra and the argument $a0. An extra word is allocated on the stack to save the result of fib(n-1). The argument n is already in $a0, so we can decrement it and then “jal fib” to implement the fib(n-1) call. The result is put into the stack. addi $a0, $a0, -1 jal fib sw $v0, 8($sp) lw $a0, 4($sp) addi $a0, $a0, -2 jal fib Retrieve n, and then call fib(n-2). lw $v1, 8($sp) add $v0, $v0, $v1 The results are summed and put in $v0. lw $ra, 0($sp) addi $sp, $sp, 12 Jr $ra We only need to restore $ra before popping our frame and saying bye-bye. 4/4/2019 Dr. Hadi Hassan Computer Organization

31 Dr. Hadi Hassan Computer architecture
Complete Code fib: bgt $a0, 1, recurse move $v0, $a0 jr $ra recurse: addi $sp, $sp,-12 sw $ra, 0($sp) sw $a0, 4($sp) addi $a0, $a0, -1 jal fib sw $v0, 8($sp) lw $a0, 4($sp) addi $a0, $a0, -2 lw $v1, 8($sp) add $v0, $v0, $v1 lw $ra, 0($sp) addi $sp, $sp, 12 4/4/2019 Dr. Hadi Hassan Computer architecture

32 Dr. Hadi Hassan Computer architecture
Example Factorial Compute n! Recall that 0! = 1 1! = 1 n! = n(n-1)! Store on the stack $s0 = n, the parameter $ra, the return address Function to implement factorial: fact(n) { if n==1 then return 1 else return n*fact(n-1); } 4/4/2019 Dr. Hadi Hassan Computer architecture

33 Example of a Recursive Procedure
int fact(int n) { if (n<2) return 1; else return (n*fact(n-1)); } fact: slti $t0,$a0,2 # (n<2)? beq $t0,$0,else # if false branch to else li $v0,1 # $v0 = 1 jr $ra # return to caller else: addiu $sp,$sp,-8 # allocate 2 words on stack sw $a0,4($sp) # save argument n sw $ra,0($sp) # save return address addiu $a0,$a0,-1 # argument = n-1 jal fact # call fact(n-1) lw $a0,4($sp) # restore argument lw $ra,0($sp) # restore return address mul $v0,$a0,$v0 # $v0 = n*fact(n-1) addi $sp,$sp,8 # free stack frame 4/4/2019 Dr. Hadi Hassan Computer architecture


Download ppt "Computer Architecture Procedure Calls"

Similar presentations


Ads by Google