跳到主要内容

处理器基础

A-Level 计算机科学 · 第 4 主题

训练
讲义 词汇表
4.1

中央处理器(CPU)体系结构

大纲
Candidates should be able to: Notes and guidance
Show understanding of the basic Von Neumann model for a computer system and the stored program concept
Show understanding of the purpose and role of registers, including the difference between general purpose and special purpose registers Special purpose registers including: • Program Counter (PC) • Memory Data Register (MDR) • Memory Address Register (MAR) • The Accumulator (ACC) • Index Register (IX) • Current Instruction Register (CIR) • Status Register
Show understanding of the purpose and roles of the Arithmetic and Logic Unit (ALU), Control Unit (CU) and system clock, Immediate Access Store (IAS)
Show understanding of how data are transferred between various components of the computer system using the address bus, data bus and control bus
Show understanding of how factors contribute to the performance of the computer system Including: • processor type and number of cores • the bus widthclock speedcache memory
Understand how different ports provide connection to peripheral devices Including connection to: • Universal Serial Bus (USB) • High Definition Multimedia Interface (HDMI) • Video Graphics Array (VGA)
Describe the stages of the Fetch-Execute (F-E) cycle Describe and use 'register transfer' notation to describe the F-E cycle
Show understanding of the purpose of interrupts Including: • possible causes of interrupts • applications of interrupts • use of an Interrupt Service Routine (ISR) • when interrupts are detected during the fetch-execute cycle • how interrupts are handled

来源:剑桥国际大纲

取指-译码-执行周期

冯·诺依曼体系结构(Von Neumann architecture)是几乎每一台通用计算机的基础:

  • 一个单一存储器——立即存取存储器(Immediate Access Store,IAS)——同时容纳程序指令数据(存储程序(stored program)概念)。
  • 一个处理器(processor,CPU)从存储器取指令并一次运行一条。
  • 除非一个分支改变流程,指令按顺序运行。

存储程序的想法就是使一台计算机灵活的东西:改变程序,你就改变它所做的事,不用重新布线。

探索

Tap the parts of a Von Neumann computer

Explore each block. The CPU (control unit, ALU, registers) talks to a single main memory over the buses — and that one shared memory for instructions AND data is the Von Neumann idea.

词汇表 训练
英文 中文 拼音
Von Neumann architecture 冯·诺依曼体系结构 féng · nuò yī màn tǐ xì jié gòu
stored program 存储程序 cún chǔ chéng xù
processor 处理器 chǔ lǐ qì
arithmetic and logic unit 算术逻辑单元 suàn shù luó jí dān yuán
Immediate Access Store 立即存取存储器 lì jí cún qǔ cún chǔ qì
4.1

CPU 的主要部件

所有这些部件都位于一个小芯片内。本节后面的图显示它们如何连接;下面的照片显示实物。

白色背景上一个 Intel CPU 芯片的底面,一个扁平的方块,覆盖着数百个小金色触点垫的网格,它们压到主板插座上
一个现代 CPU:整个处理器是一个小芯片(这里从下方看,显示触点)
一个主板上的方形 CPU 插座,带一个微小针脚的网格和一个金属固定杆,周围是电路走线
主板上匹配的 CPU 插座:芯片的触点压到这些针脚上

算术逻辑单元(ALU)

ALU(算术逻辑单元)做算术(加、减……)和逻辑(AND、OR、比较)。它从寄存器(registers)取操作数,并把结果放回一个寄存器。

控制单元(CU)

控制单元(control unit)译码每条指令,并发送控制信号来执行它——打开数据通路、告诉 ALU 做什么,以及控制存储器读和写。

系统时钟

时钟发送一串稳定的脉冲,使 CPU 保持同步。每条指令占固定数目的周期,时钟频率(clock speed,例如 3.8 GHz)是性能的一个因素。

寄存器

