学习日志
从程序员的意图到 CPU 电路:ISA、控制通路与数据通路
Today’s questions: 1. From the programmer’s intent to machine instructions. 2. From machine instructions to actual circuit behavior.
Picking up from the previous entry: the compiler compiles source code according to the ISA and chooses an implementation. Compilation essentially turns the complex operations described in a high-level language into combinations of basic machine instructions that the target CPU can execute. Through basic operations such as arithmetic, logic, data access (store/load), and control-flow transfers (conditional branches and jumps), these instructions work together to implement the program’s functionality. The CPU then executes them according to the semantics defined by the ISA.
For example, suppose we write int c = a + b at the source-code level. Our intent is to add a and b and assign the result to c. If all three variables are stored in memory, the compiler may choose this implementation at the ISA / machine-instruction level: 1. Load: read a and b from memory into registers. 2. Compute: calculate a + b using an addition instruction. 3. Store: write the result into c in memory. The CPU then executes the machine instructions to implement the behavior of c = a + b. After processing by the compiler and build tools, we obtain machine code or an executable program that can run in the target environment.
Why can the compiler and build tools produce machine code or an executable that runs in the target environment? After all, they do not negotiate with the CPU about what kind of machine code or executable to produce. It works because the compiler and CPU must follow the same ISA specification: the compiler generates code according to it, and the CPU hardware implements the execution effects it defines.
A question may arise: why load a and b from memory into registers, rather than a, b, and c? But why would we need to read C from memory? Our goal is simply to evaluate a + b and assign the final result to C. What was C’s original value? It does not participate in the computation at all.
Thus, assuming all three variables are stored in memory, the CPU can perform these steps:
| Step | CPU operation | Result |
|---|---|---|
| 1 | Read a and b from memory | Registers t0=3, t1=5 |
| 2 | The ALU performs addition | Register t2=8 |
| 3 | Write t2 to the memory location corresponding to c | c=8 in memory |
This explains an important distinction: A variable named C in the C language does not mean that the CPU must contain a register named C. C variables belong to the source-code level, while registers belong to the ISA and CPU level. The compiler establishes the correspondence between them.
II. The compiler translates the source code 'int c = a + b' into 'add t2, t0, t1'. This specifies: 1. Read data from t0 and t1. 2. Perform integer addition. 3. Write the result to t2. The core effect defined by the ISA is: t2←t0 + t1 (keeping the appropriate bits according to the destination register’s width). The ISA requires that t2 = 8 after this instruction executes. It usually does not specify how many logic gates the CPU uses internally, the structure of the adder, or how many clock cycles it takes to complete the operation. If the ISA only specifies the result of execution, what makes the hardware inside the CPU actually produce that result?
Now let’s move down from the ISA layer into the CPU’s hardware implementation. Inside the CPU are two parts with different functions that must work together. Control Path: decides which data to use, which operation to perform, and where to send the result. CU is the traditional collective name for its core control logic. Data Path: actually transfers data, performs computations, and ensures the result. It includes registers, the ALU, multiplexers, and other components.
The key realization here is that the CU mainly generates control signals, while the data path performs the corresponding data operations according to those signals. This might make it sound as though the CU understands an instruction and then tells the ALU to perform addition. What actually happens is:

A machine instruction is a group of bits. Bits at different positions are assigned to describe different information. The CU’s decoding logic recognizes these bits. When the instruction bits enter the decoding and control circuits, the circuits generate the corresponding control signals according to their already-designed logical relationships.
Both the CU and ALU use predesigned logic circuits to produce corresponding output signals from input signals. The difference is that their outputs serve different purposes. What these components can and cannot do is designed by people, in order to achieve the goals we want.
If we add another layer—the physical world: When designing the components, we establish that the corresponding input information will produce the corresponding physical effects.
| Core question | Corresponding concept |
|---|---|
| 1. What do I want the computer to do? | Programming languages and their semantics |
| 2. How do we turn this behavior into operations a machine can execute? | The compiler and ISA |
| 3. What does a machine instruction actually require the CPU to do? | The ISA’s instruction semantics |
| 4. Why can the CPU actually perform these operations? | The CU, ALU, registers, data path, and digital circuits |
Engineers design and manufacture circuit structures to realize a particular input–output relationship. When the circuit operates, physical laws determine how it responds to input signals. The ALU does not need to understand the mathematical symbol +. Its logic-circuit structure ensures that, under the design conditions, the corresponding binary input signals produce output signals that follow the rules of addition. Design intent explains why we build it. Circuit structure and physical laws explain why it can work.
The ISA (Instruction Set Architecture) is the dividing line: above it is the world of software (with variables, functions, and the concepts of addition, subtraction, multiplication, and division); below it is the world of physical circuits (with no numbers as such, only voltage levels and the movement of electrons). Going from C to the CPU’s internal hardware means translating a “specification” on paper into “physical causality” in the real world.
今天需要解决: 1. 从程序员的意图到机器指令。 2. 从机器指令到实际电路行为。
接上回,上回有讲到,编译器按照 ISA 编译源代码,选择实现方案。编译的本质,是将高级语言描述的复杂操作,转化为目标 CPU 能够执行的基本机器指令的组合。这些指令通过算术运算、逻辑运算、数据访问(store/load)、控制流转移(条件分支、跳转)等基本操作,共同实现程序的功能。最后 CPU 按照 ISA 规定的语义执行这些指令。
比如:假设我们写下 int c = a + b(源代码层); 我们的意图是:把 a 和 b 相加,把结果赋给 c。 假设这三个变量都存放在内存中,那么编译器可以选择这样的实现方案(ISA 层·机器指令): 1. 加载:把 a、b 从内存读入寄存器。 2. 运算:通过加法指令计算 a + b。 3. 存储:把计算结果写入内存中的 c。 然后 CPU 执行机器指令,实现 c = a + b 的行为。 在编译器与构建工具的处理下最终能够得到一份能够在目标环境运行的机器代码或可执行程序。
为什么在编译器与构建工具的处理下,能够得到一份目标环境运行的机器代码或可执行程序呢?毕竟,编译器和构建工具并没有和 CPU 进行协商,应该制造出一份怎样的机器代码或可执行程序。是因为规定了编译器和 CPU 必须共同遵守 ISA 规范,编译器按它生成代码,CPU 硬件按它实现执行效果。
可能会产生一个疑问,为什么是把 a,b 从内存读入寄存器,而不是 a,b,c 从内存读入寄存器?但是为什么要把 C 从内存读出来呢?因为我们的目的只是执行 a + b,并把这个最终结果赋给 C,原来的 C 是什么?根本不参与计算。
因此,假设三个变量都存放在内存中,CPU 可以执行下面的步骤:
| 步骤 | CPU 执行的操作 | 结果 |
|---|---|---|
| 1 | 从内存读取 a、b | 寄存器 t0=3,t1=5 |
| 2 | ALU 执行加法 | 寄存器 t2=8 |
| 3 | 把 t2 写入 c 对应的内存位置 | 内存中的 c=8 |

