A stack is a consecutive block of data memory allocated by the programmer. This block of memory can be use both by the microcontroller internal control as well as the programmer to store data temporarily. The stack operates with a Last In First Out (LIFO) mechanism, i.e the last thing store on the stack is the first thing to be retrieved from the stack.
The stack pointer is basically a register or registers that holds either "the memory address of the last location on that stack where data was stored" or "the memory address of the next available location on the stack to store data." The definition of the stack pointer depends on the design of the microcontroller.
In AVR microcontrollers, such as the ATMega8515, ATMega16, ATTiny13, etc., the stack pointer holds the address on the next available location on the stack available to store data.
The AVR 8-bits microcontroller stack pointer can either consist of a single I/O register SPL (Stack Pointer Low) or two (2) I/O registers SPL and SPH (Stack Pointer High). The size of the stack pointer depends on the amount of data memory a microntroller contains. If the entire data memory can be addressed using 8-bits then the stack pointer is 8-bits wide i.e. SPL only, otherwise the stack pointer is consist of SPL and SPH.

.include "m8515def.inc" LDI R16, low(RAMEND) OUT SPL, R16 LDI R16, high(RAMEND) OUT SPH, R16
/* * Compatibly in both AVR Studio 5 & AVR Studio 6 * * Created: 10/15/2011 2:44:24 PM * Author: AVR Tutorials */ .include "tn2313def.inc" ;Set up AVR ATTiny2313 stack LDI R16, RAMEND OUT SPL, R16 LDI R16, 0x33 LDI R17, 0x25 LDI R18, 0x0A PUSH R16 PUSH R17 POP R17 PUSH R18 end: RJMP end
![]() Figure 1 - Initial Stack |
![]() Figure 2 - Stack After First PUSH |
![]() Figure 3 - Stack After Second PUSH |
![]() Figure 4 - Stack After POP |
![]() Figure 5 - Stack After Third PUSH |
|
AVR Tutorials hopes this tutorial on the AVR stack and stack pointer was beneficial to you and looks forward to your continued visits for all your microcontroller tutorial needs.