寄存器是 CPU 内微小、非常快的存储。专用寄存器(special purpose registers)各在周期中有一个固定的工作:

  • 程序计数器(Program Counter,PC)——下一条指令的地址。
  • 内存地址寄存器(Memory Address Register,MAR)——正在读或写的地址。
  • 内存数据寄存器(Memory Data Register,MDR)——正在往存储器去或从存储器来的数据。
  • 当前指令寄存器(Current Instruction Register,CIR)——正在译码的指令。
  • 累加器(Accumulator,ACC)——ALU 正在处理的值。
  • 状态寄存器(Status Register)——容纳分支所用的标志(flags,进位、零、负、溢出)。
  • 变址寄存器(Index Register)——变址寻址中所用的一个偏移量。

通用寄存器(general-purpose registers)由程序员用于计算过程中的临时值。寄存器与存储器之间的数据移动用寄存器传送(register transfer)记法写——例如 MAR ← [PC]("把 PC 的内容复制到 MAR")。

冯·诺依曼 CPU 的框图,显示 PC、MAR、MDR、CIR、ACC、状态寄存器、控制单元、ALU 和系统时钟,由地址、数据和控制总线连到主存储器和输入/输出
冯·诺依曼 CPU:由总线连接的寄存器、控制单元和 ALU
词汇表 训练
英文 中文 拼音
register 寄存器 jì cún qì
control unit 控制单元 kòng zhì dān yuán
clock speed 时钟频率 shí zhōng pín lǜ
Program Counter 程序计数器 chéng xù jì shù qì
Memory Address Register 内存地址寄存器 nèi cún dì zhǐ jì cún qì
Memory Data Register 内存数据寄存器 nèi cún shù jù jì cún qì
Current Instruction Register 当前指令寄存器 dāng qián zhǐ lìng jì cún qì
Accumulator 累加器 lěi jiā qì
Status Register 状态寄存器 zhuàng tài jì cún qì
flags 标志 biāo zhì
Index Register 变址寄存器 biàn zhǐ jì cún qì
general-purpose registers 通用寄存器 tōng yòng jì cún qì
special purpose registers 专用寄存器 zhuān yòng jì cún qì
register transfer 寄存器传送 jì cún qì chuán sòng
4.1

总线

三条内部总线(buses,一组组并行导线)连接各部件:

  • 地址总线(address bus)——传送存储器地址。单向(CPU → 存储器)。
  • 数据总线(data bus)——传送数据。双向
  • 控制总线(control bus)——传送控制信号(读、写、中断)。双向

一条 $n$ 位地址总线能到达 $2^{n}$ 个存储单元。数据总线宽度决定每次访问移动多少位(常常是字长)。

CPU、存储器和输入/输出各接入系统总线内的地址总线(单向)、数据总线和控制总线
连接 CPU、存储器和输入/输出的三条系统总线
词汇表 训练
英文 中文 拼音
buses 总线 zǒng xiàn
address bus 地址总线 dì zhǐ zǒng xiàn
data bus 数据总线 shù jù zǒng xiàn
control bus 控制总线 kòng zhì zǒng xiàn
4.1

影响性能的因素

  • 时钟频率——每秒更多的周期。
  • 核心数(cores)——一个多核 CPU 一次运行几个线程。
  • 字长(word size)——一个 64 位 CPU 每周期处理 64 位块,并能寻址比一个 32 位的多得多的存储器。
  • RAM 数量(随机存取存储器)——更多的 RAM 容纳更多的工作集;太少迫使操作系统(page)到磁盘。
  • 高速缓存(cache memory)大小——更多的高速缓存减少平均存储器访问时间。
  • 辅助存储器(secondary storage)类型——一个 SSD 加载程序比一个 HDD 快得多。
  • 总线宽度和速度——更宽/更快的总线更快地移动数据。

使规格与工作负载匹配:一个四核在并行工作上胜过一个双核,但更高的每核速度在单线程工作上胜出。

词汇表 训练
英文 中文 拼音
cores 核心 hé xīn
word size 字长 zì zhǎng
RAM 随机存取存储器 suí jī cún qǔ cún chǔ qì
page
secondary storage 辅助存储器 fǔ zhù cún chǔ qì
cache memory 高速缓存 gāo sù huǎn cún
4.1

端口