其实这里就解释了一个重要的区别: C 语言中的变量 C,并不等于 CPU 内部必须存在一个叫 C 的寄存器。 C 变量属于源代码,寄存器属于 ISA 和 CPU 层,编译器负责建立两者之间的对应关系。
二、编译器将源代码 'int c = a + b' 编译成 'add t2, t0, t1' 它规定: 1. 从 t0、t1 读取数据 2. 执行整数加法 3. 把结果写到 t2 ISA 规定的核心效果就是:t2←t0 + t1(结果按目标寄存器位宽保留相应的比特) ISA 它的要求就是执行完这条指令之后必须满足 t2 = 8。至于 CPU 内部采用多少个逻辑门、使用什么结构的加法器、多少个时钟周期完成 ISA,通常并不规定。 既然 ISA 只规定执行结果,是谁让 CPU 内部的硬件真正产生这个结果?
现在从 ISA 层往下走,走进 CPU 内部的硬件实现层。 在 CPU 内部有两个功能不同、却必须互相配合的部分。 控制通路(Control Path):决定使用哪些数据,执行什么操作,把结果送往哪里,CU 是其核心控制逻辑的传统统称。 数据通路(Data Path):真正负责传送数据、进行运算、保证结果,包括寄存器、ALU、多路选择器等。

这里的关键顿悟是:CU 主要负责产生控制信号,数据通路负责按照这些信号完成相应的数据操作。 这里似乎会让你觉得 CU 看懂指令,然后通知 ALU 执行加法。 实际情况是:

机器指令是一组比特,不同位置的比特被约定用来描述不同的信息 CU 的译码逻辑识别这些比特,指令比特进入译码和控制电路后,电路按照已经设计好的逻辑关系产生对应的控制信号。
CU 和 ALU 都是通过预先设计好的逻辑电路,对输入信号产生对应的输出信号。区别是,它们的输出用于不同的目的。而这些组件能做什么、不能做什么,都是人为设计成这样的,为了达成我们想要的目标而设计成这样的。
如果还想加一层物理世界: 我们在设计组件的时候就约定好了,当组件输入对应信息时,就会产生对应的物理效果。
| 核心问题 | 对应概念 |
|---|---|
| 1. 我想让计算机做什么? | 编程语言及其语义 |
| 2. 如何把这种行为变成机器能执行的操作? | 编译器与 ISA |
| 3. 机器指令实际要求 CPU 做什么? | ISA 的指令语义 |
| 4. CPU 为什么真的能够完成这些操作? | CU、ALU、寄存器、数据通路和数字电路 |
工程师为了实现某种输入--输出关系,设计并制造相应的电路结构;电路工作时,由物理规律决定它对输入信号如何响应。 ALU 不需要理解数学符号 +,它的逻辑电路结构保证了,在符合设计条件时,输入对应的二进制信号,就会产生满足加法规则的输出信号。 设计意图解释了为什么要制造它,电路结构和物理规律解释了它为什么能够工作。
ISA(指令集架构)是分水岭:ISA 之上是软件与世界(有变量,有函数,有加减乘除的概念);ISA 之下是物理电路世界(根本没有数字,只有电平的高低和电子的流动)。 从 C 到 CPU 内部硬件,就是把纸面上的“规约”翻译成真实世界的“物理因果律”。