📚 Inside the CPU: An Experimental Approach to the Central Processing Unit | 中央处理器(CPU)工作原理实验
Every program we run, every calculation we make, and every video we watch is processed by the same tiny electronic engine: the Central Processing Unit (CPU). In this revision guide, we will not only describe the theory but also design simple hands-on experiments that simulate how the CPU works. By the end, you will be able to explain the fetch-decode-execute cycle, the role of registers, the arithmetic logic unit and the control unit with real confidence.
我们运行每一个程序、进行每一次计算、观看每一段视频,背后都由同一台微型电子引擎驱动——中央处理器(CPU)。在这篇复习指导中,我们不仅会讲解理论,还会设计简单易行的动手实验,模拟 CPU 的工作过程。学完本文后,你将能够自信地解释取指-译码-执行周期、寄存器的作用、算术逻辑单元以及控制单元。
1. The CPU at a Glance | 中央处理器概览
The central processing unit is the "brain" of the computer. It carries out instructions from a program stored in memory and coordinates the rest of the hardware. To understand that "brain", we run a simple experiment: we treat the CPU as a black box, feed it an instruction, and observe the output.
中央处理器是计算机的“大脑”。它执行内存中运行程序的指令,并协调其余硬件。为了理解这个“大脑”,我们做一个简单实验:把 CPU 当作一个黑盒,向它输入一条指令,再观察输出。
Our model CPU has three essential building blocks:
我们的模型 CPU 包含三个核心部件:
-
The Arithmetic Logic Unit (ALU) performs calculations such as addition, subtraction, AND, OR and comparison.
算术逻辑单元(ALU)负责执行加法、减法、与(AND)、或(OR)以及比较等运算。
-
The Control Unit (CU) decodes instructions and controls the flow of data between the CPU, memory and input/output devices.
控制单元(CU)负责译码指令,并控制 CPU、内存与输入/输出设备之间的数据流动。
-
Registers are internal storage locations inside the CPU that temporarily hold data, addresses or control values.
寄存器是 CPU 内部的存储单元,用于临时保存数据、地址或控制值。
Together, these components carry out the fundamental machine cycle, which we will simulate in the next section.
这些组件协同工作,完成计算机最基本的机器周期,我们将在下一节中模拟这一过程。
2. Experiment 1: The Fetch-Decode-Execute Cycle | 实验一:取指-译码-执行周期
In this experiment, we simulate the three-step cycle that processes one instruction. We will use a printed instruction list and a paper "register board". The cycle has three phases: Fetch, Decode and Execute.
在本实验中,我们模拟处理一条指令所需的三步周期。我们将使用一张打印好的指令表和一个纸质“寄存器板”。该周期分为三个阶段:取指(Fetch)、译码(Decode)和执行(Execute)。
Step A - Fetch: the address of the next instruction is taken from the Program Counter (PC). That address is copied into the Memory Address Register (MAR). The memory returns the instruction, which is placed in the Memory Data Register (MDR) and then moved to the Current Instruction Register (CIR).
步骤 A——取指:程序计数器(PC)保存下一条指令的地址。该地址被复制到内存地址寄存器(MAR)。内存把指令送回,放入内存数据寄存器(MDR),再移入当前指令寄存器(CIR)。
Step B - Decode: the control unit looks at the "opcode" part of the instruction to decide what operation must be performed.
步骤 B——译码:控制单元查看指令中的“操作码”部分,以决定需要执行什么操作。
Step C - Execute: the operation is performed, for example loading data or adding two values together.
步骤 C——执行:实际操作被执行,例如加载数据或将两个数值相加。
Let us simulate the instruction "LOAD R1, #15", which should put the value 15 into register R1. We record the contents of each key component after every phase.
我们模拟指令“LOAD R1, #15”,该指令将数值 15 装入寄存器 R1。我们记录每一步之后各个关键部件的状态。
| Phase | PC | MAR | MDR | CIR | R1 |
| Start | 100 | - | - | - | 0 |
| After Fetch | 101 | 100 | LOAD R1, #15 | LOAD R1, #15 | 0 |
| After Decode | 101 | 100 | LOAD R1, #15 | opcode = LOAD | 0 |
| After Execute | 101 | 100 | 15 | LOAD R1, #15 | 15 |
The cycle repeats for every instruction until the CPU reaches a HALT instruction. This experiment shows why the PC is updated during the fetch phase: it must point to the next instruction before the current one is executed.
该周期对每一条指令重复进行,直到 CPU 遇到 HALT(停止)指令。本实验还说明为什么 PC 要在取指阶段更新:它必须在当前指令执行之前指向下一条指令。
3. Experiment 2: Register Simulation | 实验二:寄存器模拟
All CPUs rely on a small set of special registers. In our model we use five of the most important ones, and we assign each one a clear job.
所有 CPU 都依赖一小批特殊寄存器。在我们的模型中,我们使用五个最重要的寄存器,并给每个寄存器分配明确的任务。
| Register | Full Name | Purpose |
