Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 210 Computer Organization

Similar presentations


Presentation on theme: "Computer Science 210 Computer Organization"— Presentation transcript:

1 Computer Science 210 Computer Organization
Introduction to MIPS Assembly Language Subroutines

2 MIPS Memory Allocation
Stack grows down Register $sp points to top of stack Push decreases $sp Pop increases $sp pc (program counter) is register that keeps up with current location in the program Note: The stack is in memory; so stack operations are slow.

3 Subroutines Subroutines in assembly language provide the low level mechanism for high level functions, procedures, subroutines, methods, etc.

4 Important Assumption Unless stated otherwise, we assume that our subroutines are completely independent of the calling program or subroutine The subroutine knows nothing about labeled data of the caller – only receives data via appropriate registers or system stack. Subroutine can have no labeled data (doesn’t know labels used by caller)

5 Issues with Subroutines
We must learn How to call a subroutine How do we know the return location How to pass parameters to subroutine How to deal with local variables of subroutine How to protect register values of caller How to return values to caller How to protect caller’s stack values How to return to caller

6 1) Calling the Subroutine 2) Where to return
To call a subroutine, we use the jal (jump and link) instruction. This is an unconditional branch instruction jal label The label is that of the subroutine This instruction Puts 4($pc) into register $ra (return address) so we’ll know where to return. Puts the address of the label in $pc.

7 3) Passing parameters to subroutine
To pass up to 4 parameters by value, the caller uses registers $a0 - $a3 to hold the values. To pass up to 4 parameters by reference, use these same registers for the addresses. If we need to pass more parameters, say 6, we could use these registers for 4 parameters and put the other two on the stack. Of course, the specifics must be agreed upon between callers and the callee. Arrays are usually passed by reference – pass the base address and length of array.

8 4) Local variables The basic way to handle the need for local variables is to Use registers if possible Use the stack otherwise

9 5) Saved registers – One MIPS convention
It is the callee’s responsibility to preserve the values in $s0 - $s7. So if the subroutine uses these registers, their original values should be placed on stack and then restored before returning. It is the caller’s responsibility to preserve the values in $t0 - $t7. So if there are needed values in these registers, the caller should put these on stack before calling the subroutine and then restore them after the return.

10 5) Saved registers – Other conventions
Caller Saves Convention: It is the caller’s responsibility to preserve the values in $s0 - $s7 and $t0 - $t7 before calling and then restore after the call. Callee Saves Convention: It is the callee’s responsibility to preserve the values in $s0 - $s7 and $t0 - $t7 for any of the registers that it uses. So if there are needed values in these registers, the caller should put these on stack before calling the subroutine and then restore them after the return. Unless stated otherwise,we will use Callee Saves.

11 6) Returning values Registers $v0 and $v1 are used for returning values from a subroutine. If more values need to be returned, these could be on the stack. Of course, the caller would need to get them from the stack.

12 7) Preserving stack Often the caller will have valuable data on the stack. Therefore, the callee must be sure to return to the caller with the stack as before the call, possibly with return values on top of the stack. The special register $fp (frame pointer) is used to hold the value of the stack pointer when the subroutine first gets control move $fp, $sp Before returning, move $sp, $fp We refer to the section of stack memory used by the subroutine as a frame. If the frame is of fixed size, we may not need $fp.

13 8) Returning After doing the appropriate housekeeping, jr $ra
since $ra holds the appropriate return address.

14 What if subroutine calls a subroutine
Frames will get stacked on frames – that’s ok. One problem is that when a subroutine1 calls a subroutine2, $ra gets the return address back into subroutine1. The return address that subroutine1 will need when it returns will be lost. So, subroutine1 must keep it in a saved register or on the stack.

15 Subroutine Example

16 Subroutine Example

17 Subroutine Example


Download ppt "Computer Science 210 Computer Organization"

Similar presentations


Ads by Google