Share via


MIPS Prolog

9/7/2007

The MIPS prolog has several immediately contiguous parts, regardless of whether MIPS 16-bit mode, MIPS 32-bit mode, or MIPS 64-bit mode is in force.

The following steps show the required elements of a MIPS prolog.

The code examples shown apply to MIPSII. For MIPS IV, replace the sw instruction with sd, and replace the lw instruction with ld.

  1. Define the prolog and set up the entry.

    .ent <routine_name>
    <routine_name>:
    
  2. Reserve space for the stack frame.

    Addiu sp, -<frame size>
    

    An extended addiu instruction of four bytes is generated for frame sizes greater than 1024.
    If the frame size exceeds 32768, update the stack pointer with a constant from the literal pool using the following sequence of instructions.

    hw  $3, <frame size> constant offset(pc)
    move  $2, $29
    subu  $2, $2, $3
    move  $29, $2
    

    The size of the stack frame must be a multiple of eight. This includes space for local variables and temporaries, saved registers, and a procedure call argument area for non-leaf routines.
    Any routine that uses registers $16-$23, or $30, or any floating-point register that the called function saves, must save these registers.
    The procedure call argument area contains the maximum number of bytes required for the arguments of any procedure called in a non-leaf routine. This number of required bytes includes those arguments that the function can pass in registers, unless the N32 calling convention is in use.

  3. Set up a virtual frame pointer. The virtual frame pointer is sp($29) added to the frame size.

    .frame framereg (usually $29), framesize, returnreg (usually $31)
    
  4. Set a bit in the bitmask for each general-purpose register saved. Set bits in little endian order. The frame offset is the offset, a negative number, from the virtual frame pointer where the register save area begins.

    .mask mask, <frame offset>
    
  5. Store any registers that need to be saved.
    For example, if ra needs to be saved:

    sw ra, <frame size> + <frame offset>(sp)
    

    If subsequent lower number registers need to be saved, the offset depends on the MIPS mode. If the next register is a MIPS16 register,

    sw  $<mips16 register>, <frame size> + <frame offset> - n(sp)
    

    Otherwise

    move  $2, <mipsii register>
    sw  $2, <frame size> + <frame offset> - n(sp)
    

    Where N is four and is incremented by four for each subsequent lower number register to be saved.
    If the <frame size> + <frame offset> is greater than 32767, then the following 3 instruction sequence is required before executing the register save loop:

    lw  $2, <frame size> + <frame offset>constant offset(pc)
    move  $2, $29
    addu  $3, $2, $3
    

    The sw instructions depend on the mode of the MIPS register. If the next register is a MIPS16 register:

    sw  $<mips16 register>, -n($3)
    

    Otherwise

    move  $2,$<mipsii register>
    sw  $2, -n($3)
    
  6. Mark the end of the prolog.

    .prologue 0
    

See Also

Reference

MIPS Assembler Macros

Other Resources

MIPS Prolog and Epilog