5.5.1 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 is five bits to allow detection of overflow and
underflow.
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. The STKPTR always points at the currently
used place on the stack. Therefore, a CALL
or CALLW
will increment the STKPTR and then write the PC, and a return will unload the PC value
from the stack and then decrement the STKPTR.
Reference the following figures for examples of accessing the stack.