Top Banner
Chapter 9 Procedures
37

Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Chapter 9

Procedures

Page 2: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Procedures

•Why use procedures?•?•?•?

•Microprocessors (and assembly languages) provide onlyminimal support for procedures•Must build a standard form for procedures

Page 3: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Procedures

•Procedure call{ . . x = larger (a, b); . .}

•Procedure header and parametersint larger (int one, int two)

•Procedure body{ if (one > two)

larger = one; else

larger = two;}

Page 4: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Procedures

• Steps in the execution of the procedure:1. Save return address2. Procedure call3. Execute procedure4. Return

• What is the return address?

• What is the procedure call?

• What is the return?

MAL uses registers rather than variables for return address

Page 5: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Procedures

Modern load/store machines (MAL, MIPS, SPARC,…) havea jump and link instruction for procedure calls

jal procname•Place the address of the instruction following it into theregister $ra ($31)

•$31 is an arbitrary (and common) choice made atarchitecture time.•Why isn’t $31 specified in the instruction as:

jal $31, procname #NOT VALID CODE

•Branches (jumps) to the address given by the label (procname).

Page 6: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Procedures

The example becomes:

jal proc1 # use of $ra is implied..jal proc1..

proc1: # 1st instruction of procedure here..jr $ra # $ra is the alias for $31

Page 7: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Nested Procedures

What happens if a procedure calls another procedure(nesting) using jal?

jal proc1..jal proc1.

proc1: .jal proc2.jr $ra

proc2: ..jr $ra

Page 8: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Nested Procedures

Even more exciting, what happens if a procedure callsitself (recursion)?

jal proc1..jal proc1..

Proc1: ..jal proc1..jr $ra

Page 9: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Nested Procedures

•Must save return address as generated•Unknown how many to save•Needed in reverse order of saved•Use a …

Page 10: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

System Stack

A stack is so frequently used for procedure call/return,that many computer systems predefine a system stack.•The system stack is for dynamic data (vs static, knownbefore runtime)

•Return addresses•Saving registers to move other data into register bank (“spilling”)•Local variables – several instances may be defined atonce with multiple calls•??

•The system stack is very large•In a multi-user environment, how many system stacksare there?

Page 11: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

System Stack

•The MIPS system stack (as with most machines) isdefined to grow toward smaller addresses•The stack pointer points to an empty location at the top of the stack•The stack pointer is register #29, also called $sp•It is defined before program execution begins•If $sp ($29) is used for any other purpose, then thestack pointer is lost.

•This would be bad, very bad.address

0yourprogramhere

very largeaddresses

systemstackhere

Grows towards smaller addresses

Page 12: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

System Stack

•push, in MAL:sw $?, ($sp) # the $? Is replaced by some registersub $sp, $sp, 4 # contains the data to be pushed

Orsub $sp, $sp, 4sw $?, 4($sp)

•pop, in MAL:

Or

•Which forms are better if there is an interrupt that usesthe same stack?

Page 13: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

System Stack

•Often a procedure pushes/pops many things.add $sp, $sp, -4sw $8, 4($sp)add $sp, $sp, -4sw $9, 4($sp)add $sp,$sp, -4sw $10, 4($sp)

•But we do not need to change $sp each time.add $sp, $sp, -12sw $8, 12($sp)sw $9, 8($sp)sw $10,4($sp)

•And with pop as well (do the add first or last?)

Page 14: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

System Stack: Saving Return Addresses

jal doit.jal doit.

doit: sub $sp, $sp, 4 #push return addresssw $ra, 4($sp)..jal another #this would overwrite the return. #address if it had not been saved.lw $ra, 4($sp) #pop return addressadd $sp, $sp, 4jr $ra

•Note how every pop has a push.•Never leave a procedure without popping everything thatwas pushed.•Always match up your pushes and pops.

Page 15: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Stack Frames

•The compiler will put many things on the stack for eachprocedure call

•?•?•?

•The combination of all these (different for each call) is called a stack frame or activation record (AR).•Space for a stack frame is made each time a procedureis called.•The space is removed each time a return occurs.•These stack frames are pushed/popped dynamically.

Page 16: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Stack Frame

What happens to the stack when:

Example:jal Ajal B..

A: jal Cjal Djr $ra

B: jal Djr $ra

C: jal Ejr $ra

D: jr $raE: jr $ra

Page 17: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Stack Frames

A: sub $sp, $sp, 20 # allocate frame for Asw $ra, 16($sp) # save A’s return address

jal Cjal D

