Share via


MIPS16 Prolog Example

The following example shows how to construct a prolog for a MIPS16 ISA.

  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 will be generated for frame sizes greater than 1024. You can accommodate frame sizes up to 32768 bytes using the addiu instruction. In the unlikely event that the frame size exceeds 32768, update the stack pointer with a constant from the literal pool. This requires a sequence of four 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, if needed. Any routine that uses registers $16-$23, or $30, or any floating-point register saved by the called function, must save these registers. The procedure call argument area is set up so that it contains the maximum number of bytes required for the arguments of any procedure called in a non-leaf routine, including those that can be passed in registers.

  3. Set up a virtual frame pointer. The virtual frame pointer is the $29 register, sp, added to the frame size. This is the framereg added to the framepointer.

    .frame framereg (usually $29), framesize, returnreg (usually $31)
    
  4. The mask pseudo instruction marks the beginning of the register save area. A bit on in the bitmask should appear in Little Endian order for each general-purpose register saved. 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 <frame size> + <frame offset> greater than 1020, an extended sw instruction is generated. In the unlikely event that 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
    

    In this case, 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
    

The minimum proper prolog for a leaf routine includes the following segments:

  • .ent
  • entry label
  • .frame
  • .prologue 0.

For a non-leaf routine, the minimum proper prolog includes the following segments:

  • .ent
  • entry label
  • .frame
  • .prologue 0
  • Save of the return address register, ra($31).

See Also

MIPS Prolog | MIPSII Prolog Example

 Last updated on Thursday, April 08, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.