# 程序计数器

程序计数器（program counter, PC）是处理器中的一个寄存器，在存储程序计算机中，它保存着正在执行的指令或下一条将要执行的指令的内存地址。指令执行的每一步都始于用PC从内存中取出一条指令，终于将PC更新为下一条指令的地址；分支、跳转、调用、异常和中断指令会将PC改为顺序下一地址之外的值。<sup>[1](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c9/c9s1/)</sup><sup> • </sup><sup>[2](https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/CompOrg/fetchDecode.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心功能 | 保存下一条将要执行指令在主存中的地址；控制单元每个周期从该地址取指<sup>[1](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c9/c9s1/)</sup> |
| 顺序更新 | 在32位Beta等定长指令集架构中，大多数指令的下一个PC为PC + 4<sup>[1](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c9/c9s1/)</sup> |
| 改变PC的因素 | 分支、跳转、调用、返回、异常和中断；对于分支，下一个PC取决于操作码、当前PC值（用于偏移量计算）以及被测试的寄存器值<sup>[3](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)</sup> |
| 猜错的代价 | 朴素的PC+4推测会使有效CPI升高约10%；英特尔的Nehalem（2009年）分支预测正确率为95%至99%<sup>[3](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)</sup> |
| 对软件的可见性 | 在Arm Cortex-M33上作为寄存器R15暴露，在经典ARM中为r15；在A64中不能作为具名寄存器访问<sup>[4](https://developer.arm.com/documentation/100230/0004/functional-description/programmers-model/processor-core-registers-summary)</sup><sup> • </sup><sup>[5](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/the-a64-isa-and-compilers)</sup> |
| 在Java虚拟机中 | 每个JVM线程有自己的pc寄存器；线程执行原生方法时该寄存器值未定义<sup>[6](https://docs.oracle.com/en/java/javase/26/docs/specs/jvms/jvms-2.html)</sup> |

## 在指令周期中的角色

冯·诺依曼存储程序机器以循环方式执行指令：从PC指定的内存地址加载指令，将其各字段解码为控制信号，在数据通路上执行操作，最后将PC更新为下一条指令的地址。<sup>[1](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c9/c9s1/)</sup> 待取指令的地址就存放在这个隐藏寄存器中，它不属于普通指令可见的通用寄存器堆。<sup>[2](https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/CompOrg/fetchDecode.html)</sup>

**顺序执行。** 当指令占据固定大小的字时，推进PC就是一个常量加法。在Beta教学指令集中，每条指令都是32位，因此下一条指令通常位于PC + 4处。<sup>[1](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c9/c9s1/)</sup> RISC-V中也有同样的规则：对于除分支或跳转以外的任何指令，下一次取指地址都是该指令自身位置加4。<sup>[7](https://docs.riscv.org/reference/isa/unpriv/mm-formal.html)</sup>

**控制转移。** 分支和跳转指令向PC加载一个新值，于是处理器从新地址（称为分支目标）取指，而不是顺序的下一条指令。分支可以是有条件的——仅在指定条件成立时跳转——也可以是无条件的。<sup>[8](https://www.cs.umd.edu/users/meesh/411/CA-online/chapter/instruction-set-architecture/index.html)</sup> 对于条件分支，加载到PC中的值取决于三件事：操作码、当前PC值（参与偏移量计算）以及分支所测试的寄存器值。异常也会改变PC。<sup>[3](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)</sup>

## 指令集架构如何暴露和使用PC

不同指令集对PC的处理方式不同，这些选择会影响编译器、流水线和调用约定。

**显式寄存器与隐藏状态。** 经典ARM将程序计数器作为通用寄存器r15暴露，这使一些编程技巧成为可能，但也使编译器和复杂流水线设计变得复杂；A64指令集取消了这种直接访问，这也使返回预测更容易，并简化了ABI。<sup>[5](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/the-a64-isa-and-compilers)</sup> 在Arm Cortex-M33上，PC仍是寄存器R15，保存当前程序地址。<sup>[4](https://developer.arm.com/documentation/100230/0004/functional-description/programmers-model/processor-core-registers-summary)</sup> 在英特尔处理器上，对应的寄存器在32位模式下为EIP，在64位模式下为RIP。<sup>[9](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdf)</sup>

**PC相对寻址。** 由于PC保存的是代码地址，指令集架构将其用作位置无关代码的基址。RISC-V的无条件跳转全部是PC相对的：JAL将按2字节倍数计的有符号偏移量加到跳转指令自身的地址上，目标范围为±1 MiB，并将下一条指令的地址（pc + 4）存入寄存器rd作为返回地址。JALR从寄存器加12位立即数计算目标地址，清除最低有效位，同时将pc + 4写入rd。<sup>[10](https://docs.riscv.org/reference/isa/v20240411/unpriv/rv32.html)</sup> A64也扩展了PC相对寻址：PC相对的加载和存储偏移量达到±1 MB，条件分支±1 MB，无条件分支±128 MB。<sup>[5](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/the-a64-isa-and-compilers)</sup>

**调用、返回与异常。** 调用必须记录返回位置。英特尔的CALL指令将当前EIP（即CALL之后那条指令的地址）作为返回指令指针压入栈中；RET随后将其弹出回EIP。<sup>[9](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdf)</sup> 当Armv8-A处理器发生异常时，硬件自动将返回地址保存到所进入异常级别的ELR_ELx中，ERET指令在返回时从ELR_ELn恢复PC。<sup>[11](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Exception%20model.pdf)</sup> 中断过程与子程序调用的区别在于：它由信号而非指令发起，其入口地址由硬件决定，并且通常保存完整的CPU状态而不仅仅是程序计数器。<sup>[8](https://www.cs.umd.edu/users/meesh/411/CA-online/chapter/instruction-set-architecture/index.html)</sup>

**虚拟机。** 这一概念也延续到抽象机器中。Java虚拟机为每个线程分配自己的pc寄存器；线程在任一时刻执行一个当前方法，如果该方法不是原生方法，pc寄存器保存正在执行的JVM指令的地址。如果方法是原生的，pc寄存器的值未定义。该寄存器的宽度足以容纳平台上的returnAddress或原生指针。<sup>[6](https://docs.oracle.com/en/java/javase/26/docs/specs/jvms/jvms-2.html)</sup>

## 流水线与下一地址预测

在流水线处理器中，取指阶段每个周期都需要一个下一PC预测，而此时当前指令尚未解析完成。对于大多数指令，PC + 4是正确的，但对于跳转和已跳转的分支，真正的下一PC可能要等到指令读取了寄存器或执行完毕后才能确定，这就产生了控制冒险。<sup>[3](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)</sup><sup> • </sup><sup>[12](https://csg.csail.mit.edu/6.175/labs/lab5-riscv-intro.html)</sup>

**推测及其代价。** 简单流水线推测下一条指令在PC + 4处，如果分支实际跳转则取消已取的指令。在五级流水线中，这种朴素策略对跳转和已跳转分支来说是错误的，模拟轨迹显示推测错误会使有效CPI升高约10%。在给出的例子中，暂停取指阶段的代价更高，对于三指令循环会使有效CPI升高33%。<sup>[3](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)</sup>

**分支目标缓冲区。** 高性能取指单元使用分支目标缓冲区（BTB）从当前PC预测下一地址，BTB是一张记录先前使用过的、并非简单PC + 4的下一地址的表，以产生这些地址的PC为索引。预测器在每次分支解析后更新，执行阶段同时保留实际PC和预测PC，以便检测预测错误。<sup>[12](https://csg.csail.mit.edu/6.175/labs/lab5-riscv-intro.html)</sup> 方向预测器增加了历史信息：英特尔的Nehalem（2009年）根据每条分支的过去行为进行预测，通过负偏移量识别向后循环分支，并利用分支之间的相关性，达到95%至99%的正确推测率。<sup>[3](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)</sup> RISC-V以隐式方式辅助返回预测：JAL仅在rd为x1或x5时将返回地址压入返回地址栈，JALR的压栈/出栈行为则通过这两个链接寄存器中哪些用作操作数来编码。<sup>[10](https://docs.riscv.org/reference/isa/v20240411/unpriv/rv32.html)</sup>

**延迟槽。** 一种指令集架构层面的替代方案是将分支或跳转之后的指令定义为始终执行，即分支延迟槽，这使得PC + 4的猜测在构造上就是正确的。RISC-V选择了相反的设计：其控制转移指令没有架构上可见的延迟槽。<sup>[3](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)</sup><sup> • </sup><sup>[10](https://docs.riscv.org/reference/isa/v20240411/unpriv/rv32.html)</sup>

## 调试与工具

PC值也是将机器码关联回源代码的关键。在DWARF调试格式中，语句程序状态机携带一个地址寄存器，其定义为编译器生成的机器指令所对应的程序计数器值，同时还有文件、行和列寄存器，使调试器能将任何正在执行的指令映射到源代码位置。<sup>[13](https://refspecs.linuxfoundation.org/elf/TIS1.1.pdf)</sup>

## References

1. [9.1 Annotated Slides, Computation Structures, MIT OpenCourseWare](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c9/c9s1/)
2. [Fetch, Execute, Decode, CMSC 311, University of Maryland](https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/CompOrg/fetchDecode.html)
3. [15.1 Annotated Slides, Computation Structures, MIT OpenCourseWare](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c15/c15s1/)
4. [Arm Cortex-M33 Processor Technical Reference Manual, Processor Core Registers Summary](https://developer.arm.com/documentation/100230/0004/functional-description/programmers-model/processor-core-registers-summary)
5. [The A64 ISA and compilers, Arm Community blog](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/the-a64-isa-and-compilers)
6. [Chapter 2, The Structure of the Java Virtual Machine, JVM Specification](https://docs.oracle.com/en/java/javase/26/docs/specs/jvms/jvms-2.html)
7. [Formal Memory Model Specifications, Version 0.1, RISC-V](https://docs.riscv.org/reference/isa/unpriv/mm-formal.html)
8. [Instruction Set Architecture, CMSC 411 Computer Architecture, University of Maryland](https://www.cs.umd.edu/users/meesh/411/CA-online/chapter/instruction-set-architecture/index.html)
9. [Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 1](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdf)
10. [RV32I Base Integer Instruction Set, Version 2.1, RISC-V](https://docs.riscv.org/reference/isa/v20240411/unpriv/rv32.html)
11. [Exception model, AArch64 Programmer's Guides, Arm](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Exception%20model.pdf)
12. [MIT 6.175 Lab 5: RISC-V Introduction, Multi-Cycle and Two-Stage Pipeline](https://csg.csail.mit.edu/6.175/labs/lab5-riscv-intro.html)
13. [DWARF Debugging Information Format, TIS Portable Formats Specification 1.1](https://refspecs.linuxfoundation.org/elf/TIS1.1.pdf)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Computer architecture theory › CPU internal structure

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/program-counter

English original: https://www.edgepedia.org/program-counter
