next up previous
Next: Summary Up: Procedures Previous: Recursion

Stack Frame

The system stack provides a convenient mechanism for dynamically allocating storage for the various data associated with the execution of a procedure including:

Space is allocated on the stack when a procedure is called and is removed upon return from the procedure. The block of information stored on the stack to effect a procedure call and return is called the stack frame. In general, the stack frame for a procedure contains all necessary information to save and restore the state of a procedure.

Strictly speaking, it is only necessary for the calling program and the called procedure to agree on the structure of the stack frame for each procedure call. However, the specification of a calling convention facilitates the use of procedure libraries by defining the structure of the stack frame uniformly for all procedure calls.

Compilers which follow the calling convention generate code that will work correctly with procedures written in any high-level language.

The calling convention used in the MIPS architecture employs the stack frame shown below:

tabular236

The frame pointer is stored in register $30, also called $fp. A stack frame consists of the memory on the stack between the frame pointer and the stack pointer.

Under the calling convention, the following steps are necessary to call a procedure:

  1. Pass the arguments. The first four arguments are passed in registers $a0-$a3 (simpler compilers may choose to ignore this convention and pass all arguments via the stack). The remaining arguments are pushed on the stack.
  2. Save the caller-saved registers. This includes registers $t0-$t9, if they contain live values at the call site.
  3. Execute a jal instruction.

Within the called routine, the following steps are necessary:

  1. Establish the stack frame by subtracting the frame size from the stack pointer.
  2. Save the callee-saved registers in the frame. Register $fp is always saved. Register $ra and registers $a0-$a3 need to be saved if they are in use and the routine itself makes calls. Any of the registers $s0- $s7 that are used by the callee need to be saved.
  3. Establish the frame pointer by adding the stack frame size to the address in $sp.

To return from a call, a function places the returned value into $v0 and executes the following steps:

  1. Restore any callee-saved registers that were saved upon entry.
  2. Pop the stack frame by subtracting the frame size from $sp.
  3. Return by jumping to the address in register $ra.


next up previous
Next: Summary Up: Procedures Previous: Recursion

CS301 Class Account
Mon Nov 25 11:40:41 AST 1996