3.9.1 Software Stack Pointer

The dsPIC33A devices feature a software stack which facilitates function calls and exception handling. W15 is the default Stack Pointer (SP) and after any reset, it is initialized to 0x4000. This ensures that the SP will point to valid RAM and permits stack availability for exceptions, which may occur before the SP is set by the user software. The user may reprogram the SP during initialization to any location within data space.

The SP always points to the first available free word at TOS (Top-of-Stack) and fills the software stack, working from lower addresses towards higher addresses. It pre-decrements for a stack POP (read) and post-increments for a stack PUSH (write).

The software stack is manipulated using the PUSH and POP instructions. The PUSH and POP instructions are the equivalent of a MOV instruction with W15 used as the destination pointer. For example, the contents of W0 can be PUSHed onto the TOS by:

 PUSH.L W0

This syntax is equivalent to:

MOV.L W0,[W15++]

The contents of the TOS can be returned to W0 by:

 POP.L W0

This syntax is equivalent to:

MOV.L [--W15],W0

During any CALL instruction, the PC is PUSHed onto the stack, such that when the subroutine completes execution, program flow may resume from the correct location. When the PC is PUSHed onto the stack, the Most Significant Byte of PC is zero-extended, as shown in Figure 3-3. During exception processing, the Most Significant seven bits of the PC are concatenated with the lower byte of the STATUS Register (SRL) and IPL[3] (CORCON[3]). This allows the primary STATUS Register contents and CPU Interrupt Priority Level to be automatically preserved during interrupts.

Note: In order to protect against misaligned stack accesses, W15[0] is always clear.
Figure 3-3. Stack Operation for CALL Instruction