说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C9-ARM64/index.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
9. ARM Assembly
本章介绍 ARM 第 8 版应用处理器配置(ARMv8-A)体系结构的 A64 ISA,它是所有运行 Linux 操作系统的 ARM 计算机上使用的最新 ARM ISA。回想一下,指令集架构(ISA)定义了机器级程序的指令集合和二进制编码。要运行本章的示例,你需要一台装有 ARMv8-A 处理器并安装了 64 位操作系统的机器。本章的示例使用的是运行 64 位 Ubuntu Mate 操作系统的树莓派 3B+。注意,2016 年以来发布的每一款树莓派都可以使用 A64 ISA。不过,截至本书写作时,Raspberry Pi OS(树莓派的默认操作系统)仍然是 32 位的。
In this chapter, we cover the ARM version 8 application profile (ARMv8-A) architecture A64 ISA, the latest ARM ISA that is in use on all Linux OS ARM computers. 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 ARMv8-A processor with a 64-bit operating system installed. The examples in this chapter use a Raspberry Pi 3B+ running the 64-bit Ubuntu Mate operating system. Note that every Raspberry Pi released since 2016 can use the A64 ISA. However, Raspberry Pi OS (the default Raspberry Pi operating system) is still 32-bit as of this writing.
现代更新(截至 2026-11)· 树莓派系统已有 64 位正式版
原文说 Raspberry Pi OS 仍是 32 位的。2022 年,树莓派官方正式推出了 64 位 Raspberry Pi OS:官方公告指出,自 2016 年的树莓派 3 起旗舰产品就能运行完整的 64 位系统,此前官方系统为了兼容一直基于 32 位 Raspbian 构建;改用 64 位后,可以让单个进程使用 8GB 树莓派 4 的全部内存,许多闭源应用也只提供 arm64 版本。如今在树莓派上跟着本章操作,直接装 64 位官方系统即可,uname -m 会输出 aarch64。Raspberry Pi:Raspberry Pi OS (64-bit)
读者可以运行 uname -m 命令,确认自己的系统装的是 64 位操作系统。装有 64 位操作系统的系统会输出:
Readers can confirm that they have a 64-bit version of the OS on their system by running the
uname -mcommand. A system with a 64-bit operating system will output the following:
译注 · Apple Silicon Mac
在 Apple Silicon Mac 上,uname -m 输出的是 arm64(Apple 对 AArch64 的叫法),本章的 C 程序可以直接在 Mac 上编译、运行和反汇编,这是全书三种汇编里最方便上手的一种。不过 macOS 遵循 Apple 自己的 ARM64 ABI,与本章所用的 Linux 有几处差别:符号名前多一个下划线(_main、_printf);寄存器 x18 被平台保留;可变参数函数(如 printf)的可变参数一律通过栈传递,而不是像 Linux 那样放在 w1、x2 等寄存器里。译者在 M 系列 Mac 上用 clang -O0 -S 编译下一节的 adder2 程序,adder2 本身的汇编与书中几乎一样,但调用 printf 前是先 str x8, [x9] 把 x 存到栈上。需要与书中完全一致的输出时,可以用 clang -S -O0 -target aarch64-linux-gnu 生成 Linux 风格的汇编来对照。调试器方面,macOS 上用 LLDB 代替 GDB(见第 3 章的现代更新)。依据:Apple Developer:Writing ARM64 code for Apple platforms
1 | $ uname -m |
虽然可以用 ARM 的 GNU 工具链交叉编译工具在 Intel 机器上构建 ARM 二进制文件,但你无法在 x86 系统上直接运行 ARM 二进制文件。有兴趣直接在自己的笔记本电脑上学习 ARM 汇编的读者,可以试试 QEMU,它可以模拟(emulate)一个 ARM 系统。模拟器与虚拟机的不同之处在于,它还会模拟另一个系统的硬件。
Although it is possible to build ARM binaries on Intel machines using ARM’s GNU toolchain cross-compilation tools, you cannot run ARM binaries directly on a x86 system. Readers interested in learning about ARM assembly directly on their laptops are encouraged to explore QEMU, which can emulate an ARM system. Emulators differ from virtual machines in that they also simulate the hardware of another system.
另一种选择是使用亚马逊最近推出的 EC2 A1 实例。每个实例都让用户能使用一颗遵循 ARMv8-A 规范的 64 位 Graviton 处理器。
Another alternative is to use one of Amazon’s recently released EC2 A1 instances. Each instance gives users access to a 64-bit Graviton processor, which follows the ARMv8-A specification.
不过要记住,编译器生成的具体汇编指令在很大程度上受操作系统和具体机器体系结构的影响。因此,在 AWS 实例上或通过 QEMU 模拟生成的汇编,可能与本章所示的示例略有不同。
Keep in mind, however, that the specific assembly instructions produced by a compiler are highly influenced by the operating system and precise machine architecture. Therefore, the assembly produced on AWS instances or through QEMU emulation may differ slightly from the examples shown in this chapter.
原书旁注 · RISC 与 ARM 处理器(RISC and ARM processors)
多年来,复杂指令集计算机(CISC)体系结构一直主导着个人计算机和服务器市场。常见的 CISC 体系结构包括 Intel 和 AMD 的处理器。然而,由于移动计算领域的需求,精简指令集计算机(RISC)体系结构在过去十年里势头渐盛。ARM(意为 Acorn RISC machine,Acorn 精简指令集机器)是 RISC 体系结构的一个例子,RISC-V 和 MIPS 也是。RISC 体系结构的处理器能效高,可以延长电池续航,因此对移动计算尤其有吸引力。近年来,ARM 和其他 RISC 处理器开始进军服务器和高性能计算(HPC)市场。例如,日本的富岳(Fugaku)超级计算机使用的就是 ARM 处理器,它是截至 2020 年世界上最快的超级计算机。
For many years, complex instruction set computer (CISC) architectures dominated the personal computing and server markets. Common examples of CISC architectures include Intel and AMD processors. However, reduced instruction set computer (RISC) architectures gained momentum over the past decade due to demand from the mobile computing sector. ARM (which stands for Acorn RISC machine) is an example of a RISC architecture, along with RISC-V and MIPS. RISC architectures are especially attractive to mobile computing due to the energy efficiency of their processors, which prolongs battery life. In recent years, ARM and other RISC processors have begun making headway in the server and high performance computing (HPC) markets. For example, Japan’s Fugaku supercomputer, the fastest in the world as of 2020, uses ARM processors.
现代更新(截至 2026-11)· ARM 在超算和个人电脑上的进展
- 超级计算机:富岳在 2026 年 6 月的 TOP500 榜单上排名第 9。ARM 架构的 CPU 仍活跃在榜单前列,例如第 5 名的 JUPITER Booster(德国于利希)和第 10 名的 Alps(瑞士)都使用 NVIDIA 基于 ARM 的 Grace CPU(GH200 超级芯片),其中 JUPITER Booster 是欧洲第一台百亿亿次(Exascale)系统。TOP500:List - June 2026;TOP500:Highlights - June 2026
- 个人电脑:2020 年 11 月,Apple 发布 M1 芯片,开始把 Mac 从 Intel x86 处理器迁移到自研的 ARM 架构芯片,ARM 由此进入主流笔记本和台式机。Apple Newsroom:Apple unleashes M1