100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

The Stack in Assembly

Understand how the x86 stack, ESP, EBP, CALL, RET, and calling conventions work together to manage function calls, local variables, and register preservation.

Instructions & DataIntermediate11 min readJul 10, 2026
Analogies

The Stack in Assembly

The stack is a region of memory managed via the ESP register (RSP in 64-bit mode) that follows a strict last-in-first-out (LIFO) discipline: the most recently pushed value is always the first one popped. Unlike a heap, which grows upward from low addresses, the x86 stack conventionally grows downward -- each PUSH decrements ESP before storing a value, and each POP reads the value at [ESP] before incrementing it back up, so ESP always points to whatever was pushed most recently.

🏏

Cricket analogy: A stack of players waiting to bat in the pavilion, where the next man in is always the one who arrived most recently, mirrors how the stack is LIFO and ESP always points to the most recently pushed value.

Function Calls: CALL, RET, and Return Addresses

CALL and RET rely entirely on the stack to remember where execution should resume. CALL pushes the address of the instruction immediately following itself onto the stack and then jumps to the target label; RET pops that saved return address off the stack and jumps back to it. Functions commonly begin with PUSH ebp followed by MOV ebp, esp to establish a stable frame pointer, since ESP itself will shift as the function pushes further data, while EBP stays fixed for reliably addressing parameters and locals throughout the function body.

🏏

Cricket analogy: When a substitute fielder comes on, the ground marks exactly where the injured player will return to resume mirrors how CALL automatically pushes the return address onto the stack so RET knows exactly where to resume execution.

nasm
; A simple function using a standard stack frame
section .text
global _start

add_numbers:
    push ebp                ; save caller's base pointer
    mov  ebp, esp            ; establish new frame base
    sub  esp, 4               ; reserve 4 bytes for a local variable

    mov  eax, [ebp+8]         ; first argument
    add  eax, [ebp+12]        ; second argument
    mov  [ebp-4], eax         ; store result in local variable
    mov  eax, [ebp-4]         ; return value in eax

    mov  esp, ebp              ; deallocate locals
    pop  ebp                    ; restore caller's base pointer
    ret                          ; pop return address into EIP

_start:
    push 20                    ; push second argument
    push 10                    ; push first argument
    call add_numbers
    add  esp, 8                 ; caller cleans up (cdecl convention)
    mov  ebx, eax
    mov  eax, 1
    int  0x80

Local Variables and the Stack Frame

