Microchip Technology SW006022-1N Ficha De Dados

Página de 338
MPLAB
®
 XC16 C Compiler User’s Guide
DS52071B-page 122
 2012 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 Assembler, Linker and Utilities 
for PIC24 MCUs and dsPIC DSCs User’s Guide (DS51317)
 for more information.
7.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 function’s 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 7-1:
STACK AND FRAME POINTERS 
The C run-time startup modules (crt0.o and crt1.o 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 
Stack grows 
toward 
greater 
addresses
SP (W15)
FP (W14)
Function Frame