lw $ra,16($sp) # restore A’s return addressadd $sp,$sp,20 #remove A’s frame from stackjr $ra # return from A

•The allocation and removal of a frame is done within thebody of the procedure.•The compiler does not need to know the size of a procedure’sframe.•Accesses to A’s frame are done via offsets from stack pointer.

Page 18: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Parameter Passing

Parameter = Argument•There is even less architectural support for parameterpassing•Create a convention•Follow the convention

•Follow the convention carefully•Follow the convention consistently•Never change the convention once defined

•Place data in a specific parameter location•Both the calling program and the procedure need toknow where the parameters are.•Procedure may place return values there.

Page 19: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Parameter Passing

Call by value (C, C++)•The parameter passed may not be modified by the procedure.•This can be implemented by passing a copy of the value.•The procedure can modify the value (copy) passed to it, but the value is not changed outside the scope of the procedure.

Call by reference (Fortran, C++ &, Pascal var)•The parameter passed to the subroutine can be modified.•The modification is seen outside the scope of the subroutine.•Similar to having access to global variable.

Ways of implementing these 2 variable types?• ?• ?

Page 20: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Parameter Passing

Simplest mechanism – registers•The calling program puts the parameter(s) into specificregisters, and the procedure uses them.

.

.move $s4, $s2 # put parameter in register 4jal decrementmove $s2, $s4 # recopy parameter to its correct place..

decrement:add $s4, $s4, -1jr $ra

•This is a trivial example•Why not just use $s2 within the procedure?

Page 21: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Parameters on Stack

•When there aren’t enough registers for all parameters,use the activation record (AR).

•Used for all parameters in machines with few registers.•eg. Hc11, 6502, 8086, …

sub $sp, $sp, 8 # allocate space for parameterssw $9, 8($sp) # place parameter 1 into AR of procsw $18, 4($sp) # place parameter 2 into AR of procjal proc.

proc:sub $sp, $sp, 12 # allocate remainder of AR for proc

# assume fixed size (too big) activation recordlw $10, 20($sp) # retrieve parameter 1lw $11, 16($sp) # retrieve parameter 2

# use parameters in procedure calculationsadd $sp, $sp, 20 # remove AR of procjr $ra

Page 22: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Parameters on a stack

•Calling program•Allocate space for parameters•Places parameters into stack•Calls procedure

•Procedure:•Allocates AR (or remainder of AR)•De-allocates AR of procedure (or at least most of it)

•MIPS convention•The first 4 parameters are passed in register•The alias for $4-$7 is $a0-$a3.•The first parameter is passed in $a0.•Space for all parameters passed in $a0-$a3 is allocatedin the procedure’s AR.

Page 23: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

MIPS Convention

If there are nested subroutine calls, and registers $a0-$a3are used for parameters, the values would be lost

procA: #receives 3 parameters in $a0, $a1, $a2# set up procB’s parametersmove $a0, $24 # overwrites procA’s parameter in $a0move $a1, $9 # overwrites procA’s parameter in $a1jal procB # the nested procedure call

# procA continues after procB returns# procA’s parameters are needed, but have been overwritten

Solutions…

Page 24: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

MIPS Convention

•Caller•Place parameters in $a0 to $a3•jal procedure

•Procedure•Allocate remainder of AR and push $ra•Procedure calculations•To call proc2

•Place current parameters (from $a0-$a3) into AR•Set up parameters to proc2 in $a0-$a3•Call proc2 (jal proc2)•Copy any return values out of $v0-v1, $a0-$a3•Restore current parameters from AR back to $a0-$a3

•More procedure calculations•Get procedure’s return address from AR•De-allocate AR•Return (jr $ra)

Page 25: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

MIPS Convention

# a set of procedures that do the following:# if a<b, then switch a with b and decrement both

# a is in register 20# b is in register 21

.textsub $8, $20, $21bgtz $8, othercodemove $a0, $20 # place parameters in a registersmove $a1, $21jal s_and_dmove $20, $a0 # copy the return valuesmove $21, $a1

othercode:done

Page 26: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

MIPS Convention

# s_and_d: swap its 2 parameters, and then both decrement both# $a0 ($4) – first parameter# $a1 ($5) – second parameter# $8 – temporary for switchings_and_d: sub $sp, $sp, 20 # allocate frame for switch

sw $ra, 20($sp) # save return address on stack move $t0, $a0 # switch the 2 parameters move $a0, $a1 # $t0 is alias for $8 move $a1, $t0 sw $a0, 16($sp) # save current parameters sw $a1, 12($sp) jal decrement # the parameter to decrement is already

