9.5 Stack Pointer

The Stack is mainly used for storing temporary data, local variables, and return addresses after interrupts and subroutine calls. The Stack is implemented as growing from higher to lower memory locations. The Stack Pointer register always points to the top of the Stack.

The Stack Pointer points to the data SRAM Stack area where the Subroutine and Interrupt Stacks are located. A Stack PUSH command will decrease the Stack Pointer. The Stack in the data SRAM must be defined by the program before any subroutine calls are executed or interrupts are enabled. Initial Stack Pointer value equals the last address of the internal SRAM and the Stack Pointer must be set to point above start of the SRAM. See the table for Stack Pointer details.

Table 9-1. Stack Pointer Instructions
InstructionStack PointerDescription
PUSHDecremented by 1Data is pushed onto the stack

CALL

ICALL

RCALL

Decremented by 2Return address is pushed onto the stack with a subroutine call or interrupt
POPIncremented by 1Data is popped from the stack

RET

RETI

Incremented by 2Return address is popped from the stack with return from subroutine or return from interrupt

The AVR Stack Pointer is implemented as two 8-bit registers in the I/O space. The number of bits actually used is implementation dependent. Note that the data space in some implementations of the AVR architecture is so small that only SPL is needed. In this case, the SPH register will not be present.