第 88 / 187 篇

8. 32 位 x86 汇编(IA32)

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C8-IA32/index.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

8. 32-bit x86 Assembly (IA32)

本章介绍 32 位 Intel 体系结构(IA32)的指令集架构。回想一下,指令集架构(ISA)定义了机器级程序的指令集合和二进制编码。要运行本章的示例,你需要一台装有 x86 处理器的机器,或者一个能生成 32 位可执行文件的编译器。「x86」这个术语常被用作 IA32 体系结构的同义词。x86 及其 64 位变体 x86-64 在现代计算机中无处不在。

In this chapter, we cover the Intel Architecture 32-bit (IA32) instruction set architecture. Recall that an instruction set architecture (or ISA) defines the set of instructions and binary encodings of a machine-level program. To run the examples in this chapter, you will need access to a machine with an x86 processor or a compiler that can create 32-bit executables. The term “x86” is often used synonymously with the IA32 architecture. The x86, and its 64-bit variant x86-64, are ubiquitous in modern computers.

如今很少有机器还使用 32 位处理器;2007 年以来生产的大多数 Intel 和 AMD 系统都是 64 位处理器。要检查你的处理器是什么类型,使用 uname -m 命令:

Very few modern machines have 32-bit processors; most Intel and AMD systems produced since 2007 have 64-bit processors. To check what type of processor you have, use the uname -m command:

1
2
$ uname -m
i686

如果 uname -m 返回 i686 或 i386,你的系统是 32 位处理器。而如果返回 x86_64,你的系统就是较新的 64 位处理器。请注意,由于 x86-64 是较早的 IA32 ISA 的扩展,几乎所有 64 位系统都包含一个 32 位子系统,可以执行 32 位可执行文件。

If typing uname -m returns either i686 or i386, your system has a 32-bit processor. However, if the uname -m command returns x86_64, your system has a newer 64-bit processor. Please note that since x86-64 is an extension of the older IA32 ISA, virtually all 64-bit systems contain a 32-bit subsystem that allows the execution of 32-bit executables.

现代更新(截至 2026-11)· 32 位 x86 正在退场

「几乎所有 64 位系统都能执行 32 位程序」在硬件层面仍大体成立,但操作系统正陆续放弃 32 位:Apple 从 macOS Catalina 10.15 起不再兼容 32 位应用;Windows 11 的系统要求是兼容的 64 位处理器;Arch Linux 从 2017 年 11 月起不再支持 i686。学习 IA32 仍有价值(大量旧代码、教材和安全研究资料都基于它),但要运行本章示例,最方便的是 64 位 Linux 加上 gcc -m32 所需的 multilib 软件包。Apple:32-bit app compatibility with macOS;Microsoft:Windows 11 Specs and System Requirements;Arch Linux:Phasing out i686 support

如果你使用的是 64 位 Linux 系统,有时需要安装额外的软件包,才能像本章这样生成 32 位可执行文件。例如,在 Ubuntu 机器上,你需要安装 32 位开发库和其他软件包,给 GCC 加上交叉编译功能:

If you have a 64-bit Linux system, additional packages are sometimes required to allow users to create 32-bit executables, like we will be doing in this chapter. For example, on an Ubuntu machine you will need to install 32-bit development libraries and additional packages to augment GCC with cross-compiling features:

译注

Apple Silicon Mac(uname -m 输出 arm64)无法运行 32 位 x86 程序。只想对照阅读汇编的话,可以用 clang -S -O0 -target i386-linux-gnu 文件.c 让 Clang 交叉生成 IA32 汇编(只能看,不能链接运行);要真正运行,需要 x86 Linux 虚拟机或云服务器并安装上面的软件包。

1
sudo apt-get install libc6-dev-i386 gcc-multilib

原书旁注 · x86 语法的分支(x86 Syntax Branches)

x86 体系结构通常遵循两种不同的语法分支之一。UNIX 机器通常使用 AT&T 语法,因为 UNIX 是在 AT&T 贝尔实验室开发的,对应的汇编器是 GNU 汇编器(GAS)。由于本书的大多数示例使用 GCC,本章介绍的是 AT&T 语法。Windows 机器通常使用 Intel 语法,微软的宏汇编器(MASM)使用的就是这种语法。Netwide 汇编器(NASM)是 Linux 上使用 Intel 语法的汇编器的一个例子。两种语法孰优孰劣的争论,是这个领域的「圣战」之一。不过,熟悉两种语法都是有价值的,因为程序员在不同场合可能会遇到其中任何一种。

x86 architectures typically follow one of two different syntax branches. UNIX machines commonly use the AT&T syntax, given that UNIX was developed at AT&T Bell Labs. The corresponding assembler is GNU Assembler (GAS). Since we use GCC for most examples in this book, we cover AT&T syntax in this chapter. Windows machines commonly use Intel syntax, which is used by Microsoft’s Macro Assembler (MASM). The Netwide Assembler (NASM) is an example of a Linux assembler that uses Intel syntax. The argument regarding the superiority of one syntax over the other is one of the “holy wars” of the discipline. However, there is value in being familiar with both syntaxes, as a programmer may encounter either in various circumstances.