一个端口(port)是一个用于连接外围设备(peripheral)的物理插座:

  • USB(通用串行总线)——通用(键盘、驱动器、手机)。
  • HDMI(高清多媒体接口)——向一个屏幕的数字视频和音频。
  • VGA(视频图形阵列)——向一个显示器的较老的模拟视频输出。
  • Ethernet(RJ-45)——有线 LAN。音频插孔——耳机/麦克风。

不同的端口用不同的信号,所以一根 HDMI 电缆不会装进一个 USB 插座。USB-C 不寻常,它携带视频、数据和电力。

词汇表 训练
英文 中文 拼音
port 端口 duān kǒu
peripheral 外围设备 wài wéi shè bèi
4.1

取指-执行周期

CPU 重复取指-执行周期(fetch-execute cycle),每条机器指令运行一次。

取指

  1. PC 的地址被复制到 MAR。
  2. PC 被递增以指向下一条指令。
  3. 一个信号经控制总线传送。
  4. 存储器把指令放到数据总线上。
  5. 它被复制到 MDR,然后到 CIR。
取指的寄存器传送按顺序:1 PC 的地址去 MAR;2 MAR 把地址发给存储器;3 指令返回到 MDR;4 MDR 把它复制到 CIR;同时 PC 被递增
一次取指中的寄存器传送:PC → MAR → 存储器 → MDR → CIR,PC 被递增

译码

CU 译码 CIR 中的指令——什么运算,以及哪些操作数或地址。

执行

CU 执行它:算术/逻辑去 ALU(结果到 ACC);一个加载/存储在存储器和一个寄存器之间移动数据;一个分支改变 PC。然后周期重复。

从 START 开始的取指-执行周期流程图:取指阶段(PC 到 MAR、递增 PC、读信号、存储器到数据总线到 MDR 到 CIR)、译码阶段、执行阶段,然后一个循环回到 START 的中断检查
取指-执行周期,每次都有一个中断检查
探索

The fetch-execute cycle

Tap round the loop the CPU repeats billions of times a second. Watch how fetch uses the PC/MAR/MDR/CIR registers, then decode and execute act on what was fetched.

探索

The fetch–execute cycle

Step through how the CPU runs one instruction — fetch it from memory, decode it, then execute it, over and over.

词汇表 训练
英文 中文 拼音
fetch-execute cycle 取指-执行周期 qǔ zhǐ - zhí xíng zhōu qī
4.1

中断

一个中断(interrupt)是一个暂停正常周期的信号,以便 CPU 能处理一个紧急事件(一次按键、一个数据包到达、一个硬件故障、除以零、操作系统计时器)。

处理一个:

  1. 完成当前指令。
  2. 保存状态(PC 和寄存器)。
  3. 中断服务程序(interrupt service routine,ISR)的地址加载到 PC 并运行它。
  4. ISR 处理事件。
  5. 恢复保存的状态并继续。

中断让系统及时响应,而 CPU 不用不断检查设备,并且这是操作系统多任务的方式。

中断处理流程图:运行中的程序被中断,CPU 完成当前指令、在栈上保存它的状态(PC 和寄存器)、运行中断服务程序、恢复状态并继续
一个中断如何嵌入取指-执行周期
词汇表 训练
英文 中文 拼音
interrupt 中断 zhōng duàn
interrupt service routine 中断服务程序 zhōng duàn fú wù chéng xù
4.2

汇编语言

大纲
Candidates should be able to: Notes and guidance
Show understanding of the relationship between assembly language and machine code
Describe the different stages of the assembly process for a two-pass assembler Apply the two-pass assembler process to a given simple assembly language program
Trace a given simple assembly language program
Show understanding that a set of instructions are grouped Including the following groups: • Data movement • Input and output of data • Arithmetic operations • Unconditional and conditional instructions • Compare instructions
Show understanding of and be able to use different modes of addressing Including immediate, direct, indirect, indexed, relative

来源:剑桥国际大纲

CPU 实际运行机器码(machine code)——位模式,专属于一个体系结构。汇编语言(assembly language)是一种可读的形式,每条机器指令一条指令,用像 LDDADDJMP 这样的助记符(mnemonics)书写。一个汇编器(assembler)把它翻译成机器码。

一个汇编器把汇编助记符翻译成机器码位模式
一个汇编器把助记符变成机器码位模式

