Share via


ARM Stack Alignment (Compact 2013)

3/26/2014

This topic explains how to maintain stack alignment when porting your code to work in Windows Embedded Compact 2013. When porting your code to ARM, you must keep the stack aligned on a four-byte boundary. When you are calling a function, you must keep the stack aligned on an eight-byte boundary.

To maintain stack alignment:

  • Use the prolog and epilog macros LEAF_ENTRY, LEAF_END, NESTED_ENTRY, and NESTED_END in your functions.
  • For leaf functions, which are functions that don’t call other functions, change occurrences of ENTRY_END to LEAF_END. If you have functions that use LEAF_ENTRY but don’t have a corresponding ENTRY_END, you will need to add LEAF_END.
  • For nested functions, which are functions that do call other functions, change occurrences of ENTRY_END to NESTED_END.
  • In nested functions, use prolog helpers such as PROLOG_PUSH and EPILOG_POP to save and restore registers.
  • Use PROLOG_STACK_ALLOC when allocating space on the stack and ensure that the stack is always four-byte aligned or eight-byte aligned before calling a function.

The following example shows the use of these macros to align the stack.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

;-------------------------------------------------------------------------------
;  Function:  NestedFunction
;
    NESTED_ENTRY NestedFunction
    PROLOG_PUSH {r11, lr}    ; The stack remains 8-byte aligned because we used PROLOG_PUSH

    PROLOG_STACK_ALLOC Size  ; Be sure that Size is 8-byte aligned because we are
                             ; going to call a function.
    ...
    bl do_something
    EPILOG_STACK_FREE Size   ; Free the stack space allocated by PROLOG_STACK_ALLOC
    EPILOG_POP {r11, pc}     ; Restore the registers saved by PROLOG_PUSH
    ; Restore registers
    NESTED_END NestedFunction

For more information about these macros, see Macros for ARM Porting.

See Also

Concepts

Port an ARM BSP to Compact 2013