The calling convention
- A calling convention defines how subroutines are called.
- For mips this includes:
- How registers are used, and when they must be preserved
- Where and how parameters are passed
- How the stack is managed.
- Stack Management:
- See the back of the green sheet.
- An activation record or frame
- The frame contains all storage a subroutine requires
- Variable storage (local variables)
- Register preservation
- parameters.
- The called routine must manage the stack
- The end of the frame is pointed to by the stack pointer.
- The stack pointer is adjusted as needed by the routine. (More on this later)
- On exit
- The beginning of the frame is pointed to by the frame pointer
- Upon entry:
- sw $fp, -4($sp)
- subiu $fp, $sp, 4
- Upon Exit:
- For a non-leaf routine, the $ra must be preserved
- Other registers which might be saved
- Look at the register information on the front of the green sheet.
- Preserved across a call refers to who is responsible for saving the registers.
- As we have seen, $sp, $fp, and $ra must be preserved, or saved and restored by the called routine.
- We only need to do this if we change the values of a register.
- So if you want to use $s0, you must:
- Save it on the stack.
- use it
- Restore it when you are finished with the routine.
- The opposite is true of the registers that are not preserved across a call
- If you use $t0, and make a routine call
- The routine might also use $t0, and it is not the responsibility of the routine to preserve this register.
- Therefore if you want $t0 preserved across a call you must
- Save $t0 on the stack
- Make the routine call
- restore $t0
- Parameters:
- Registers $a0 through $a4 are used for the first four parameters
- Additional parameters are placed in the last portion of the calling routine's stack in reverse order (back of green sheet)
- Return values are stored in $v0 and $v1
- Here is a complete example