What happens when you call a function?
A visual guide to function call principles through assembly language.
This visual guide explains how function calls work at the low level, using detailed diagrams to illustrate the complete stack evolution during function execution. Through hands-on examples on the x86-64 platform, you'll gain a solid understanding of how C code translates to assembly instructions. The guide concludes with practical examples demonstrating interesting stack frame behaviors.
- How control transfers between functions
- How data is passed between functions
- How stack frames are created and destroyed
- The dangers of array out-of-bounds access in C/C++
- Why inline functions are more efficient
- Why loops are preferred over recursion
- Why static variables persist after function returns
- What stack overflow attacks are and how they work
- Prerequisites
- The Stack in Memory
- Control Transfer
- Data Passing
- Register Saving and Restoring
- Storage of Local Variables
- Function Stack Frame
- C Function Stack Frame Example
- Stack Frame Memory Reuse
- Manually Modifying Stack Frame Data
- Summary
-
Books:
- "Computer Systems: A Programmer's Perspective" (3rd Edition)
-
Online Courses:
All diagrams were created with Excalidraw.
Translation assisted by Claude (Anthropic).
MIT
