Microchip Technology SW006023-2N Data Sheet

Page of 238
MPLAB
®
 XC32 C/C++ Compiler User’s Guide
DS51686E-page 112
 2012 Microchip Technology Inc.
7.4
AUTO VARIABLE ALLOCATION AND ACCESS
This section discusses allocation of auto variables (those with automatic storage dura-
tion). This also includes function parameter variables, which behave like auto vari-
ables, as well as temporary variables defined by the compiler.
The auto (short for automatic) variables are the default type of local variable. Unless 
explicitly declared to be static, a local variable will be made auto. The auto 
keyword may be used if desired.
The auto variables, as their name suggests, automatically come into existence when 
a function is executed, then disappear once the function returns. Since they are not in 
existence for the entire duration of the program, there is the possibility to reclaim mem-
ory they use when the variables are not in existence and allocate it to other variables 
in the program.
The PIC32’s software stack is used to store all auto variables. Functions are reentrant 
and each instance of the function has its own area of memory on the stack for its auto 
and parameter variables, as described below. See Section 4.4 “Stack” and 
Section 12.3.2 “Initialize Stack Pointer and Heap” for more information on the stack.
The compiler dedicates General Purpose Register 29 as the software Stack Pointer. All 
processor stack operations, including function call, interrupts and exceptions use the 
software stack. The stack grows downward from high addresses to low addresses.
By default, the size of the stack is 1024 bytes. The size of the stack may be changed 
by specifying the size on the linker command line using the 
--defsym_min_stack_size
 linker command line option. An example of allocating 
a stack of 2048 bytes using the command line is:
xc32-gcc foo.c -Wl,--defsym,_min_stack_size=2048
The run-time stack grows downward from higher addresses to lower addresses (see 
Figure 7-1). The compiler uses two working registers to manage the stack:
• Register 29 (sp) – This is the Stack Pointer. It points to the next free location on 
the stack.
• Register 30 (fp) – This is the Frame Pointer. It points to the current function’s 
frame. Each function, if required, creates a new frame from which automatic and 
temporary variables are allocated. Compiler optimization may eliminate Stack 
Pointer references via the Frame Pointer to equivalent references via the Stack 
Pointer. This optimization allows the Frame Pointer to be used as a General 
Purpose Register.