# in $a0 lw $a0, 12($sp) # set up parameter in $a0 jal decrement move $a1, $a0 # copy return value lw $a0, 16($sp) # restore current parameter lw $ra, 20($sp) # get return address jr $ra

# procedure decrement: subtracts 1 from parameter $a0decrement: addi $a0, $a0, -1

jr $ra

Page 27: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Summary: Parameter Passing Styles

1. Use registers• Advantages• Disadvantages

2. Use some registers, and place the rest on the stack• Advantages• Disadvantages

3. Put all parameters on the stack (an unsophisticatedcompiler might do this)• Advantages• Disadvantages

4. Put parameters in memory set aside for them• Advantages• Disadvantages

Page 28: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Frame Pointers

•During a procedure call, the stack can grow and shrink•Why?

•Example:•At one point parameter 2 might be at 16($sp)•At another point within the same procedure, parameter2 might be at 24($sp)

param2

$sp

procedure’sframe

base of frame

temp2temp1

param2

$sp

procedure’sframe

base of frame

Page 29: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Frame Pointers

temp2temp1

param2

$sp

procedure’sframe

frame pointer

•A Frame Pointer points to the base of the AR•Parameter, etc. locations are constant wrt the frame

•Parameter 2 will be at…

Page 30: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Frame Pointers

•A new register is needed for the FP•The FP must be initialized at the start and restored at theend of every procedure

•By the compiler or assembly-code writer•On the MIPS, all data with a stack frame is accessed viathe stack pointer, $sp.•Why?

Page 31: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Frame Pointers

# the frame (AR) is 4 words: 2 words for 2 parameters# passed in $a0 and $a1, 1 for ra, and 1 for FPproc: sub $sp, $sp, 8 # allocate remainder of frame

# assumes that caller allocates space# for the 2 parameters

sw $ra, 8($sp) #save procedure’s return addresssw $16, 4($sp) # save caller’s frame pointeradd $16, $sp, 16 # set procedure’s frame pointer

## procedure’s code in herelw $ra, -8($16) #restore return addressmove $8, $16 # save frame pointer temporarily lw $16, -12($16) # restore callers frame pointermove $sp, $8 # remove procedure’s frame (AR)jr $ra

frame pointerreturn addressspace for P2space for P1

$sp

$16 (frame pointer)

Page 32: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Register Spilling

•On calling a procedure, many registers may be in use What should be done?

Two solutions•Callee saved

•A procedure clears out some registers for its own use•Register values are preserved across procedure calls•MIPS calls these saved registers: $s0-$s8 (aliases for$16-$23, $30)•The called procedure

•Saves register values in its AR,•Uses the register for local variables,•Restores register values before it returns.

Page 33: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Register Spilling

•Caller Saved•The calling program saves the registers that it doesnot want a called procedure to overwrite•Register values are NOT preserved across procedurecalls•MIPS calls these temporary registers: $t0-$t9(aliases for $8-$15, $24-$25)•Procedures use there registers for local variables•The values do not need to be preserved outside thescope of the procedure.•How about $v0-$v1, and $a0-$a3?

Page 34: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Procedure Calls

From the compiler’s point of view, a procedure call lookslike:

call setupprocedure callreturn cleanup...

procedure:prologue

calculations

epilogue

Page 35: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Procedure Calls

The full convention includes

Call Setup•Save any callee-save registers that are currently in use•Place current parameters into current stack frame•Allocate space for all parameters for procedure to becalled

•Change $sp by the total parameter size•Place first 4 parameters to procedure into $a0-$a3•Place remaining parameters on stack

Procedure Call•JAL

Page 36: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Procedure Calls

Prologue•Allocate space for remainder of stack frame•Save return address in stack frame•Copy needed parameters from stack frame into registers•Save any needed saved registers into current stack frame

Epilogue•Restore (copy) return address from stack frame into $ra•Restore any saved registers•De-allocate stack frame (or most of it)

•Move $sp so the space for the procedure’s frame is gone

Return Cleanup•Copy needed return values and parameters from $v0-$v1,$a0-$a3, or stack frame to correct places•De-allocate remainder of procedure’s stack frame

•Move $sp so the space for the procedure’s frame is gone•Restore any saved registers from call setup

Page 37: Chapter 9 Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard.

Summary: Procedure Calls

•Minimal ASM support•Need formal and consistent mechanism

•Why?•Activation record includes

•?•?•?•?•?

•Caller must…•Callee must…