Deep Dive into Function Call Implementation: From Assembly to CPU Registers
This article thoroughly explains low‑level function call implementation, covering Intel and AT&T assembly syntax, the evolution of CPU registers from 16‑bit to 64‑bit x86, caller‑ and callee‑saved conventions, stack‑based to register‑based parameter passing, stack frame setup, and assembly representations of control structures.
This article provides a comprehensive explanation of how function calls are implemented at the底层 (low-level) in programming languages. The content begins with an overview of assembly language, covering both Intel and AT&T syntax formats, and then delves into CPU register architectures across different generations: 80x86 (16-bit), x86 (32-bit), and x86-64 (64-bit).
The article explains in detail the general-purpose registers in each architecture, including their naming conventions, specific purposes (such as rax for return values, rsp for stack pointer, rbp for frame pointer), and the distinction between Caller-saved and Callee-saved registers. It also covers the evolution of parameter passing methods - from stack-based in x86 to register-based (rdi, rsi, rdx, rcx, r8, r9) in x86-64 architecture.
Function call mechanics are explained through the lens of stack frame structure: how parameters are pushed onto the stack (from right to left), how the call instruction saves return address and jumps to the function, and how the function prologue (push rbp; mov rbp, rsp) sets up the new stack frame. The article demonstrates these concepts with actual assembly code examples using GCC 12.1 with -O0 optimization.
Additionally, the article covers control structures (if, for, while) at the assembly level, showing how they are implemented using comparison (cmp) and jump (jle, jl, jmp) instructions based on CPU flag registers. The author emphasizes that understanding these底层 mechanisms helps developers write more efficient code and better understand high-level programming concepts.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.