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:
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:
Within the called routine, the following steps are necessary:
To return from a call, a function places the returned value into $v0 and executes the following steps: