Stack Pointer Register
The stack is a crucial part of any microcontroller's operation, especially when it comes to function calls and interrupts. The Stack Pointer Register keeps track of the top of the stack, ensuring data push and pop operations happen seamlessly.
label_important
The Stack is mainly used for storing temporary data, for storing local variables and for storing return addresses after interrupts and subroutine calls. The Stack Pointer Register always points to the top of the Stack. Note that the Stack is implemented as growing from higher memory locations to lower memory locations.
This implies that a Stack PUSH command decreases the Stack Pointer. The Stack Pointer points to the data SRAM Stack area where the Subroutine and Interrupt Stacks are located.
This Stack space in the data SRAM must be defined by the program before any subroutine calls are executed or interrupts are enabled.
The Stack Pointer must be set to point above 0x60
.
Stack Pointer Register is defined as -
Bit15 |
Bit14 |
Bit13 |
Bit12 |
Bit11 |
Bit10 |
Bit9 |
Bit8 |
SP15 |
SP14 |
SP13 |
SP12 |
SP11 |
SP10 |
SP9 |
SP8 |
SPH |
Bit7 |
Bit6 |
Bit5 |
Bit4 |
Bit3 |
Bit2 |
Bit1 |
Bit0 |
SP7 |
SP6 |
SP5 |
SP4 |
SP3 |
SP2 |
SP1 |
SP0 |
SPL |
The Stack Pointer is decremented by one when data is pushed onto the Stack with the PUSH instruction, and it is decremented by two when the return address is pushed onto the Stack with a subroutine call or interrupt.
It is incremented by one when data is popped from the Stack with the POP instruction, and it is incremented by two when the address is popped from the Stack with return from subroutine RET or return from interrupt RETI.
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.