Stack Frame and Alignment

The following figure depicts generation of the stack frame. The stack grows upward, so pushing data onto the stack increments the stack pointer’s address value.

Figure 1. Stack Frame
Stack Frame

The stack pointer (SP) is a 24-bit register, always treated as a word pointer. During a function execution, the stable position for the SP is at the top of the user and compiler locals. The SP increases during the call if the stack is used for passed parameters.

The software stack supports structured programming techniques, such as parameter passing to subroutines and local variables. These techniques are available for both assembly-language and high-level-language programming. It is possible to support passed parameters and local variables for a subroutine at the same time within the stack frame.

The compiler stores local data by size. It stores smaller data closest to the SP, exploiting SP addressing modes that have small offsets. This means that the compiler packs all bytes two per word near the stack pointer. It packs the block of words next, then blocks of longs. Aggregates (structs and arrays) are farthest from the stack pointer, not sorted by size.

Note: When a function makes a dynamic allocation, the compiler reserves R5 as a stack frame pointer. (This is the stack pointer before allocations.)
The compiler always must operate with the stack pointer long aligned. This means that: