Accessing the Stack

The stack is accessible through the TOSH, TOSL and STKPTR registers. STKPTR is the current value of the Stack Pointer. The TOSH:TOSL register pair points to the TOP of the stack. Both registers are read/writable. TOS is split into TOSH and TOSL due to the 15-bit size of the PC. To access the stack, adjust the value of STKPTR, which will position TOSH:TOSL, then read/write to TOSH:TOSL. STKPTR also allows the detection of Overflow and Underflow condition.
Important: Care must be taken when modifying STKPTR while interrupts are enabled.

During normal program operation, CALL, CALLW and interrupts will increment STKPTR, while RETLW, RETURN and RETFIE will decrement STKPTR. STKPTR can be monitored to obtain the value of stack memory left at any given time. STKPTR always points at the currently used place on the stack. Therefore, a CALL or CALLW will increment STKPTR and then write the PC, and a return will unload the PC value from the stack and then decrement STKPTR.

Reference the following figures for examples of accessing the stack.
Figure 1. Accessing the Stack Example 1
Figure 2. Accessing the Stack Example 2
Figure 3. Accessing the Stack Example 3
Figure 4. Accessing the Stack Example 4