Stack Convention - 2024.2 English

MicroBlaze Processor Reference Guide (UG984)

Document ID
UG984
Release Date
2024-11-27
Version
2024.2 English

The stack conventions used by MicroBlaze are detailed in the following table.

The shaded area in the following table denotes a part of the stack frame for a caller function, while the unshaded area indicates the callee frame function. The ABI conventions of the stack frame define the protocol for passing parameters, preserving non-volatile register values, and allocating space for the local variables in a function.

Functions that contain calls to other subroutines are called as non-leaf functions. These non-leaf functions have to create a new stack frame area for its own use. When the program starts executing, the stack pointer has the maximum value. As functions are called, the stack pointer is decremented by the number of words required by every function for its stack frame. The stack pointer of a caller function always has a higher value as compared to the callee function.

Table 1. Stack Convention
High Address
Function Parameters for called sub-routine (Arg n .. Arg1)

(Optional: Maximum number of arguments required for any called procedure from the current procedure).

Old Stack Pointer Link Register (R15)
Callee Saved Register (R31....R19)

(Optional: Only those registers which are used by the current procedure are saved)

Local Variables for Current Procedure

(Optional: Present only if Locals defined in the procedure)

Functional Parameters (Arg n .. Arg 1)

(Optional: Maximum number of arguments required for any called procedure from the current procedure)

New Stack Pointer Link Register
Low Address

Consider an example where Func1 calls Func2, which in turn calls Func3. The stack representation at different instances is depicted in the following figure. After the call from Func 1 to Func 2, the value of the stack pointer (SP) is decremented. This value of SP is again decremented to accommodate the stack frame for Func3. On return from Func 3 the value of the stack pointer is increased to its original value in the function, Func 2.

Details of how the stack is maintained are shown in the following figure.

Figure 1. Stack Frame

Stack protection is available to ensure that the stack does not grow above the high limit or shrink below the low limit. The Stack High Register (SHR) and Stack Low Register (SLR) are used to enforce this, respectively. These registers are automatically initialized to the stack limits from linker symbols by the crt0.o initialization file.

Enabling stack protection in hardware can be useful to detect erroneous program behavior due to stack size issues, which can otherwise be very hard to debug.