CS170 Computer Organization and Architecture I Ayman Abdel-Hamid

CS170 Computer Organization and Architecture I Ayman Abdel-Hamid

CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 18: 11/5/2002 Lecture 18: 11/5/2002 CS170 Fall 2002 1 Outline Procedure calls (section 3.6) For more information see section A.6 Lecture 18: 11/5/2002 CS170 Fall 2002 2 Procedures Procedure/Subroutine A set of instructions stored in memory which perform a set of operations based on the values of parameters passed to it and returns one or more values

Steps for execution of a procedure or subroutine The program (caller) places parameters in places where the procedure (callee) can access them The program transfers control to the procedure The procedure gets storage needed to carry out the task The procedure carries out the task, generating values The procedure (callee) places values in places where the program (caller) can access them The procedure transfers control to the program (caller) Lecture 18: 11/5/2002 CS170 Fall 2002 3 Procedure calling/return Studies of programs show that a large portions of procedures have a few parameters passed to them and return a very few, often one value to the caller Parameter values can be passed in registers MIPS allocates various registers to facilitate use of procedures $a0-$a3 four argument registers in which to pass parameters

$v0-$v1 two value registers in which to return values $ra one return address register to return to point of origin jump-and-link instruction jal ProcedureAddress Jump to an address and simultaneously save the address of the following instruction in register $ra (What is the address of the following instruction?) jal is a J-format instruction, with 26 bits relative word address. Pseudodirect addressing applies in this case. Lecture 18: 11/5/2002 CS170 Fall 2002 4 Procedure calling/return How to do the return jump? Use a jr instruction

jr $ra Refined MIPS steps for execution of a procedure Caller puts parameter values in $a0-$a3 Caller uses a jal X to jump to procedure X (callee) Callee performs calculations Callee place results in $v0-$v1 Callee returns control to the caller using jr $ra Lecture 18: 11/5/2002 CS170 Fall 2002 5 More Registers?? What happens when the compiler needs more registers than 4 argument and 2 return value registers? Can we use $t0-$t7, $s0-$s7 in callee or does caller need values in these registers?? $t0-$t9: 10 temporary registers that are not preserved by the callee on a procedure call $s0-$s7: 8 saved registers that must be preserved on a procedure call if used Any registers needed by the caller must be restored to the values they contained before the procedure was invoked How? Spill registers to memory

use the registers in callee restore contents from memory We need a stack (LIFO data structure) (Why?) Placing data onto stack push Removing data from stack pop Lecture 18: 11/5/2002 CS170 Fall 2002 6 Stack and Stack Pointer A pointer is needed to the stack top , to know where the next procedure should place the registers to be spilled or where old register values can be found (stack pointer) $sp is the stack pointer Stacks grow from higher addresses to lower addresses What does a push/pop means in terms of operations on the stack pointer (+/-)? Higher address

$sp Higher address Contents of register X $sp Lower address Lower address After push of contents of register X Lecture 18: 11/5/2002 CS170 Fall 2002 7 Simple Example 1/2 int leaf_example (int g, int h, int i, int j) { int f; f = (g+h) (i+j);

return f; } Leaf_example: #procedure label subi $sp,$sp,4 #make room for 1 item sw $s0, 0 ($sp) #store register $s0 for use later add $t0, $a2, $a1 # $t0 g+h What is the generated MIPS assembly code? add $t1,$a2,$a3 # $t1 i+j sub $s0,$t0,$t1

#f $t0-$t1 Local variable f corresponds to $s0. Hence, we need to save $s0 before actually using it for local variable f (maybe caller needs it) add $v0,$s0,$zero # set up return value in $v0 lw $s0, 0($sp) # restore register $s0 for caller Return value will be in $v0 addi $sp,$sp,4 #adjust stack to delete 1 item Textbook assumes that $t0, $t1 need to be saved for caller (page 135) jr $ra

#jump back to caller g,h, i, and j correspond to $a0 through $a3 Lecture 18: 11/5/2002 CS170 Fall 2002 8 Simple Example According to textbook page 134 and 135 Lecture 18: 11/5/2002 2/2 subi $sp,$sp,12 # adjust stack to make room for 3 items

sw $t1, 8($sp) # save register $t1 for later use sw $t0 ,4($sp) # save register $t0 for later use sw $s0,0($sp) # save register $s0 for later use CS170 Fall 2002 9 Real Picture: It is not that Simple 1/2 How about if a procedure invokes another procedure? main calls procedure A with one argument A calls procedure B with one argument If precautions not taken $a0 would be overwritten when B is called and value of parameter passed to A would be lost When B is called using a jal instruction, $ra is overwritten

