7.4.4 Stack and Stack Pointer
The stack is used to store return addresses after interrupts and subroutine calls and for storing temporary data. The Stack Pointer (SP) always points to the top of the stack. The address pointed to by the SP is stored in the Stack Pointer (CPU.SP) register. The CPU.SP is implemented as two 8-bit registers accessible in the I/O memory space.
Data are pushed and popped from the stack using the instructions in the table below or by executing interrupts. The stack grows from higher to lower memory locations, implying that pushing data to the stack will decrease the SP, and popping data from the stack will increase the SP.
The SP is automatically set to the highest address of the internal SRAM after a reset. If the stack needs to be allocated to a different SRAM address location (or if multiple stacks are used), the address must fall within the SRAM address space, with sufficient space reserved for the anticipated stack size. See the SRAM Data Memory topic in the Memories section for the SRAM start address and SRAM size. The new SP must be defined before any subroutine calls execute and interrupts are enabled. See the table below for SP details.
Instruction | Stack Pointer | Description |
---|---|---|
PUSH | Decremented by 1 | Data are pushed onto the stack |
CALL | Decremented by 2 | A return address is pushed onto the stack with a subroutine call or interrupt |
POP | Incremented by 1 | Data are popped from the stack |
RET | Incremented by 2 | A return address is popped from the stack with a return from either a subroutine or an interrupt |
During interrupts or subroutine calls, the return address is pushed automatically on the stack as a word, and the SP is decremented by two. The return address consists of two bytes, and the Least Significant Byte (LSB) is pushed on the stack first (at the higher address). For example, a byte pointer return address of 0x0006 is saved on the stack as 0x0003 (shifted one bit to the right), pointing to the fourth 16-bit instruction word in the program memory. The return address is popped off the stack with RETI (when returning from interrupts) and RET (when returning from subroutine calls), and the SP is incremented by two.
The SP is decremented by one when data are pushed on the stack with the PUSH instruction and incremented by one when data are removed from the stack using the POP instruction.
To prevent corruption when updating the SP from software, a write to SPL will automatically disable interrupts for up to four instructions or until the next I/O memory write, whichever comes first.