说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C10-asm_takeaways/index.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
10. Key Assembly Takeaways
本书的这一部分介绍了汇编的基础知识。虽然如今大多数人用高级程序设计语言编程,但理解汇编能让程序员更好地理解自己的程序和编译器在做什么。对于为嵌入式系统和其他资源受限环境设计软件的人,以及从事漏洞分析的人来说,汇编知识也是必不可少的。本书汇编部分的各章涵盖了 64 位 Intel 汇编(x86-64)、32 位 Intel 汇编(IA32)和 64 位 ARM 汇编(ARMv8-A)。
This part of the book has covered the basics of assembly. While most people today code in a high-level programming language, an understanding of assembly increases a programmer’s ability to better understand what their programs and compilers are doing. A knowledge of assembly is also essential for anyone who designs software for embedded systems and other resource-constrained environments, and for people who work in vulnerability analysis. The chapters contained in the assembly part of this book have spanned 64-bit Intel assembly (x86-64), 32-bit Intel Assembly (IA32), and 64-bit ARM assembly (ARMv8-A).
共同特征
Common Features
无论学的是哪一种具体的汇编语言,所有汇编语言都有一些值得强调的共同特征。
Regardless of the specific assembly language that one learns, there are some common features to all assembly languages worth highlighting.
ISA 定义了汇编语言
一台机器上可用的具体汇编语言由这台机器的指令集架构(ISA)定义。要识别某台 Linux 机器的底层体系结构,使用
uname -m命令。
The ISA defines the assembly language
The specific assembly language available on a machine is defined by the instruction set architecture (ISA) of that machine. To identify the underlying architecture of a particular Linux machine, use the
uname -mcommand.
寄存器存放数据
每种 ISA 都定义了一组基本的寄存器,CPU 用它们来处理数据。有些寄存器是通用的,可以存放任何类型的数据;另一些寄存器是专用的,通常由编译器保留作特定用途(例如栈指针、基址指针)。通用寄存器可读可写,而有些专用寄存器是只读的(例如指令指针)。
Registers hold data
Every ISA defines a set of basic registers that the CPU uses to operate on data. Some registers are general purpose and can hold any kind of data, whereas other registers are special purpose and are typically reserved by the compiler for specific uses (e.g., stack pointer, base pointer). While general purpose registers are readable and writable, some special purposes registers are read-only (e.g., the instruction pointer).
指令规定了 CPU 能做什么
ISA 还定义了一系列指令,规定 CPU 可以执行的操作。每条指令都有一个指明它做什么的操作码(opcode),以及一个或多个指明所用数据的操作数(operand)。ISA 规定了用于数据移动、算术运算、条件判断、分支和内存访问的具体指令。这些核心指令常常组合起来,用于表示数组、结构体和矩阵等更复杂的数据结构。
Instructions specify what the CPU can do
The ISA also defines a series of instructions that specify operations that the CPU can perform. Each instruction has an operation code (opcode) that specifies what the instruction does, and one or more operands that specifies the data to be used. The ISA documents specific instructions for data movement, arithmetic operations, conditionals, branches, and accessing memory. These core instructions are often combined to represent more complex data structures like arrays, structs, and matrices.
程序栈存放与特定函数相关的局部变量
编译器使用进程虚拟地址空间中的栈(即栈内存)来存放临时数据。在所有现代系统上,程序栈都向低内存地址方向增长。编译器用栈指针和基址指针来指定一个栈帧,它界定了栈中与某个特定函数或过程相关的区域。每次函数调用都会在栈上添加一个新的栈帧,界定与被调用函数相关的栈区域。函数返回时,与它相关的栈帧被从栈中移除。通常,函数结束时栈指针和基址指针会恢复为原来的值。虽然这种簿记工作暗示局部变量已从栈中「清理」掉了,但旧数据通常会以垃圾值的形式残留下来,有时会导致难以调试的行为。恶意攻击者还可以利用对 ISA 栈簿记方式的了解,制造缓冲区溢出等危险的安全攻击。
The program stack holds local variables associated with a particular function
The compiler uses the stack (or stack memory) of a process’s virtual address space to store temporary data. On all modern systems, the program stack grows toward lower memory addresses. The compiler uses the stack pointer and base pointer to specify a stack frame that defines the area of the stack that is associated with a particular function or procedure. A new stack frame is added to the stack with every function call and defines the stack region associated with the callee function. The stack frame associated with a particular function is removed from the stack when that function returns. Typically, the stack and base pointers return to their original values when a function ends. While this bit of bookkeeping suggests that local variables are “cleaned” from the stack, old data usually sticks around in the form of junk values, which can sometimes lead to hard-to-debug behaviors. Malicious actors can also use knowledge of an ISA’s stack bookkeeping to create dangerous security exploits, like buffer overflows.
安全
虽然所有系统都容易受到缓冲区溢出之类的安全漏洞的影响,但相对较新的 ARMv8-A 有机会吸取影响较老的 Intel 体系结构的一些安全缺陷的教训。不过,第一道防线永远是程序员。即使有额外的保护措施,也没有哪种 ISA 能完全免受潜在安全缺陷的影响。用 C 编程时,程序员应尽可能使用长度说明,以减少因越界而导致安全漏洞的可能性(见表 1)。
Security
While all systems are vulnerable to security vulnerabilities like buffer overflow, the relatively recent ARMv8-A has had the opportunity to learn from some of the security flaws that affected older Intel architectures. However, the first line of defense is always the programmer. Even with additional protections, no ISA is invulnerable to potential security flaws. When coding in C, programmers should use length specifiers whenever possible to reduce the chance of security vulnerabilities resulting from boundary overruns (see Table 1).
表 1. 带长度说明的 C 函数
Table 1. C functions with Length Specifiers
| 不要用: | 改用: |
|---|---|
gets(buf) |
fgets(buf, 12, stdin) |
scanf("%s", buf) |
scanf("%12s", buf) |
strcpy(buf2, buf) |
strncpy(buf2, buf, 12) |
strcat(buf2, buf) |
strncat(buf2, buf, 12) |
sprintf(buf, "%d", num) |
snprintf(buf, 12, "%d", num) |
Instead of: Use: gets(buf)fgets(buf, 12, stdin)scanf("%s", buf)scanf("%12s", buf)strcpy(buf2, buf)strncpy(buf2, buf, 12)strcat(buf2, buf)strncat(buf2, buf, 12)sprintf(buf, "%d", num)snprintf(buf, 12, "%d", num)
译注 · 表中有两处仍会溢出
- **
scanf("%12s", buf)应为%11s**:%s的宽度只限制读入的字符数,scanf还要在后面再写一个'\0',所以%12s最多写 13 字节,对 12 字节的buf正好越界 1 字节。译者用 clang 编译这一行,会得到警告:'scanf' may overflow; destination buffer in argument 2 has size 12, but the corresponding specifier may require size 13。fgets(buf, 12, stdin)和snprintf(buf, 12, ...)的 12 则是包含'\0'在内的缓冲区总大小,是对的。 strncat的第三个参数不是目标缓冲区的大小,而是最多从源串追加多少个字符;目标缓冲区至少要有strlen(dst) + strnlen(src, n) + 1字节(见 strncat(3))。若buf2中已有内容,strncat(buf2, buf, 12)照样会溢出,安全的上限应是sizeof(buf2) - strlen(buf2) - 1。strncpy(buf2, buf, 12)不会溢出,但当buf的长度不小于 12 时,它不会写入'\0',buf2将不是一个合法的字符串(参见第 2 章关于字符串函数的译注)。
延伸阅读
Further Reading
本书只让你初步领略了几种最流行的汇编语言。想更深入地理解汇编,建议查阅各个 ISA 的规范:
This book offers just a taste of some of the most popular assembly languages in use. For more in-depth understanding of assembly, we encourage you to check out the ISA specifications:
现代更新(截至 2026-11)· 延伸阅读的新地址,以及第四种值得了解的 ISA
- Intel 手册:原书链接所在的 software.intel.com 页面已迁到 Intel 开发者网站。Intel 64 与 IA-32 体系结构软件开发者手册仍免费提供,分为合订本、四卷本和十卷本,三套内容完全相同;截至 2026 年 10 月的页面,各卷 PDF 为第 093 版,其中第 2A–2D 卷是按字母排序的指令参考,查某条指令的确切语义时最常用。Intel:Intel 64 and IA-32 Architectures Software Developer Manuals
- RISC-V:除了本书讲的 x86 和 ARM,开放的 RISC-V 指令集也值得一看。它的规范在 GitHub 上公开维护,分为第一卷「非特权架构」(普通程序可用的指令,相当于本书各章讲的内容)、第二卷「特权架构」(操作系统使用的部分)和第三卷「Profiles」,各卷前言注明了哪些版本已由 RISC-V International 正式批准。GitHub:riscv/riscv-isa-manual
对学习 32 位汇编感兴趣的读者,下面这些免费资源也可能有用:
The following free resources may also be useful for those who are interested in learning 32-bit assembly:
- IA32 编程网络旁注(Randal Bryant、David O’Hallaron 著)
- IA32 Programming Web Aside (Randal Bryant and David O’Hallaron)
- 32 位 ARM 汇编(Azeria Labs)
- 32-bit ARM Assembly (Azeria Labs)
下面这些书也对汇编有深入的讨论;它们不是免费的,但都是很好的延伸阅读资源:
The following books also feature in-depth discussions of assembly; these aren’t free, but they are great resources for further reading:
- Intel 系统:Computer Systems: A Programmer’s Perspective(中译本《深入理解计算机系统》,Randal Bryant、David O’Hallaron 著)
- Intel systems: Computer Systems: A Programmer’s Perspective (Randal Bryant and David O’Hallaron)
- ARMv8:Computer Organization and Design(中译本《计算机组成与设计:硬件/软件接口》,David Patterson、John Hennessy 著)
- ARMv8: Computer Organization and Design (David Patterson and John Hennessy)