# 汇编语言

**汇编语言**是一种符号化编程语言，其中每条语句通常对应特定处理器的一条机器指令：程序员用称为助记符的名称代替数字来书写操作码和标识内存位置，再由**汇编器**程序将该文本翻译成机器语言<sup>[1](https://www.computerhistory.org/revolution/early-computer-companies/5/117)</sup>。与高级语言的编译器不同，汇编器通常保留底层机器码每行一条指令的结构<sup>[1](https://www.computerhistory.org/revolution/early-computer-companies/5/117)</sup>。

| 关键事实 | 详情 |
|---|---|
| 本质 | 处理器机器码的符号化表示，由汇编器按每条指令一条语句进行翻译<sup>[1](https://www.computerhistory.org/revolution/early-computer-companies/5/117)</sup> |
| 最早里程碑 | 剑桥大学的EDSAC于1949年5月6日完成首次计算，运行速度为每秒714次操作；其Initial Orders汇编器接受符号化指令<sup>[2](https://www.computerhistory.org/tdih/may/6/)</sup><sup> • </sup><sup>[3](https://www.cs.tufts.edu/~nr/cs257/archive/david-salomon/asl.pdf)</sup> |
| 语法 | 典型的源代码行包含四个字段：标号、指令、操作数和注释<sup>[4](https://www.nasm.us/doc/nasm03.html)</sup> |
| 架构依赖性 | 每种汇编语言属于一种指令集架构；例如Armv8-A支持A32、T32和A64指令集<sup>[5](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Armv8-A%20Instruction%20Set%20Architecture.pdf?revision=ebf53406-04fd-4c67-a485-1b329febfb3e)</sup> |
| 现代工具链中的角色 | GNU汇编器`as`主要用于汇编GNU C编译器的输出，供链接器使用<sup>[6](https://sourceware.org/binutils/docs/as.html)</sup> |
| 性能定位 | 现代编译器已缩小差距；1997年的一款DSP编译器生成的代码仍比非常优秀的汇编程序员慢约一倍<sup>[7](https://users.ece.utexas.edu/~bevans/professional/DSParchitecture/compiler.html)</sup> |

## 汇编与机器码的关系

程序存储在计算机内存中时，是一串编码指令的零一序列，直接编写该序列既繁琐又易出错。汇编语言是编程超越原始机器码的最早进步：它让人们用名称而非数字来书写操作码和内存位置，由汇编器完成翻译<sup>[1](https://www.computerhistory.org/revolution/early-computer-companies/5/117)</sup>。下一步进展由Fortran等语言的编译器实现，即将一条类英语语句翻译成所需数量的机器指令，取消了逐一对应关系<sup>[1](https://www.computerhistory.org/revolution/early-computer-companies/5/117)</sup>。

指令集本身由处理器的架构文档定义。英特尔64和IA-32软件开发手册的第二卷合起来专门用于完整指令集参考，描述每条指令的格式并提供参考页<sup>[8](https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html?wapkw=ALL%28software+developer%27s+manual%29)</sup>。Arm为每个架构版本发布架构参考手册，给出每条指令在内存中的编码、参数、描述其功能的伪代码以及使用限制<sup>[5](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Armv8-A%20Instruction%20Set%20Architecture.pdf?revision=ebf53406-04fd-4c67-a485-1b329febfb3e)</sup>。

## 源代码语法

大多数汇编器共享通用的行结构。x86平台的Netwide Assembler（NASM）将每个源代码行描述为四个字段的某种组合：标号、带操作数的指令，以及由分号引入的注释；大多数字段是可选的<sup>[4](https://www.nasm.us/doc/nasm03.html)</sup>。操作数可以是直接命名的寄存器（如`ax`或`ebx`）、有效地址、常量或表达式<sup>[4](https://www.nasm.us/doc/nasm03.html)</sup>。

除真实机器指令外，汇编器还接受**伪指令**，它们是汇编器而非CPU的产物。NASM的伪指令包括`SECTION`，用于将代码导向`.text`、`.data`和`.bss`等命名段；`BITS 16`、`BITS 32`或`BITS 64`，用于切换生成代码所针对的处理器模式；以及`EXTERN`和`GLOBAL`，用于在模块间导入和导出符号<sup>[9](https://man.archlinux.org/man/nasm.1.en)</sup>。`DB`和`RESB`等伪指令将数据字节放入指令字段，尽管它们并非真实机器指令<sup>[4](https://www.nasm.us/doc/nasm03.html)</sup>。

同一台机器的汇编器之间语法并不统一。GNU汇编器手册指出，680x0汇编语言语法存在若干不兼容版本，且`as`并不总是使用与同一架构的其他汇编器相同的语法<sup>[6](https://sourceware.org/binutils/docs/as.html)</sup>。NASM同样不使用`gas`风格的语法，即寄存器名必须以`%`符号为前缀<sup>[4](https://www.nasm.us/doc/nasm03.html)</sup>。

## 汇编器

汇编器读取符号化源文本并生成机器码，通常输出为供链接器使用的目标文件。GNU汇编器`as`实际上是一个汇编器家族，每种架构一个，共享目标文件格式、大多数伪指令和语法约定；它主要用于汇编GNU C编译器`gcc`的输出，供链接器`ld`使用，且与早期汇编器不同，它设计为一遍扫描源文件即可完成汇编<sup>[6](https://sourceware.org/binutils/docs/as.html)</sup>。

编译器也会将汇编作为中间步骤输出，而C编译器直接向程序员暴露汇编。GCC的基本`asm`语句将汇编指令的字面字符串逐字传递到汇编输出文件；GCC不解析这些指令，不知道它们的含义，也不知道对内存或寄存器的修改等副作用<sup>[10](https://gcc.gnu.org/onlinedocs/gcc/Basic-Asm.html)</sup>。基本`asm`语句可用于C函数内部或文件作用域，在那里它们可以发出汇编器伪指令、定义汇编语言宏，或用汇编语言编写整个函数<sup>[10](https://gcc.gnu.org/onlinedocs/gcc/Basic-Asm.html)</sup>。

## 早期历史

第一批存储程序计算机几乎立即引入了符号化编程。由[莫里斯·威尔克斯](https://www.edgepedia.org/zh/maurice-wilkes)在剑桥大学数学实验室建造的EDSAC于1949年5月6日完成首次计算，运行速度为每秒714次操作<sup>[2](https://www.computerhistory.org/tdih/may/6/)</sup>。其汇编器称为Initial Orders，实现在由旋转电话选择器构成的只读存储器中，接受由单字母助记符、十进制地址和第三个字段组成的符号化指令，第三个字段使12个程序员预设常量之一在汇编时加到地址上<sup>[3](https://www.cs.tufts.edu/~nr/cs257/archive/david-salomon/asl.pdf)</sup>。

威尔克斯还率先提出了标号的使用（他称之为浮动地址），率先使用了早期形式的宏（他称之为合成指令），并率先开发了子程序库<sup>[3](https://www.cs.tufts.edu/~nr/cs257/archive/david-salomon/asl.pdf)</sup>。

后来的机器完善了这一模型。IBM 650于1953年左右首次交付，其汇编器与当今汇编器非常相似：SOAP（符号优化与汇编程序）以传统方式进行符号汇编，可能是第一个这样做的汇编器。其主要特征是优化计算下一条指令的地址，因为650将程序存储在磁鼓上，每条指令都必须放置在这样的位置：前一条指令一执行完毕，它便恰好处于读头下方<sup>[3](https://www.cs.tufts.edu/~nr/cs257/archive/david-salomon/asl.pdf)</sup>。[IBM 704](https://www.edgepedia.org/zh/ibm-704)于1956年首次交付，具有浮点硬件和变址寄存器等特性，同年获得了其第一个汇编器UASAP-1<sup>[3](https://www.cs.tufts.edu/~nr/cs257/archive/david-salomon/asl.pdf)</sup>。

## 汇编与编译器性能

手写汇编是否优于编译代码取决于编译器。1990年之前，[数字信号处理器](https://www.edgepedia.org/zh/digital-signal-processor)的C编译器需求极低，性能进展甚微；到1997年，免费可用的Motorola 56000 C编译器生成的代码仍比非常优秀的汇编程序员慢约一倍。到1992年，手机和打印机等复杂嵌入式产品搭载了操作系统，而从C语言与操作系统交互比从汇编语言更容易<sup>[7](https://users.ece.utexas.edu/~bevans/professional/DSParchitecture/compiler.html)</sup>。

在现代嵌入式开发中，情况进一步变化。制造商销售的设备拥有充足的程序存储器，而编译器（通常由芯片制造商自己构建）可靠地应用优化技巧；程序员可能知道节省时钟周期的技巧，但编译器知道许多自己的技巧，且从不忘记使用它们。汇编在嵌入式工作中仍然有价值，主要在于理解处理器在最底层实际做什么，以及在C代码行为不符合预期时阅读反汇编<sup>[11](https://www.allaboutcircuits.com/technical-articles/is-assembly-language-relevant-for-embedded-software-development/)</sup>。Arm的指南同样指出，即使不日常编写汇编的开发者，也从理解指令集能做什么以及编译器如何使用它中受益，这有助于编写高效代码和调试编译器输出；汇编在首阶段引导软件和某些底层内核活动等领域仍然重要<sup>[5](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Armv8-A%20Instruction%20Set%20Architecture.pdf?revision=ebf53406-04fd-4c67-a485-1b329febfb3e)</sup>。

## 当今的指令集

汇编语言随指令集架构的发展而演变，而这些架构也在不断扩展。Armv8-A支持三种指令集：A32和T32，用于AArch32执行状态，也称为ARM和Thumb；以及A64，用于AArch64，是定长32位指令集，“64”指执行状态而非内存中指令的大小<sup>[5](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Armv8-A%20Instruction%20Set%20Architecture.pdf?revision=ebf53406-04fd-4c67-a485-1b329febfb3e)</sup>。Apple文档说明了如何编写遵循其平台支持的应用程序二进制接口的ARM64汇编，以便手写代码与编译器生成的代码正确互操作<sup>[12](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms?changes=_1)</sup>。RISC-V是一种基于既定RISC原则的开放标准指令集架构，在公共库中发布其批准规范<sup>[13](https://docs.riscv.org/)</sup>。

指令编码本身可能很复杂。英特尔的指令集参考区分了操作数形式，如`r32`（双字通用寄存器之一，包括64位模式下使用REX.R时的R8D至R15D）和`r/m16`（具有16位操作数大小属性的指令的字寄存器或内存操作数）<sup>[14](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-2a-manual.pdf)</sup>。

## 当前研究中的汇编

汇编仍然是自动化研究的活跃目标。2025年，研究人员探索了大语言模型能否使汇编代码更快，因为其对执行的底层控制允许高级语言难以表达的加速；他们使用[近端策略优化](https://www.edgepedia.org/zh/proximal-policy-optimization)，在8,072个真实世界程序的基准上训练模型，奖励结合了通过测试用例检查的功能正确性和与行业标准编译器gcc -O3相比的执行速度<sup>[15](https://arxiv.org/abs/2505.11480)</sup>。更早的工作以不同角色使用语言模型进行编译器优化，将未优化的汇编作为输入，输出编译器选项列表以最佳优化程序<sup>[16](https://arxiv.org/abs/2309.07062)</sup>。

在另一个极端，底层实验继续探索指令集本身。一位硬件研究者的“CPU去优化”项目寻找最慢的x86机器指令，并报告了单条指令198,002,498,236个周期的记录，约62秒；同一开发者此前创建了movfuscator，一个仅使用mov指令的C编译器<sup>[17](https://www.tomshardware.com/pc-components/cpus/hardware-researcher-spins-up-cpu-deoptimization-project-to-find-the-slowest-machine-code-worst-offender-takes-198-billion-cycles-to-execute)</sup>。

## References

1. [Higher Level Languages, Computer History Museum](https://www.computerhistory.org/revolution/early-computer-companies/5/117)
2. [May 6, 1949: British Computer EDSAC Performs First Calculation, Computer History Museum](https://www.computerhistory.org/tdih/may/6/)
3. [David Salomon, A Short History of Assemblers and Loaders](https://www.cs.tufts.edu/~nr/cs257/archive/david-salomon/asl.pdf)
4. [NASM: The Netwide Assembler, Chapter 3: The NASM Language](https://www.nasm.us/doc/nasm03.html)
5. [Armv8-A Instruction Set Architecture, Arm Limited](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Armv8-A%20Instruction%20Set%20Architecture.pdf?revision=ebf53406-04fd-4c67-a485-1b329febfb3e)
6. [Using as, the GNU Assembler manual, sourceware.org](https://sourceware.org/binutils/docs/as.html)
7. [For a DSP processor, is a compiler important?, University of Texas at Austin](https://users.ece.utexas.edu/~bevans/professional/DSParchitecture/compiler.html)
8. [Manuals for Intel 64 and IA-32 Architectures, Intel Corporation](https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html?wapkw=ALL%28software+developer%27s+manual%29)
9. [nasm(1) manual page, Arch Linux](https://man.archlinux.org/man/nasm.1.en)
10. [Basic Asm, Using the GNU Compiler Collection (GCC)](https://gcc.gnu.org/onlinedocs/gcc/Basic-Asm.html)
11. [Is Assembly Language Still Relevant for Embedded Software Development?, All About Circuits](https://www.allaboutcircuits.com/technical-articles/is-assembly-language-relevant-for-embedded-software-development/)
12. [Writing ARM64 code for Apple platforms, Apple Developer Documentation](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms?changes=_1)
13. [RISC-V Ratified Specifications Library](https://docs.riscv.org/)
14. [Intel 64 and IA-32 Architectures Software Developer's Manual Volume 2A: Instruction Set Reference, A-L](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-2a-manual.pdf)
15. [Improving Assembly Code Performance with Large Language Models via Reinforcement Learning, arXiv:2505.11480](https://arxiv.org/abs/2505.11480)
16. [Large Language Models for Compiler Optimization, arXiv:2309.07062](https://arxiv.org/abs/2309.07062)
17. [CPU deoptimization project finds the slowest x86 instruction, Tom's Hardware](https://www.tomshardware.com/pc-components/cpus/hardware-researcher-spins-up-cpu-deoptimization-project-to-find-the-slowest-machine-code-worst-offender-takes-198-billion-cycles-to-execute)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › C-family and imperative languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/assembly-language

English original: https://www.edgepedia.org/assembly-language
