Computer Architecture Procedure Calls

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
Lecture 9: MIPS Instruction Set
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
MIPS Assembly Language
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
MIPS Functions and the Runtime Stack
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Prof. Hsien-Hsin Sean Lee
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 5: Procedure Calls
Logical and Decision Operations
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
MIPS Assembly Language Programming
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
MIPS function continued
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

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

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

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

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

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

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

Arguments and return values Procedure use # Pseudocode: # c = sumOfSquares(3,5) # Registers: c => $t2 li $a0, 3 # (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

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

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

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

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, 2 # calculate offset to end of array add $t0, $a0, $t0 # calculate stop address lw $v0, 0($a0) # initial max value is 0-th element addi $a0, $a0, 4 # 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, $t1 # reset max noNewMax: j fmaxLoop # return to loop test fmaxDone: jr $ra # return to caller 4/4/2019 Dr. Hadi Hassan Computer architecture

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

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

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

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

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

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

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

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

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,-12 #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

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, 12 #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

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

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

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

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

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

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, 10 # 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

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

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

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

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

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

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