Skip to content

The Stack

Stack Memory

A Stack is a data structure which should hopefully need no explanation. But why do we use Stacks for managing memory related to functions, as opposed to a Queue or a List?

Calling functions inside functions behaves like a stack. The last function to be called is the first to be returned. This is why we model them as a stack.

What is stored on the Stack?

The stack stores the addresses that the function is supposed to return to. Additionally, it can store register values that are spilt by the caller / callee. The caller-saved registers are pushed first, then the return address, then the callee-saved registers. They are popped in the reverse order.

Stacks can be either increasing or decreasing. Increasing stacks go to higher memory addresses when values are pushed, with decreasing stacks doing the opposite.

Stack Pointer

The stack pointer is a register that stores the current head of the stack. The register used for this is decided by the ISA.