两遍汇编器

一个两遍汇编器读源两次:

  • 第一遍构建一个符号表(symbol table):每当一个标签(label,如 LOOP:)出现时,记录它的地址;还不生成代码。
  • 第二遍生成代码:翻译每条指令,当一条引用一个标签(如 JMP LOOP)时,在符号表中查找它的地址。

两遍处理前向引用(forward references,一个跳到后面定义的标签的跳转)。

示例指令集

剑桥用一个小的通用集:数据移动(LDDLDMLDILDXSTOMOV)、算术(ADDSUBINCDEC)、逻辑/位(ANDORXORLSLLSR)、比较和分支(CMPJMPJPEJPN)、I/O(INOUT),以及 END。确切的助记符在试卷的参考表中给出。

探索

How a two-pass assembler works

Step through it. The assembler reads your code twice: pass 1 just finds where every label lives, so pass 2 can fill in the addresses — that is how a jump to a label defined later still works.

词汇表 训练
英文 中文 拼音
machine code 机器码 jī qì mǎ
assembly language 汇编语言 huì biān yǔ yán
mnemonics 助记符 zhù jì fú
assembler 汇编器 huì biān qì
symbol table 符号表 fú hào biǎo
label 标签 biāo qiān
forward references 前向引用 qián xiàng yǐn yòng
4.2

寻址方式

寻址方式(addressing mode)说明 CPU 如何找到操作数:

  • 立即寻址(immediate addressing)——操作数是指令中的值。LDM #10 加载 10。
  • 直接寻址(direct addressing)——指令容纳一个地址;操作数是那里的值。LDD 200
  • 间接寻址(indirect addressing)——指令容纳一个地址,它容纳另一个地址,那才是数据。LDI 200
  • 变址寻址(indexed addressing)——有效地址是 address + index register;用于数组。LDX 100 且 IR = 5 读地址 105。

(相对寻址(relative addressing)把地址作为从 PC 起的一个偏移量给出——用于跳转。)

四种寻址方式到达它们的操作数。立即:LDM #10 直接给出 10。直接:LDD 200 读存储单元 200(=42)。间接:LDI 200 读单元 200(=250)再读单元 250(=99)。变址:LDX 100 且变址寄存器 5 读单元 105(=7)
每种寻址方式如何到达它的操作数——立即、直接、间接和变址

例题。 内存中:地址 200 存放 250,地址 250 存放 99,地址 105 存放 7。变址寄存器中是 5。分别执行 LDM #200LDD 200LDI 200LDX 100 之后,累加器中各是什么?跟着每种寻址方式要找多远来看。LDM #200立即寻址 - 操作数就是指令中写的那个数,所以累加器中是 200LDD 200直接寻址 - 去地址 200 取出其中的内容:250LDI 200间接寻址 - 地址 200 中存的是 250,那是另一个地址,所以继续去地址 250:99LDX 100变址寻址 - 把变址寄存器加到地址上,$100 + 5 = 105$,再读地址 105:7。用"跳几次"来区分它们:立即 0 次,直接 1 次,间接 2 次,变址 1 次(在加上变址值之后)。

词汇表 训练
英文 中文 拼音
addressing mode 寻址方式 xún zhǐ fāng shì
immediate addressing 立即寻址 lì jí xún zhǐ
direct addressing 直接寻址 zhí jiē xún zhǐ
indirect addressing 间接寻址 jiàn jiē xún zhǐ
indexed addressing 变址寻址 biàn zhǐ xún zhǐ
relative addressing 相对寻址 xiāng duì xún zhǐ
4.2

跟踪汇编程序

跟踪它:做一个表,列有 PC、ACC、变址寄存器、每个变量和任何标志。逐条走过指令,每条之后更新表;当分支改变 PC 时跟随它们;在 END 停止。一个常见模式是用变址寻址在一个数组上循环。

4.3

位操作