Local variables that a function needs during its execution but doesn't want to keep in registers are allocated by subtracting their combined size from ESP -- for example, SUB esp, 16 carves out 16 bytes of scratch space -- and accessed via negative offsets from EBP, such as [ebp-4]. This space is deallocated instantly when the function's epilogue runs MOV esp, ebp (restoring ESP to EBP's value) followed by POP ebp; NASM's ENTER and LEAVE instructions can perform the equivalent prologue and epilogue in a single instruction each, though discrete instructions are often faster on modern CPUs.

🏏

Cricket analogy: Reserving a specific patch of the outfield exclusively for a fielding drill during practice, distinct from the main pitch, mirrors SUB esp, 16 carving out 16 bytes of stack space for a function's local variables.

The prologue push ebp / mov ebp, esp and epilogue mov esp, ebp / pop ebp can be replaced by the single instructions ENTER n, 0 and LEAVE respectively, which many compilers avoid in optimized code because the discrete instructions are typically faster on modern CPUs despite ENTER/LEAVE being more compact.

Calling Conventions and Stack Cleanup

Calling conventions define who is responsible for cleaning up the stack after a function call. Under cdecl, the caller pushes arguments and is responsible for restoring ESP afterward, typically with ADD esp, N -- this flexibility is why cdecl supports variable-argument functions like printf, since the caller always knows exactly how many bytes it pushed. Under stdcall, the callee instead cleans up its own arguments as part of its RET N instruction. On modern x86-64 ABIs, the stack must additionally be kept 16-byte aligned before a CALL, since SIMD instructions operating on stack-resident data require that alignment.

🏏

Cricket analogy: Whether the fielding side or the batting side resets the field markers after an over depends on the match's specific rules, just as cdecl requires the caller to clean up pushed arguments with ADD esp, N after CALL, while stdcall makes the callee clean up via RET N.

Mixing calling conventions -- for example, calling a stdcall function (which cleans up its own arguments via RET N) as if it were cdecl (where the caller cleans up with ADD esp, N) -- cleans the stack twice, leaving ESP pointing at the wrong location for the next instruction and typically crashing the program soon after, often far from the actual mismatched call site, which makes it hard to debug.

  • The stack grows toward lower memory addresses; PUSH decrements ESP, POP increments it.
  • CALL automatically pushes the return address before jumping; RET pops it back into EIP.
  • A standard stack frame is set up with PUSH ebp; MOV ebp, esp and torn down with MOV esp, ebp; POP ebp.
  • Local variables are allocated by subtracting from ESP and accessed via negative offsets from EBP.
  • cdecl requires the caller to clean up pushed arguments after CALL; stdcall makes the callee clean up via RET N.
  • ENTER and LEAVE are compact alternatives to the manual prologue/epilogue sequence.
  • Mismatched calling conventions or unbalanced PUSH/POP corrupt the stack pointer and typically crash the program.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#AssemblyLanguageStudyNotes#TheStackInAssembly#Stack#Assembly#Function#Calls#DataStructures#StudyNotes#SkillVeris

Frequently Asked Questions

21 categories · pick one to explore

Where can I get free study notes for programming and tech subjects?
SkillVeris offers completely free study notes covering programming and tech subjects, with no signup fees or paywalls. The notes are structured by course and topic, written for quick understanding, and enriched with the Learn Through Hobbies analogy method, so you can revise concepts through cricket, music, gaming, cooking and more.
Are SkillVeris study notes good for exam revision?
Yes, the study notes are designed for efficient revision: each topic answers its heading immediately, keeps explanations concise, and links to related glossary terms and cheat sheets. Students preparing for university exams or certification tests use them as quick revision notes because they distil concepts without the padding of full textbooks.
What subjects do the free study notes cover?
The study notes span the platform's main domains, including AI and machine learning, Python and programming, web development, DevOps, cloud, security and databases. Coverage mirrors the 37 live courses, so notes exist for the topics you are actually studying, and new note sets are added as courses launch.
How are SkillVeris study notes different from regular textbooks?
The notes are answer-first, concise and free, whereas textbooks are long and often expensive. Each section explains one concept directly, then reinforces it through selectable hobby analogies like cricket or cooking. Notes also cross-link to the glossary, blog and cheat sheets, letting you jump to related material instantly instead of flipping pages.
Can I use the developer study material without creating an account?
The study notes are free to access, and SkillVeris does not charge anything for its developer study material at any point. Browsing notes is straightforward from the Study Notes section, and if you want progress tracking, certificates and AI Mentor conversations tied to your learning, a free account unlocks those extras.
Do the study notes explain concepts with analogies?
Yes, this is a signature SkillVeris feature. Study notes use the Learn Through Hobbies method, explaining technical concepts through analogies from twelve domains including cricket, music, gaming, photography, travel, movies, fitness, chess, cooking, finance, business and sports. You can switch the analogy domain instantly to whichever hobby makes the concept click.
Are the revision notes suitable for last-minute exam preparation?
Yes, revision notes on SkillVeris work well for last-minute preparation because every section states the answer in its first sentences, so skimming is genuinely effective. Pair them with the relevant cheat sheet for formulas and syntax, and use the glossary for any unfamiliar term you meet while cramming.
Is there free study material for AI and machine learning?
Yes, SkillVeris provides free study notes across its AI and ML catalogue, covering Python for AI, deep learning frameworks like PyTorch and TensorFlow, Hugging Face Transformers, Large Language Models, RAG, AI agents and MLOps. All of it is free, making it a strong resource for Indian students and global learners alike.
Can beginners understand the study notes, or are they for experts?
Beginners can absolutely use them. The notes are written in plain language, define terms as they appear, and lean on hobby analogies to make abstract ideas concrete. Difficulty scales with the underlying course level, so beginner-course notes stay gentle while advanced-course notes go deeper, and the glossary supports you throughout.
How do study notes connect with SkillVeris courses?
Study notes are organised by course and topic, so they map directly to the structured courses and their 24–40-lesson curriculum. Many learners study a lesson first, then use the matching notes for revision before module assessments and the final exam, where 80 percent is required to pass and earn the certificate.
Are there study notes for Python specifically?
Yes, Python is well covered through notes tied to the Python-focused courses, including Python for AI and ML. Topics span fundamentals through applied machine learning usage. You can reinforce the notes with Python practice in Code Lab, which runs code in your browser with no installation required.
Do the study notes include code examples?
Yes, study notes include code examples wherever a concept is best shown in code, alongside explanations, key points and analogies. Reading a snippet in the notes and then reproducing it yourself in Code Lab is an effective loop, since Code Lab lets you run code in the browser across six languages.
How often is new study material added to SkillVeris?
Study material grows alongside the course catalogue. Whenever new courses join the platform's 37 live courses, matching study notes, glossary entries and cheat sheets are added so the resources stay in sync. Existing notes are also refined over time, so it is worth revisiting topics you studied earlier.
Can I use SkillVeris notes to prepare for technical interviews?
Yes, the notes make excellent interview revision because they compress each concept into direct, answer-first explanations, which mirrors how you should answer interview questions. Combine them with the SkillVeris interview questions feature, which includes readiness scoring, to test whether your revision has actually made you interview-ready.
Are the study notes mobile-friendly for studying on the go?
Yes, the study notes are built to load fast and read comfortably on mobile devices, so you can revise during a commute or between classes. Sections are short and answer-first, which suits small screens, and analogy switching works on mobile too, letting you study anywhere without carrying books.
What is the difference between study notes and cheat sheets?
Study notes explain concepts in depth with context, examples and analogies, making them ideal for learning and revision. Cheat sheets are compact quick-reference summaries of syntax, commands and key facts, ideal once you already understand a topic. Most learners study the notes first, then keep the cheat sheet handy while coding.
Do study notes help if I am stuck on a course lesson?
Yes, reading the matching study notes often clarifies a lesson because the same concept is explained from a different angle, frequently with a different analogy. If you are still stuck, ask the AI Mentor, which answers 24/7 at Quick, Detailed or Deep-dive depth until the idea genuinely makes sense.
Is there free study material for DevOps and cloud topics?
Yes, SkillVeris carries free study notes for DevOps and cloud topics as part of its coverage across 37 live courses. The material suits learners following the DevOps Engineer or Cloud Engineer paths, and it links to related glossary terms and cheat sheets so you can revise the whole toolchain in one place.
Can school or college students in India use these notes for projects?
Yes, students across India and worldwide use SkillVeris notes for coursework, projects and exam preparation, and everything is free, which matters for student budgets. The notes explain concepts clearly enough to cite in project reports, and Code Lab lets you prototype the project code directly in your browser.
How should I combine study notes with other SkillVeris resources?
A proven loop: learn from a course lesson, revise with the matching study notes, look up unfamiliar terms in the glossary, keep the cheat sheet open while practising in Code Lab, and quiz yourself with interview questions. The AI Mentor fills any remaining gaps 24/7, at whatever depth you need.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse