+ All Categories
Home > Documents > Procedures in more detail

Procedures in more detail

Date post: 02-Jan-2016
Category:
Upload: callum-brooks
View: 69 times
Download: 0 times
Share this document with a friend
Description:
Procedures in more detail. Procedures. Why use procedures? Code reuse More readable code Less code Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for procedures. Procedures. Procedure call { . . x = larger (a, b); - PowerPoint PPT Presentation
34
Procedures in more detail
Transcript
Page 1: Procedures in more detail

Proceduresin more detail

Page 2: Procedures in more detail

2CMPE12c Gabriel Hugh Elkaim

• Why use procedures?– Code reuse– More readable code– Less code

• Microprocessors (and assembly languages) provide only minimal support for procedures

• Must build a standard form for procedures

Procedures

Page 3: Procedures in more detail

3CMPE12c Gabriel Hugh Elkaim

Procedures

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

Procedure header and parametersint larger (int x, int y)

Procedure body{ if (x > y)

bigger = x; else

bigger = y; return (bigger);}

Page 4: Procedures in more detail

4CMPE12c Gabriel Hugh Elkaim

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: Procedures in more detail

5CMPE12c Gabriel Hugh Elkaim

• Modern load/store architectures (MIPS, Sparc) have a Jump and Link instruction:

jal procedure_name

• Places the address of the instruction following the jal call into $RA ($31)– $31 is an arbitrary (and common) choice

made at architecture time– Why isn’t the register used within the

instruction call?jal $31, procedure_name #INVALID

CODE

• Branches (jumps) to the address given by the label (procedure_name)

Procedures

Page 6: Procedures in more detail

6CMPE12c Gabriel Hugh Elkaim

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: Procedures in more detail

7CMPE12c Gabriel Hugh Elkaim

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: Procedures in more detail

8CMPE12c Gabriel Hugh Elkaim

Nested Procedures

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

jal proc1..jal proc1..

proc1: ..jal proc1..jr $ra

Page 9: Procedures in more detail

9CMPE12c Gabriel Hugh Elkaim

Nested Procedures

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

Page 10: Procedures in more detail

10CMPE12c Gabriel Hugh Elkaim

System Stack

A stack is so frequently used for procedure call/return, that many computer systems predefine a system stack.

Page 11: Procedures in more detail

11CMPE12c Gabriel Hugh Elkaim

• The system stack is for dynamic data (vs static, known before runtime)– Return addresses– Saving registers to move other data into

register bank (“spilling”)– Local variables – several instances may be

defined at once with multiple calls– Dynamic memory allocation

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

stacks are there?

System Stack

Page 12: Procedures in more detail

12CMPE12c Gabriel Hugh Elkaim

• The MIPS system stack is defined to grow towards a smaller address– very common implementation

• The stack pointer points to an empty location at the top of the stack

• The stack pointer is $sp ($29)• It is defined and initialized before

program execution begins• If $sp ($29) is used for any other

purpose, the stack is pointer is lost.– This would be bad, very bad!!

System Stack

Page 13: Procedures in more detail

13CMPE12c Gabriel Hugh Elkaim

Smaller address your

programhere

Largeraddress

systemstackhere

Grows towards smaller addresses

System Stack

Memory

Page 14: Procedures in more detail

14CMPE12c Gabriel Hugh Elkaim

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:add $sp, $sp, 4lw $?, ($sp)

Orlw $?, 4($sp)add $sp, $sp, 4

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

Page 15: Procedures in more detail

15CMPE12c Gabriel Hugh Elkaim

System Stack

Often a procedure pushes/pops many things.add $sp, $sp, -4sw $s2, 4($sp)add $sp, $sp, -4sw $s3, 4($sp)add $sp, $sp, -4sw $s4, 4($sp)

But we do not need to change $sp each time.add $sp, $sp, -12sw $s2, 12($sp)sw $s3, 8($sp)sw $s4, 4($sp)

Can do this for the pop as well

Page 16: Procedures in more detail

16CMPE12c Gabriel Hugh Elkaim

System Stack: Saving Return Addresses

jal doit...jal doit...

doit: sub $sp, $sp, 4 #push return addresssw $ra, 4($sp)......jal another #overwrites $ra ...lw $ra, 4($sp) #pop return addressadd $sp, $sp, 4jr $ra

Page 17: Procedures in more detail

17CMPE12c Gabriel Hugh Elkaim

• Note how every pop has a push.• Never leave a procedure without popping

everything that was pushed.• Always match up your pushes and pops.

System Stack: Saving Return Addresses

Page 18: Procedures in more detail

18CMPE12c Gabriel Hugh Elkaim

Parameter Passing

Parameter = Argument

• There is even less architectural support for parameter passing

• Need to create a convention

Page 19: Procedures in more detail

19CMPE12c Gabriel Hugh Elkaim

• 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 to know where the parameters are.• Procedure may place return values there.

Parameter Passing

Page 20: Procedures in more detail

20CMPE12c Gabriel Hugh Elkaim

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

Page 21: Procedures in more detail

21CMPE12c Gabriel Hugh Elkaim

Parameter Passing

Simplest method: Use registers•The calling program puts the parameter(s) into specific registers, and the procedure uses them.

.

.move $s4, $s2 # put parameter in

register $s4jal decrementmove $s2, $s4 # copy parameter back to

its# correct place

.

.decrement:

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

•Why not just use $s2 within the procedure?

Page 22: Procedures in more detail

22CMPE12c Gabriel Hugh Elkaim

Another method: Use system Stack

When there aren’t enough registers for all parameters, use the system stack in something called an activation record (AR).

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

Parameter Passing

Page 23: Procedures in more detail

23CMPE12c Gabriel Hugh Elkaim

sub $sp, $sp, 8 # allocate space for parameterssw $s2, 8($sp) # place parameter 1 into AR of procsw $s3, 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 record

lw $t0, 20($sp) # retrieve parameter 1lw $t1, 16($sp) # retrieve parameter 2

# use parameters in procedure calculations

add $sp, $sp, 20 # remove AR of procjr $ra

Parameters on system stack

Page 24: Procedures in more detail

24CMPE12c Gabriel Hugh Elkaim

Parameters on system 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 allocated in the procedure’s AR.

Page 25: Procedures in more detail

25CMPE12c Gabriel Hugh Elkaim

MIPS Convention

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

procA: #receives 3 parameters in $a0, $a1, $a2…# set up procB’s parametersmove $a0, $t0 # overwrites procA’s parameter in

$a0move $a1, $t1 # overwrites procA’s parameter in

$a1jal procB # the nested procedure call

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

been# overwritten

Solutions…

Page 26: Procedures in more detail

26CMPE12c Gabriel Hugh Elkaim

MIPS Convention

Need to expand on the MIPS convention to now preserve the argument registers also

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

Procedure•Allocate remainder of AR and push $ra•Procedure calculations

Page 27: Procedures in more detail

27CMPE12c Gabriel Hugh Elkaim

• Place current parameters ($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

MIPS Convention

Procedure continued…• To call proc2

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

Page 28: Procedures in more detail

28CMPE12c Gabriel Hugh Elkaim

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 29: Procedures in more detail

29CMPE12c Gabriel Hugh Elkaim

Register SpillingWhen in a procedure may need to use registers that are already being used, what to do?

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 30: Procedures in more detail

30CMPE12c Gabriel Hugh Elkaim

Register Spilling

Caller Saved•The calling program saves the registers that it doesnot want a called procedure to overwrite•These register values are NOT preserved across procedure calls•MIPS calls these temporary registers: $t0-$t9(aliases for $8-$15, $24-$25)•Procedures use these 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 31: Procedures in more detail

31CMPE12c Gabriel Hugh Elkaim

Procedure Calls

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

call setupprocedure callreturn cleanup.procedure:prologue.calculations.epilogue

Page 32: Procedures in more detail

32CMPE12c Gabriel Hugh Elkaim

MIPS Procedure Calls

The full MIPS 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 33: Procedures in more detail

33CMPE12c Gabriel Hugh Elkaim

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 save 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

MIPS Procedure Calls

Page 34: Procedures in more detail

34CMPE12c Gabriel Hugh Elkaim

Summary: Procedure Calls•Minimal ASM support•Need formal and consistent mechanism

•Why?•Activation record includes

•Return addresses•parameters•Save registers•Saved arguments•…

•Caller must…•Callee must…


Recommended