How about if caller needs the values in temporary registers $t0-$t9? More than 4 arguments? Local variables that do not fit in registers defined in procedures? (such as?) We need to store the register contents and allocate the local variables somewhere? We already saw a solution when we saved $s0 before using it in the previous example Lecture 18: 11/5/2002 CS170 Fall 2002 10 Real Picture: It is not that Simple 2/2 Solution Use segment of stack to save register contents and hold local variables (procedure frame or activation record) If $sp changes during procedure execution, that means that accessing a local variable in memory might use different offsets depending on their position in the procedure Some MIPS software uses a frame pointer $fp to point to first word procedure frame

$fp provides a stable base register within a procedure for local memory references $sp points to the top of the stack, or the last word in the current procedure frame An activation record appears on the stack even if $fp is not used. Lecture 18: 11/5/2002 CS170 Fall 2002 11 Procedure Call details 1/3 Caller Passes arguments The first 4 in registers $a0-$a3 The remainder of arguments in the stack (push onto stack) Load other arguments into memory in the frame $sp points to last argument Save the caller-saved registers ($a0-$a3 and $t0-$t9) if and only if the caller needs the contents intact after call return Execute a jal instruction which saves the return address in $ra and jumps to the procedure

Lecture 18: 11/5/2002 CS170 Fall 2002 12 Procedure Call details 2/3 Callee Allocates memory on the stack for its frame by subtracting the frames size from the stack pointer ($sp $sp frame size) Save callee-saved registers in the frame ($s0-$s7, $fp, and $ra) before altering them since the caller expects to find these registers unchanged after the call $fp is saved by every procedure that allocates a new stack frame (we will not worry about this issue in our examples) $ra only needs to be saved if the callee itself makes a call Establish its frame pointer (we will not worry about this issue in our examples) The callee ends by Return the value if a function in $v0 Restore all callee-saved registers that were saved upon procedure entry Pop the stack frame by adding the frame size to $sp Return by jumping to the address in register $ra (jr $ra) Lecture 18: 11/5/2002

CS170 Fall 2002 13 Procedure Call details 3/3 Figure 3.12 page 139 Lecture 18: 11/5/2002 CS170 Fall 2002 14

Recently Viewed Presentations

  • INTRODUCTION TO CLASS STARTERS - pdsd.org

    INTRODUCTION TO CLASS STARTERS - pdsd.org

    LT9 - Antonym. Definition: A word that is the opposite of another word. Example: Auggie's. emotions were . up and down, his mother thought he was . beautiful, he . thought he . was . hideous. Jack was a friend...
  • Book Club Planner First, look at your book,

    Book Club Planner First, look at your book,

    You will need have the major events that show the problem, leading to the turning point or the largest action of the text, which then rushes to the conclusion and solution. Use the mountain to help you climb through your...
  • Parent/Child Incorporated (PCI) of San Antonio & Bexar

    Parent/Child Incorporated (PCI) of San Antonio & Bexar

    Our heroes have always relied on their families for strength and support. Serving alongside our men and women in uniform are spouses, siblings, parents, and children who personify the ideals of patriotism, pride, resilience, service above self, and honor.
  • Summary of the NSF/IARPA/NSA Workshop on the Science of Security

    Summary of the NSF/IARPA/NSA Workshop on the Science of Security

    Identify scientific questions regarding computer security. Stimulate new work toward defining and answering those questions. Encourage work that goes beyond ad hoc point solutions and informal security arguments to discover foundational scientific laws for reasoning about and designing secure computing...
  • On Your Map - PCMS

    On Your Map - PCMS

    On Your Map - Refer to Page 39 in the Textbook. Cut Map to fit on page 11 of IN. Outline, c. olor in and label the 4 settlements. Four settlements should include: New . Spain, New France, New England,...
  • Series & Parallel Circuits & Circuit Symbols

    Series & Parallel Circuits & Circuit Symbols

    3: A 9V battery is connected in parallel to 2 bulbs: 4W, & 2W. A) Sketch the diagram with the proper symbols. Show real and conventional current flow direction. B) Sketch a voltmeter reading the voltage at the 2 W...
  • OHIO STATE UNIVERSITY EXTENSION Take Charge of Your

    OHIO STATE UNIVERSITY EXTENSION Take Charge of Your

    The Burden of Diabetes in Ohio. Diabetes is the fifth leading cause of death in Ohio. 1 million Ohioans now suffer from diabetes…⅓ do not know they have diabetes.
  • The Detrimental Impact of Chronic Renal Insufficiency

    The Detrimental Impact of Chronic Renal Insufficiency

    Recovery of aggregation was delayed after abciximab, but rapid after tirofiban and eptifibatide. These results were confirmed by conventional platelet aggregometry. Thus, tirofiban, abciximab, and eptifibatide, at currently recommended doses, achieved an adequate inhibition of platelet aggregation.