大纲
Candidates should be able to: Notes and guidance
Show understanding of and perform binary shifts Logical, arithmetic and cyclic Left shift, right shift
Show understanding of how bit manipulation can be used to monitor/control a device Carry out bit manipulation operations Test and set a bit (using bit masking)
Instruction Label | Opcode | Operand Explanation
AND #n / Bn / &n Bitwise AND operation of the contents of ACC with the operand
AND
Bitwise AND operation of the contents of ACC with the contents of
XOR #n / Bn / &n Bitwise XOR operation of the contents of ACC with the operand
XOR
Bitwise XOR operation of the contents of ACC with the contents of
OR #n / Bn / &n Bitwise OR operation of the contents of ACC with the operand
OR
Bitwise OR operation of the contents of ACC with the contents of
LSL #n Bits in ACC are shifted logically n places to the left. Zeros are introduced on the right hand end
LSR #n Bits in ACC are shifted logically n places to the right. Zeros are introduced on the left hand end
Labels an instruction
Gives a symbolic address
All questions will assume there is only one general purpose register available (Accumulator) ACC denotes Accumulator IX denotes Index Register
can be an absolute or symbolic address # denotes a denary number, e.g. #123 B denotes a binary number, e.g. B01001010 & denotes a hexadecimal number, e.g. &4A

来源:剑桥国际大纲

一个逻辑移位(logical shift)把所有位向左或向右移若干位,用 0 填充新位置。

  • 左移 1(LSL #1)——位向左移,一个 0 从右边进入;对一个无符号数这是 × 2
  • 右移 1(LSR #1)——位向右移,一个 0 从左边进入;对一个无符号数这是整数 ÷ 2

$n$ 位乘以或除以 $2^{n}$。例子:00001011(11)LSL #100010110(22)。

一个算术右移保留符号位,使一个负的有符号数保持为负。一个循环移位(cyclic shift,旋转)把从一端掉出的位从另一端送回,所以没有位丢失。

三个 8 位移位:LSL #1 把 00001011 变成 00010110(乘 2,一个 0 从右边进入);LSR #1 把它变成 00000101(整数除以 2,一个 0 从左边进入);ASR #1 把 10110100 变成 11011010,复制符号位
逻辑左($\times 2$)、逻辑右($\div 2$)和算术右(保留符号位)

用于监控/控制的位操作

嵌入式设备常常每个信号用一个寄存器(bit)(例如位 $n$ = LED $n$)。用一个掩码(mask)——位掩码——你可以:

  • 置位 $n$:R = R OR 一个位 $n$ 置位的掩码。
  • 清位 $n$:R = R AND 一个位 $n$ 清零而其余置位的掩码。
  • 翻转位 $n$:R = R XOR 一个位 $n$ 置位的掩码。
  • 测试位 $n$:R AND 掩码,然后检查结果是否非零。
字节 01001000 上的位掩码:用 OR 00000100 置位 2 得到 01001100;用 AND 10111111 清位 6 得到 00001000;用 XOR 00001000 翻转位 3 得到 01000000
用 OR 置一个位、用 AND 清它、用 XOR 翻转它——每个都用一个掩码

位操作快、用很少的存储器,并让一个字节容纳至多 8 个开/关状态。

探索

Shift and mask the bits of a byte

Pick an operator and watch each result bit. A left shift (<<) moves every bit up one place (×2); a right shift (>>) moves them down (÷2); AND with a mask clears the bits you don't want.

词汇表 训练
英文 中文 拼音
logical shift 逻辑移位 luó jí yí wèi
mask 掩码 yǎn mǎ
bit wèi
cyclic shift 循环移位 xún huán yí wèi
4.3

考试技巧

  • 用寄存器传送术语(PC、MAR、MDR、CIR、ACC)学取指-执行周期,以及什么递增 PC。
  • 说出每个寄存器的工作;地址总线是单向的,数据总线是双向的。
  • 区分寻址方式(立即、直接、间接、变址)——一个常见的题目。
  • 解释时钟频率、核心数、高速缓存大小和字长如何影响性能
  • 对于一个二进制移位,说明它是逻辑的还是算术的;左移乘以 2,右移除以 2。

本主题的互动课程

逐步学习,并即时检测练习。

A-Level 计算机科学历年真题

A-Level 计算机科学的更多主题

登录或创建账号

IGCSE, A-Level & AP