0Pricing
Assembly Language & x86 Low-Level Systems Programming · Leçon

Principes fondamentaux de la pile d’appels

Découvrez les principes de la pile d’appels, sa structure et son utilisation pour gérer les appels de fonctions et les données locales.

Principes fondamentaux de la pile d’appels est une leçon Assembly Language & x86 Low-Level Systems Programming gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Assembly Language & x86 Low-Level Systems Programming, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Assembly Language & x86 Low-Level Systems Programming comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

Meet the Call Stack

Every time your program calls a function or procedure, it uses a special area of memory called the call stack. Think of it like a stack of plates in a cafeteria. You can only add or remove plates from the top.

The call stack is crucial for managing function calls, local variables, and remembering where to return to after a function finishes.

LIFO: Last In, First Out

The call stack operates on a LIFO principle: Last In, First Out. This means the last item added to the stack is always the first one to be removed.

  • When a function is called, its data is 'pushed' onto the stack.
  • When it returns, its data is 'popped' off.
  • This ensures proper order for nested calls.

Stack Pointers: ESP & EBP

Two main registers are essential for managing the stack in x86 assembly:

  • ESP (Stack Pointer): Always points to the top of the stack, the very last item pushed. The stack grows downwards (towards lower memory addresses).
  • EBP (Base Pointer): Points to a fixed location within the current stack frame, helping locate local variables and function arguments.

We'll see how they work together to organize data.

Adding Data with PUSH

The PUSH instruction adds data to the top of the stack. When you PUSH a value (e.g., a 32-bit register):

  • The ESP register is first decremented by the size of the data (4 bytes for a 32-bit value).
  • Then, the value is written to the memory location that ESP now points to.

It's like placing a new plate on top of the stack, which makes the stack 'taller' and its top move 'down'.

PUSH in Action

Observe how PUSH changes the stack pointer and stores values. In this example, assume ESP initially points to 0x100 (a high memory address). The stack grows downwards.

section .text
  global _start

_start:
  ; Assume ESP initially points to 0x100
  ; Stack grows downwards (towards lower addresses)

  mov eax, 0x10   ; Load value 10 (hex) into EAX
  push eax        ; ESP becomes 0xFC, memory at [0xFC] = 0x10

  mov ebx, 0x20   ; Load value 20 (hex) into EBX
  push ebx        ; ESP becomes 0xF8, memory at [0xF8] = 0x20

  ; At this point:
  ; ESP = 0xF8
  ; Memory at 0xF8 contains 0x20
  ; Memory at 0xFC contains 0x10

  ; Exit the program cleanly
  mov eax, 1      ; sys_exit system call number
  xor ebx, ebx    ; exit code 0
  int 0x80

Removing Data with POP

The POP instruction removes data from the top of the stack and places it into a specified register or memory location. When you POP a value:

  • The value at the memory location currently pointed to by ESP is read.
  • Then, the ESP register is incremented by the size of the data (e.g., 4 bytes).

This effectively 'removes' the top item and moves the pointer 'up', making the stack 'shorter'.

PUSH & POP Example

Let's see PUSH and POP working in sequence. Notice how ESP returns to its original position after an equal number of pushes and pops.

section .text
  global _start

_start:
  ; Assume ESP starts at some address (e.g., 0x100)

  mov eax, 50     ; Load 50 into EAX
  push eax        ; Push EAX onto stack. ESP -= 4. [ESP] = 50

  mov ebx, 100    ; Load 100 into EBX
  push ebx        ; Push EBX onto stack. ESP -= 4. [ESP] = 100

  ; Stack now has 100 at top, then 50.
  ; ESP is pointing to the 100.

  pop ecx         ; Pop top of stack into ECX. ESP += 4. ECX = 100
  pop edx         ; Pop next item into EDX. ESP += 4. EDX = 50

  ; After pops, ECX is 100, EDX is 50.
  ; ESP is back to its initial position before the pushes.

  ; Exit
  mov eax, 1
  xor ebx, ebx
  int 0x80

Understanding Stack Frames

When a function (or procedure) is called, a dedicated region on the stack, called a stack frame (or activation record), is created for it. This frame holds all the data related to that specific function call.

A stack frame typically includes:

  • Function arguments passed to it
  • Local variables used within the function
  • The return address (where the program should jump back to after the function finishes)
  • Saved register values from the calling function

EBP: The Frame Pointer

The EBP (Base Pointer) register is primarily used to manage stack frames. Unlike ESP, which constantly moves as data is pushed and popped, EBP usually remains fixed at the base of the current function's stack frame.

This stability makes it easy to access local variables and arguments using fixed offsets from EBP (e.g., [EBP-4] for a local variable, [EBP+8] for an argument), even if ESP changes due to pushes/pops within the function.

Stack Check

Consider the following assembly code snippet:

  mov eax, 10
  push eax
  mov ebx, 20
  push ebx
  pop ecx
  pop edx

What will be the final value in the EDX register after this code executes?

Recap: Call Stack Fundamentals

You've learned the basics of the x86 call stack!

  • The call stack is a LIFO data structure fundamental for managing function calls.
  • ESP (Stack Pointer) always points to the top of the stack and moves with PUSH/POP operations.
  • EBP (Base Pointer) is used to define a stable stack frame for a function, helping access local data and arguments.
  • PUSH decrements ESP then stores the value; POP retrieves the value then increments ESP.

Next, we'll build on this by learning how to define and call our own procedures, utilizing these stack concepts.

Questions Fréquemment Posées

La leçon « Principes fondamentaux de la pile d’appels » est-elle gratuite ?

Oui — le texte complet de « Principes fondamentaux de la pile d’appels » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Assembly Language & x86 Low-Level Systems Programming, passe à CoddyKit PRO. Le cours Assembly Language & x86 Low-Level Systems Programming comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Principes fondamentaux de la pile d’appels » ?

Découvrez les principes de la pile d’appels, sa structure et son utilisation pour gérer les appels de fonctions et les données locales. Tu pratiques Assembly Language & x86 Low-Level Systems Programming avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Assembly Language & x86 Low-Level Systems Programming ?

Aucune expérience préalable n'est requise. Assembly Language & x86 Low-Level Systems Programming sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.

Combien de temps prend la leçon « Principes fondamentaux de la pile d’appels » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Assembly Language & x86 Low-Level Systems Programming ?

Oui. Chaque leçon Assembly Language & x86 Low-Level Systems Programming inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Principes fondamentaux de la pile d’appels
  2. Définition et appel de procédures
  3. Transmission des arguments et valeurs de retour
  4. Cadres de pile et variables locales
← Retour à Assembly Language & x86 Low-Level Systems Programming