学习日志
从 ISA 到 CPU:一条加法指令的执行过程
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. Now let’s move down from the ISA layer into the CPU’s internal hardware implementation. First, the CPU fetches the instruction at the address held in the PC and places it in the IR. The CU decodes the instruction and generates control signals, determining what operation the ALU performs and whether the PC advances by 4 or jumps to a specified address, among other things. For example, here is a simplified process for the CPU executing add x3, x1, x2: 1. Fetch the instruction at the address specified by the PC and place it in the IR. 2. The CU reads the instruction in the IR and generates control signals: “Select x1 and x2 as the data sources, and select the ALU’s addition function.” 3. Write the result back to register x3. 4. Set PC=PC+4 and continue executing instructions.
接上回,上回有讲到,编译器按照 ISA 编译源代码,选择实现方案。编译的本质,是将高级语言描述的复杂操作,转化为目标 CPU 能够执行的基本机器指令的组合。这些指令通过算术运算、逻辑运算、数据访问(store/load)、控制流转移(条件分支、跳转)等基本操作,共同实现程序的功能。最后 CPU 按照 ISA 规定的语义执行这些指令。 现在从 ISA 层向下走,进入 CPU 内部的硬件实现层。 首先,CPU 在 PC 中的地址取出指令放到 IR 中,CU 译码指令并产生控制信号,决定 ALU 做什么运算、PC 选择 +4,还是跳转到指定的地址等等。 比如:CPU 执行 add x3, x1, x2 的简化过程 1. 按照 PC 指定的地址取出指令,放到 IR 中 2. CU 读取 IR 中的指令并产生控制信号“选择 x1、x2 作为数据来源,选择 ALU 加法功能” 3. 最后把结果写回 x3 寄存器 4. PC=PC+4,继续执行指令