Difference between revisions of "Stack"

From SEGGER Wiki
Jump to: navigation, search
(Created page with "A stack is a memory area used to store and retrieve information in LIFO (last in, first out) order. It is very commonly used in basically any computer system and any program....")
 
m
Line 4: Line 4:
 
There are basically only 2 operation which can be performed on a stack:
 
There are basically only 2 operation which can be performed on a stack:
 
* Push - Adding information to the stack
 
* Push - Adding information to the stack
* Pop - Retrieving information from stack
+
* Pop - Removing information from stack
   
A stack needs a stack pointer (SP), which typically points to the next address to either push or pop from.
+
A stack needs a stack pointer (SP), which typically points to the last item pushed onto the stack.
  +
Hardware stacks used for program execution (that contain return addresses and activation frames) typically grow
Stacks typically grow downward, so pushing a 32-bit value onto a stack will decrement the stack pointer by 4,
 
where pop a 32-bit value does the opposite and increments the stack pointer by 4.
+
downward from high memory to low memory; pushing a 32-bit value onto a stack will decrement the stack pointer
  +
by 4, where pop a 32-bit value does the opposite and increments the stack pointer by 4.
   
 
==Stack overflow==
 
==Stack overflow==
One problem that is hard to control and avoid is stack overflow. It happens when the application has pushed more data onto the stack that actually fit,
+
One problem that is hard to control and avoid is stack overflow. It happens when the application has pushed more data onto the stack than actually fit,
 
thereby overwriting other data areas or writing into areas that are not data. In any case, the effects will usually create problem when program execution continues,
 
thereby overwriting other data areas or writing into areas that are not data. In any case, the effects will usually create problem when program execution continues,
 
very likely a crash of the program.
 
very likely a crash of the program.
This is a problem especially in smaller [[Embedded System]]s, which do not have an MMU or MPU to control the access to the stack.
+
This is a problem especially in smaller [[Embedded System]]s, which do not have a memory management unit (MMU) or
  +
memory protection unit (MPU) to control the access to the stack.

Revision as of 06:45, 24 June 2019

A stack is a memory area used to store and retrieve information in LIFO (last in, first out) order. It is very commonly used in basically any computer system and any program.

Basic operations

There are basically only 2 operation which can be performed on a stack:

  • Push - Adding information to the stack
  • Pop - Removing information from stack

A stack needs a stack pointer (SP), which typically points to the last item pushed onto the stack. Hardware stacks used for program execution (that contain return addresses and activation frames) typically grow downward from high memory to low memory; pushing a 32-bit value onto a stack will decrement the stack pointer by 4, where pop a 32-bit value does the opposite and increments the stack pointer by 4.

Stack overflow

One problem that is hard to control and avoid is stack overflow. It happens when the application has pushed more data onto the stack than actually fit, thereby overwriting other data areas or writing into areas that are not data. In any case, the effects will usually create problem when program execution continues, very likely a crash of the program. This is a problem especially in smaller Embedded Systems, which do not have a memory management unit (MMU) or memory protection unit (MPU) to control the access to the stack.