Vault 7: Projects
This publication series is about specific projects related to the Vault 7 main publication.
MPLAB
®
XC16 C Compiler Users Guide
DS50002071C-page 148 2012-2013 Microchip Technology Inc.
Alternatively, a stack of specific size may be allocated with a user-defined section from
an assembly source file. In the following example, 0x100 bytes of data memory are
reserved for the stack:
.section *,data,stack
.space 0x100
The linker will allocate an appropriately sized section and initialize __SP_init and
__SPLIM_init so that the run-time startup code can properly initialize the stack. Note
that since this is a normal assembly code, section attributes such as address may be
used to further define the stack. Please see the MPLAB XC16 Assembler, Linker and
Utilities Users Guide (DS52106) for more information.
10.3.2.2 THE C STACK USAGE
The C compiler uses the software stack to:
Allocate automatic variables
Pass arguments to functions
Save the processor status in interrupt functions
Save function return address
Store temporary results
Save registers across function calls
The run-time stack grows upward from lower addresses to higher addresses. The
compiler uses two working registers to manage the stack:
W15 This is the Stack Pointer (SP). It points to the top of stack which is defined
to be the first unused location on the stack.
W14 This is the Frame Pointer (FP). It points to the current functions frame.
Each function, if required, creates a new frame at the top of the stack from which
automatic and temporary variables are allocated. The compiler option
-fomit-frame-pointer can be used to restrict the use of the FP.
FIGURE 10-1: STACK AND FRAME POINTERS
The C run-time startup modules in libpic30-omf.a initialize the Stack Pointer W15
to point to the bottom of the stack and initialize the Stack Pointer Limit register to point
to the top of the stack. The stack grows up and if it should grow beyond the value in the
Stack Pointer Limit register, then a stack error trap will be taken. The user may initialize
the Stack Pointer Limit register to further restrict stack growth.
The following diagrams illustrate the steps involved in calling a function. Executing a
CALL or RCALL instruction pushes the return address onto the software stack. See
Figure 10-2.
Stack grows
toward
greater
addresses
SP (W15)
FP (W14)
Function Frame
Protego_Release_01_05-Related-OEM-Documentation-MPLAB-XC16-C-Compiler.pdf