Skip to main content

Microcontrollers

Section 13.5 The Stack and Stack Operations

The stack is a special area of RAM reserved for temporary data storage. It cannot overlap with the general purpose, I/O, or extended I/O memory registers. Therefore, the memory address at which it starts on the ATmega328P must be greater than or equal to 0x0100, which is the first address used for internal SRAM. Usually, the stack is initialized at the highest available address in RAM. (This is done automatically on the ATmega328P microcontroller, which initializes the stack pointer register at a value equal to RAMEND, the last address of the internal SRAM.)
The current location of the most recent data byte in the stack is known as the stack pointer (SP). The stack pointer consists of two 8-bit registers that are stored in the I/O register space.
The stack itself can be visualized as a stack of plates in a cafeteria, with each piece of data represented by a plate. A new plate can be added to the stack by placing it on top of the other plates. When data needs to be removed from the stack, the plate on top must be removed first. This system is known as last-in, first-out.
Data can be added to the stack using the push register on stack instruction (PUSH). When this occurs, new information is placed into temporary storage, and the value stored in SP is decremented. Removing data from the stack occurs using the pop register from stack instruction (POP). The value stored in SP is incremented during this instruction.
The contents of the stack and location of SP is depicted schematically in Figureย 13.5.1 (a). Figureย 13.5.1 (b) demonstrates this information after a PUSH operation. Figureย 13.5.1 (c) demonstrates this information after a subsequent POP operation.
Three rectangles each containing 5 segments. The segment lowest down is labeled as "RAMEND." The left rectangle (a) has the lowest 3 segments labeled "stack data." The middle segment has an arrow pointing to the label "SP." The top 2 segments are labeled "unused." The middle rectangle (b) has the lowest 3 segments labeled "stack data." The next segment up is labeled "new data" and has the arrow pointing to the label "SP." The top segment is labeled "unused." The right rectangle (c) has the lowest 3 segments labeled "stack data." The middle segment has an arrow pointing to the label "SP." The top 2 segments are labeled "unused."
Figure 13.5.1. Stack operations. (a) Memory in the stack, (b) after a PUSH instruction, and (c) after a POP instruction.
Use of the stack is essential during a subroutine call (external function or an ISR). In order to return to the exact spot in memory where the program left to execute the subroutine, the return address is pushed to the top of the stack before the location of the subroutine is accessed. After the subroutine is complete, the program goes back to the location and the return address is popped out of the stack.

Subsection 13.5.1 Stack Problems

Care must be taken when allocating data memory to be used in the stack. Stack overflow is a situation in which too much data is pushed onto the stack which causes the stack pointer (SP) to point to an address outside of the stack memory area.
Stack underflow is a situation in which too much data is popped from the stack so that the stack pointer (SP) points to an address below the stack bottom.
A stack collision occurs when the stack is allocated to memory addresses that overlap with data registers.