8.3 Stack
The 16-bit devices use what is referred to in this user’s guide as a “software stack.” This is the typical stack arrangement employed by most computers and is ordinary data memory accessed by a push-and-pop type instruction and a stack pointer register. The term “hardware stack” is used to describe the stack employed by Microchip 8-bit devices, which is only used for storing function return addresses.
The 16-bit devices dedicate register W15 for use as a software Stack Pointer. All processor stack operations, including function calls, interrupts and exceptions, use the software stack. The stack grows upward, towards higher memory addresses.
The dsPIC DSC device also supports stack overflow detection. If the Stack
Pointer Limit register, SPLIM, is initialized, the device will test for overflow on all
stack operations. If an overflow should occur, the processor will initiate a stack error
exception. By default, this will result in a processor Reset. Applications may also install
a stack error exception handler by defining an interrupt function named
_StackError
. See Interrupts for details.
The C run-time startup module initializes the Stack Pointer (W15) and the
Stack Pointer Limit register during the startup and initialization sequence. The initial
values are normally provided by the linker, which allocates the largest stack possible from
unused data memory. The location of the stack is reported in the link map output file.
Applications can ensure that at least a minimum-sized stack is available with the
--stack
linker command-line option. See the MPLAB®
XC16 Assembler, Linker and Utilities User’s Guide (DS50002106) for details.
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 User’s Guide (DS50002106) for more information.