SH-4 Prolog and Epilog Examples

The following examples show how to use prolog and epilog to perform certain tasks.

  • Allocate a stack frame to save R14, R8, and PR

    This example allocates a stack frame to save R14, R8, and PR, and to allow alloca() calls. It also allocates a 4-word argument build area. Local variables and temporaries do not need stack space.

    NESTED_ENTRY Function
    
    mov.l R14, @-R15 // Save old frame pointer.
    mov.l R8, @-R15 // Save a permanent register.
    sts.l PR, @-R15 // Save return address.
    mov  R15, R14  // Set up new frame pointer.
    add  #12, R14
    add  #-16, R15  // Allocate argument save area
    
    PROLOG_END
    
    // Routine body
    
    add  #-12, R14  // Find base of RSA.
    mov  R14, R15
    lds.l @R15+, PR  // Restore return address.
    mov.l @R15+, R8  // Restore R8.
    rts        // Return.
     mov.l @R15+, R14 // Restore R14.
    
    ENTRY_END Function
    
  • Allocate a stack frame for a leaf routine

    This example allocates a stack frame for a leaf routine that requires 40 bytes for local variables and temporaries. It uses permanent registers R8, R9, and R10. This routine has no alloca locals, so no frame pointer is required.

    NESTED_ENTRY Function
    
    mov.l R8, @-R15
    mov.l R9, @-R15
    mov.l R10, @-R15
    add  #-40, R15
    
    PROLOG_END
    
    // Routine body
    
    add  #40, R15
    mov.l @R15+, R10
    mov.l @R15+, R9
    rts
    mov.l @R15+, R8
    
    ENTRY_END Function
    
  • Allocate a stack frame with 64KB of memory

    This example allocates a stack frame that requires 64 KB of memory. It saves argument register R4 to the incoming argument save area, and saves argument register R5 to allocated register R8. No separate frame pointer is required. The segment saves return address and permanent register R8. It declares an exception handler.

    EXCEPTION_HANDLER RoutineHandler
    NESTED_ENTRY Function
    
    mov.l R4, @R15   // Save argument to incoming argument save area.
    mov.l R8, @-R15
    sts.l PR, @-R15
    mov.l @(0x0000001C,pc),r1   // Load constant -65528.
    add     r1,r15        // Allocate stack frame.
    mov.l R5, R8        // Save argument to register.
    
    PROLOG_END
    
    // Routine body
    
    mov.l @(0x0000000C,pc),r1   // Load constant 65528.
    add     r1,r15        // Remove stack frame.
    lds.l @R15+, PR      // Recover return address.
    rts
    mov.l @R15+, R8     // Restore R8.
    
    ENTRY_END Function
    
  • SH-4 function call

    This example shows an SH-4 function call with permanent floating point register saves and restores. It distributes floating-point arguments to registers, and copies argument registers to allocated registers and the stack.

    NESTED_ENTRY Function
    
    mov.l    r8, @-sp // Store permanent registers.
    mov.l    r9, @-sp
    fmov.s   fr12, @-sp
    fmov.s   fr13, @-sp
    sts.l    pr,  @-sp
    add.l    #-80, sp // Allocate stack frame.
    mov.l    r5, r8 // Move incoming argument registers to 
    fmov.s   fr8, fr13 // allocated registers.
    mov.l    #112, r0  // Spill argument to stack.
    add.l    sp,  r0
    fmov.s   fr6, @r0
    fmov.s   fr7, @-r0
    PROLOG_END
    
    // Routine body
    
    add.l   #80, sp
    lds.l   @sp+,pr   // Restore permanent registers.
    fmov.s   @sp+,fr13
    fmov.s   @sp+,fr12
    mov.l   @sp+,r9
    rts
    mov.l   @sp+,r8
    
    ENTRY_END Function
    

See Also

SH-4 Prolog and Epilog | SH-4 Prolog | SH-4 Epilog

 Last updated on Thursday, April 08, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.