Inside the CPU: Illustrated Guide to Registers, ALU, and Control Unit
This article walks through the internal structure of a CPU—explaining the roles of registers, the arithmetic‑logic unit, the control unit, cache hierarchy, bus interface, clock, and pipeline—using diagrams, code snippets, and concrete examples to illustrate how each component works together.
CPU Core Components
Registers act as a fast pocket of storage, the ALU is the calculator that performs arithmetic and logic operations, and the Control Unit (CU) is the commander that coordinates all parts.
Register File
What Is a Register?
寄存器 = CPU内部的高速存储单元 = 比内存快100倍 = 但容量极小(只有几十到几百个)
类比: 内存 = 仓库(很大但存取慢) 寄存器 = 口袋(很小但存取快)Common Register Types
// 1. 通用寄存器 - 什么都能存
// x86: AX, BX, CX, DX, SI, DI, BP, SP
// ARM: R0‑R15
// 2. 程序计数器 PC - 下一条指令在哪
// 又叫 IP (Instruction Pointer)
// 3. 指令寄存器 IR - 当前执行的指令
// 4. 状态寄存器 - 记录运算结果状态
// 又叫 FLAGS (e.g., Zero, Carry, Overflow)
// 5. 栈指针 SP - 栈的顶部Register Layout Example
┌──────────────────────────────────────┐
│ 寄存器文件 │
│ ┌────────┐ ┌────────┐ ┌────────┐ │
│ │ R0 │ │ R1 │ │ R2 │ │
│ │ 0x3A │ │ 0xFF │ │ 0x00 │ │
│ └────────┘ └────────┘ └────────┘ │
│ ┌────────┐ ┌────────┐ ┌────────┐ │
│ │ PC │ │ IR │ │ SP │ │
│ │0x100 │ │0xA55A │ │0xFFE0 │ │
│ └────────┘ └────────┘ └────────┘ │
│ ┌────────┐ ┌────────┐ │
│ │ PSW │ │ ... │ │
│ │Z=1,C=0│ │ │ │
│ └────────┘ └────────┘ │
└──────────────────────────────────────┘Arithmetic Logic Unit (ALU)
What Is the ALU?
ALU = Arithmetic Logic Unit
功能:
1. 算术运算:加、减、乘、除
2. 逻辑运算:与、或、非、异或
3. 移位运算:左移、右移
4. 比较运算:大于、小于、等于ALU Structure
┌────────────────────────────────────┐
│ ALU │
│ 输入A ─────→┐
│ 输入B ─────→├→ ┌────────────┐ │
│ 操作码 ───→├→ │ 运算单元 │ │
│ └─────┬──────┘ │
│ │ │
│ ↓ │
│ ┌─────────┐ │
│ │ 结果输出│ │
│ └─────────┘ │
│ ┌─────────┐ │
│ │ 状态标志│ │
│ └─────────┘ │
└────────────────────────────────────┘
状态标志:
- Z (Zero):结果为0
- C (Carry):进位/借位
- S (Sign):符号位
- O (Overflow):溢出ALU Operation Example
// 假设ALU执行加法:5 + 3
输入A = 00000101 (5的二进制)
输入B = 00000011 (3的二进制)
操作码 = ADD
ALU内部:
00000101 + 00000011 = 00001000 (8)
输出结果 = 00001000
状态标志 Z=0, C=0, O=0Control Unit (CU)
What Is the Control Unit?
控制单元 = CPU的"大脑"
= 指挥各部件协调工作
= 执行指令的"指挥官"
类比: ALU = 运动员
寄存器 = 器材
控制单元 = 教练Control Unit Workflow
1. 取指令(Fetch) 从内存读取下一条指令到 IR
2. 译码(Decode) 分析指令要做什么
3. 取操作数(Fetch Operand) 从寄存器或内存读取数据
4. 执行(Execute) 让 ALU 进行运算
5. 写回(Write Back) 把结果存到寄存器或内存Microprogrammed vs. Hard‑wired Control
1. 微程序控制
- 用微指令解释机器指令
- 灵活,易于修改
- 速度稍慢
- 早期 CPU 常用
2. 硬布线控制
- 用硬件电路直接实现
- 速度快,不灵活
- 现代 CPU 常用Bus Interface Unit (BIU)
┌────────────────────────────────────┐
│ CPU芯片 │
│ ┌─────────────────────────────┐ │
│ │ 总线接口单元 (BIU) │ │
│ │ - 地址总线驱动 │ │
│ │ - 数据总线收发 │ │
│ │ - 指令预取队列 │ │
│ │ - 段寄存器 │ │
│ └─────────────────────────────┘ │
│ ↑ ↓ │
│ 地址总线 数据总线 │
└────────────────────────────────────┘Cache
Structure
CPU缓存 = 数据的高速中转站
结构:
┌────────────────────────┐
│ CPU核心 │
│ L1缓存(指令) │
│ L1缓存(数据) │
│ ┌──────────────────┐ │
│ │ L2缓存 │ │
│ └──────────────────┘ │
│ ┌──────────────────┐ │
│ │ L3缓存 │ │
│ └──────────────────┘ │
└────────────────────────┘Characteristics
L1 – 32KB‑64KB, fastest, closest to core
L2 – 256KB‑1 MB, fast, middle distance
L3 – few MB‑dozens MB, slower, farthest from core
Clock and Pipeline
Clock
时钟 = CPU的"心跳"
= 每次脉冲执行一步操作
频率 = 1秒内的脉冲数 = GHz(十亿次/秒)
3.0 GHz = 每秒30亿个时钟周期
每个指令需要的周期数(CPI):
- 简单指令:1个周期
- 复杂指令:多个周期Five‑Stage Pipeline
五级流水线:
┌─────┬─────┬─────┬─────┬─────┐
│ IF │ ID │ EX │ MEM │ WB │
└─────┴─────┴─────┴─────┴─────┘
同一时刻并行处理 5 条指令的不同阶段,效率大幅提升!Full CPU Internal Diagram
┌─────────────────────────────────────────────────────────────┐
│ CPU芯片 │
│ ┌─────────────────────────────────────────────────┐ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ 寄存器组 │ │ 控制单元 │ │ 时钟 │ │ │
│ │ │ - 通用 │←──→│ - 译码 │←───│ 发生器 │ │ │
│ │ │ - PC/IR │ │ - 时序 │ │ │ │ │
│ │ │ - 状态 │ │ - 控制 │ │ │ │ │
│ │ └────┬─────┘ └────┬─────┘ └──────────┘ │ │
│ │ │ │ │ │
│ │ │ ┌─────┴─────┐ │ │
│ │ │ │ │ │ │
│ │ ↓ ↓ ↓ │ │
│ │ ┌───────────────────────┐ │ │
│ │ │ ALU │ │ │
│ │ │ - 算术运算 │ │ │
│ │ │ - 逻辑运算 │ │ │
│ │ │ - 移位运算 │ │ │
│ │ └─────────┬─────────┘ │ │
│ │ │ │ │
│ │ ┌──────┴──────┐ │ │
│ │ │ 状态标志 │ │ │
│ │ │ Z/C/S/O │ │ │
│ │ └─────────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│ ↑ ↓ │
│ 地址总线 数据总线 │
└─────────────────────────────────────────────────────┘Conclusion
The CPU’s three core blocks—register file, ALU, and control unit—operate together with auxiliary parts such as cache, clock, and bus interface. Execution follows the classic five‑stage pipeline: fetch, decode, fetch operand, execute, and write back, illustrating how a modern processor turns binary instructions into useful work.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Learning Made Simple
Learn IT: using simple language and everyday examples to study.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
