Module 5 — Stack Pointer (SP)
Learning Objectives
After completing this module, you should be able to answer:
- What is a stack?
- Why does a CPU need a stack?
- What is the Stack Pointer?
- Why does the stack usually grow downward?
- How are function calls implemented?
- How are local variables stored?
- What happens during recursion?
- What is a stack frame?
- What is stack overflow?
- How does the OS use the stack?
- How does an RTOS use separate stacks?
1. What is a Stack?
A stack is a special memory area that follows the LIFO (Last In, First Out) principle.
Think of a stack of plates.
If you want Plate A, you must first remove D, C, and B.
Computers use exactly the same idea.
2. Why Do We Need a Stack?
Every program repeatedly calls functions.
Example:
main()
{
fun1();
}
fun1()
{
fun2();
}
fun2()
{
}
Question:
How does the CPU know where to return after fun2() finishes?
Answer:
The stack stores the return address.
Without a stack, the CPU would not know where execution should continue.
3. What is the Stack Pointer (SP)?
The Stack Pointer is a special CPU register.
It always points to the top of the stack.
Example:
Whenever something is pushed,
SP changes.
Whenever something is popped,
SP changes again.
4. Push Operation
SupposeSP = 1000
Push value10
After PUSH
Notice
SP moved.
Diagram
Before
After PUSH
5. Pop Operation
Current stack
ResultSP =1000
Value10
removed.
Push/Pop Animation
PUSH
POP
6. Why Does the Stack Grow Downward?
Most CPUs use descending stacks.
Example
Reasons include:
- Efficient coexistence with the heap (which typically grows upward).
- Simpler addressing modes on many architectures.
- Historical CPU design choices.
Note: While downward-growing stacks are common (ARM, x86, many others), they are not universal. Some architectures use upward-growing stacks.
7. Function Call Example
void add()
{
int x=10;
}
int main()
{
add();
}
When main() calls add():
CPU performs:
Stack During Function Call
Initially
After CALL
After Local Variable
8. Function Parameters
Example
add(10,20);
Depending on the calling convention:
Arguments may be
- Registers (preferred)
- Stack (when registers are exhausted or required)
Example Stack
On ARM, the first few integer arguments are usually passed in registers (R0–R3 in AArch32, X0–X7 in AArch64), with additional arguments placed on the stack.
9. Local Variables
Example
void fun()
{
int a;
int b;
}
The compiler reserves stack space.
When the function returns,
this memory is released automatically by adjusting the SP.
10. Nested Function Calls
Example
Stack
Each function gets its own stack frame.
11. Stack Frame
A stack frame contains everything needed for one function invocation.
The OS and debugger use stack frames to unwind function calls.
12. Recursion
Example
factorial(5)
Calls
Each call creates another stack frame.
This is why deep recursion can exhaust stack space.
13. Stack Overflow
Suppose
Stack Size8 KB
Recursive function
fun()
{
fun();
}
Never returns.
Eventually
This is a stack overflow.
In embedded systems, stack size is often fixed, making this especially important.
14. RTOS Example
RTOS
Each task has its own Stack Pointer.
Context switch
This allows each task to resume exactly where it left off.
15. Interrupt Stack
Suppose
Image Processing running.
Camera interrupt occurs.
CPU
Many CPUs perform part of this automatically on exception entry.
Embedded Example (ADAS)
Suppose
The interrupted code resumes because the CPU preserved the execution state on the stack.
ARM Stack Example
BL (Branch with Link) stores the return address in the Link Register. If needed (for nested calls), the compiler saves the Link Register on the stack.
x86 Stack Example
GDB Example
When you execute:
backtrace
GDB walks through stack frames to produce something like:
This is how crash reports identify the call sequence.
Interview Questions
Beginner
- What is a stack?
- What is the Stack Pointer?
- What is LIFO?
- Why is the stack needed?
Intermediate
- Explain PUSH and POP.
- Why does the stack usually grow downward?
- What is a stack frame?
- How are local variables stored?
- How are return addresses stored?
Senior
- Explain a function call at the assembly level.
- How does recursion affect the stack?
- Explain stack overflow.
- How does an RTOS use multiple stacks?
- How does the CPU use the stack during an interrupt?
- Why does a debugger rely on stack frames?
Common Mistakes
- ❌ Thinking the stack stores the entire program. It mainly stores call-related data such as return addresses, local variables, saved registers, and sometimes function arguments.
- ❌ Assuming all function parameters are passed on the stack. Modern ABIs pass many arguments in registers.
- ❌ Believing local variables always live on the stack. Compilers may keep them in registers or optimize them away.
- ❌ Assuming every architecture grows the stack downward. Most do, but not all.
Summary
The stack is a LIFO memory region that supports function calls, local variables, recursion, interrupts, and multitasking. The Stack Pointer always identifies the top of the current stack. During execution, the CPU and operating system continuously push and pop data as functions are called, interrupts occur, and tasks are switched. A solid understanding of the stack is essential for debugging, embedded development, and operating system internals.
Mini Quiz
- Why is the stack required for function calls?
- What does the Stack Pointer contain?
- Why does the stack usually grow downward?
- What information is stored in a stack frame?
- Why does recursion consume more stack memory?
- How does an RTOS allow multiple tasks to run independently using stacks?
- Why does a stack overflow occur?
Concept Mastery Assessment — Module 05
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.