AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

AVR Microcontroller Stack and Stack Pointer

Tutorial Objectives


After completing this AVR microcontroller tutorial readers should be able to:
  • Give a technical definition for the term stack.
  • Give a technical definition for the term stack pointer.
  • Explain the operation of the stack and stack pointer.
  • Write assembly code to initialize an AVR stack pointer.


AVR Stack & Stack Pointer



What is a stack?

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.

What is the Stack Pointer?

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 Stack Pointer

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.



Setting-up the AVR Stack


A programmer set up the stack on a microcontroller by loading the start address of the stack into the microcontroller stack pointer. The code below shows how to set up the stack in an AVR ATMega8515 microcontroller. Here the address being loaded is that of the last memory location in SRAM. This code could also be used for the ATMega16 , ATMega32 or any one of the AVR microcontrooler with a 16-bits stack pointer, you just of the replace the "m8515def.inc" include file with "m16def.inc" or "m32def.inc" respectively.


Notes:
  1. RAMEND is a label that represents the address of the last memory location in SRAM. To use this label you MUST ensure that you include the definition header file for the specific microcontroller.
  2. The functions low() and high() are use by the assembler to return the low byte and high byte respectively of a 16-bit word. Remember we are dealing with a 8-bit microcontroller which can only handle only 8-bits at a time. RAMEND is a 16-bit word and so we use the functions to split it.
.include "m8515def.inc"
 
		LDI	R16, low(RAMEND)
		OUT	SPL, R16
		LDI	R16, high(RAMEND)
		OUT	SPH, R16


AVR Microcontroller's Stack Operation


As mentioned earlier the stack can be use by both the microcontroller internal control and the programmer. The programmer accesses the stack using the PUSH and POP instructions. The PUSH instruction is use to store data on the stack while the POP instruction is used to retrieve data from the stack. The operation of the AVR stack will be discussed using the code that follows.


Notes:
Notice here the microcontroller being used in the code below is the Atmel ATTiny2313 AVR microcontroller. This AVR microcontroller's data memory can be addressed using 8-bits and therefore its stack pointer is defined only by the SPL.

/*
 *  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


Atmel AVR Microcontroller Stack Operation
Figure 1 - Initial Stack
Atmel AVR Microcontroller Stack Operation
Figure 2 - Stack After First PUSH
Atmel AVR Microcontroller Stack Operation
Figure 3 - Stack After Second PUSH
Atmel AVR Microcontroller Stack Operation
Figure 4 - Stack After POP
Atmel AVR Microcontroller Stack Operation
Figure 5 - Stack After Third PUSH
Atmel AVR Microcontroller Stack Operation



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.