0Pricing
Assembly Language & x86 Low-Level Systems Programming · 课时

调用栈基础

探索调用栈的原理和结构,以及它如何用于管理函数调用和局部数据。

调用栈基础 是 CoddyKit 上的免费 Assembly Language & x86 Low-Level Systems Programming 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Assembly Language & x86 Low-Level Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「调用栈基础」课时是免费的吗?

是的 — 「调用栈基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Assembly Language & x86 Low-Level Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。

「调用栈基础」这节课中我会学到什么?

探索调用栈的原理和结构,以及它如何用于管理函数调用和局部数据。 你通过在浏览器中直接运行的动手代码来练习 Assembly Language & x86 Low-Level Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Assembly Language & x86 Low-Level Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Assembly Language & x86 Low-Level Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「调用栈基础」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Assembly Language & x86 Low-Level Systems Programming 课中编写并运行代码吗?

能。每节 Assembly Language & x86 Low-Level Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 调用栈基础
  2. 定义与调用过程
  3. 传递参数与返回值
  4. 栈帧与局部变量
← 返回 Assembly Language & x86 Low-Level Systems Programming