Computer Systems Fundamentals
University Computer Science Topic 3 5:52 English narration · English and 中文 subtitles you can switch
Chapters
Transcript
This course follows a program from a single bit to a running process.
本课程追踪程序从单个比特到运行中的进程的全过程。
The first two units are the alphabet: how bits encode data, and how logic gates compute with them.
前两个单元介绍基础:比特如何编码数据,以及逻辑门如何用它们进行计算。
The next two are the machine: the processor, memory and its instructions.
接下来的两个单元介绍机器:处理器、存储器及其指令。
The last two are the machinery around it: the operating system and the compiler toolchain.
最后两个单元介绍周边机制:操作系统和编译工具链。
Every layer is a contract with the one below.
每一层都与下一层有一个约定。
The same bits can mean different things.
相同的比特可以表示不同的含义。
In 8 bits, unsigned numbers run from 0 to 255; two's complement runs from minus 128 to 127.
在 8 位 中,无符号数的范围是 0 到 255;补码表示的数的范围是 -128 到 127。
To negate, flip every bit and add one: minus 73 becomes 1 0 1 1 0 1 1 1.
要取相反数,先将每一位取反再加一:-73 表示为 1 0 1 1 0 1 1 1。
Floats store sign, exponent and fraction, so 0.1 plus 0.2 gives 0.30000000000000004, a designed-in rounding.
浮点数存储符号、指数和小数部分,因此 0.1 加 0.2 得到 0.30000000000000004,这是设计中固有的舍入结果。
Here the same 32 bits are read three ways: as an unsigned integer, as a two's complement integer and as an IEEE floating-point number.
这里将同样的 32 位 以三种方式读取:无符号整数、补码整数和 IEEE 浮点数。
The bits never change; only the type tells the machine how to read them.
比特本身从未改变;机器只根据类型来决定如何读取它们。
So when you see a strange value, first ask which type the bits were read as.
所以当你看到一个奇怪的值时,首先要问这些比特是按什么类型读取的。
A half adder is just two gates: the sum is a XOR b, and the carry is a AND b, so 1 plus 1 gives sum 0, carry 1.
半加器只有两个门:和是 a XOR b,进位是 a AND b,所以 1 加 1 得到的和是 0,进位是 1。
Two half adders and an OR make a full adder, and chaining them adds whole numbers, with a delay that grows with length.
两个半加器加上一个 OR 门构成全加器,将全加器串联起来就可以做整数加法,但延迟会随长度增加。
A multiplexer uses a select bit to pass one of two inputs.
多路选择器用一个选择位,从两个输入中选一个通过。
Count the gates in each circuit.
数一数每个电路中的门。
The half adder needs one XOR and one AND.
半加器需要一个 XOR 门和一个 AND 门。
The full adder joins two half adders with an OR.
全加器用一个 OR 门连接两个半加器。
The multiplexer picks a lane.
多路选择器选择一条输入通路。
Each gate adds a small delay, and in a ripple-carry adder the carry must ripple through every stage, which is why gate delay limits the clock speed.
每个门都会带来一点延迟,在行波进位加法器中,进位必须逐级传播,这就是门延迟限制时钟速度的原因。
The processor runs one loop forever: fetch the instruction the program counter points to, decode it into control signals, execute it, and move the counter on by 4.
处理器不断重复一个循环:取出程序计数器指向的指令,将它译码为控制信号,执行指令,然后将计数器增加 4。
Memory forms a pyramid, from registers at about a nanosecond to main memory at about 100.
存储器构成一个金字塔,访问时间从寄存器的约一纳秒到主存的约 100 纳秒不等。
Caches load 64 adjacent bytes at a time, so walking memory in order is much faster.
高速缓存每次加载相邻的 64 字节,所以按顺序遍历内存要快得多。
Walk one instruction around this loop.
跟随一条指令走完这个循环。
The program counter sends an address to memory, and the instruction comes back into the instruction register.
程序计数器向存储器发送地址,指令返回并进入指令寄存器。
The control unit decodes it, the ALU or memory executes it, and the result goes into the register file.
控制单元对它译码,ALU 或存储器执行它,结果进入寄存器堆。
Beside the loop, the memory pyramid shows why staying close to the processor matters.
循环旁边的存储器金字塔说明了为什么使用靠近处理器的存储器很重要。
Instructions come in a few shapes: add uses three registers, addi adds a constant, load and store move data between memory and registers, and branches compare and jump.
指令有几种形式:add 使用三个寄存器,addi 加上一个常数,load 和 store 在存储器与寄存器之间传送数据,分支指令进行比较并跳转。
A while loop becomes two instructions: decrement R1, then branch back if it is not zero.
一个 while 循环可以变成两条指令:将 R1 减一,然后在它不为零时跳回。
Adding one to an array element becomes a load, an add and a store.
给数组元素加一则需要一次加载、一次加法和一次存储。
Follow register R1 through this trace, line by line.
在这段执行记录中,逐行跟踪寄存器 R1。
It starts at 3, and each pass decrements it and checks it: 3, then 2, then 1, then 0, when the branch is no longer taken and the loop ends.
它从 3 开始,每一轮都将它减一并检查:先是 3,然后是 2、1、0,此时不再发生分支,循环结束。
Each line shows the instruction fetched, what it executed and how the registers changed.
每一行都显示取出的指令、执行的操作以及寄存器的变化。
This is a loop unrolled in time.
这就是将循环按时间展开。
No ordinary program touches the hardware directly; the operating system sells services instead.
普通程序不会直接访问硬件;操作系统会提供服务。
A process is a program plus its state, and a context switch saves exactly that state.
进程是程序及其状态,而上下文切换保存的正是这些状态。
Virtual memory gives each process its own addresses, through page tables.
虚拟内存通过页表为每个进程提供自己的地址空间。
System calls like open, read and fork cross into the kernel.
open、read 和 fork 等系统调用会进入内核。
A page fault is normal paging, not a crash.
缺页是正常的分页过程,并非程序崩溃。
Trace one read call down through the layers.
追踪一次 read 调用如何逐层向下进行。
The program calls a library function, which makes a system call and crosses into the kernel.
程序调用一个库函数,该函数发起系统调用并进入内核。
The kernel checks permissions, asks the file system for the blocks, and the device driver talks to the disk.
内核检查权限,向文件系统请求数据块,然后由设备驱动程序与磁盘通信。
The data travels back up, and the program continues as if nothing complicated happened.
数据逐层返回,程序继续运行,就像什么复杂的事都没有发生过一样。
In a context switch, the operating system saves the state of process A, its registers and program counter, in its process control block, then loads the saved state of process B.
在上下文切换中,操作系统将进程 A 的状态,也就是它的寄存器和程序计数器,保存在其进程控制块中,然后加载进程 B 保存的状态。
Switching takes time, but it lets many processes share one CPU.
切换需要时间,但它让多个进程能够共享一个 CPU。
Building a C program has four stages: the preprocessor expands macros, the compiler produces assembly, the assembler produces object code, and the linker joins the pieces into a program.
构建一个 C 程序有四个阶段:预处理器展开宏,编译器生成汇编代码,汇编器生成目标代码,链接器将各部分连接成程序。
Each stage has its own class of error; an undefined reference always comes from the linker.
每个阶段都有各自的错误类型;未定义引用总是由链接器报告。
Python instead compiles to bytecode and interprets it on every run.
Python 则编译成字节码,并在每次运行时解释执行。
Name the stage, and you can name the error.
知道了阶段,就能判断错误类型。
A broken macro is a preprocessor error.
宏出错是预处理器错误。
A type mismatch is a compiler error.
类型不匹配是编译器错误。
A bad instruction is an assembler error.
错误的指令是汇编器错误。
A missing function is a linker error, an undefined reference.
缺少函数是链接器错误,也就是未定义引用。
You can keep and inspect every intermediate file, so this pipeline is a debugging map.
你可以保留并检查每一个中间文件,所以这个流程也可以作为调试路线图。
Now test yourself from memory.
现在凭记忆自测。
How is minus 73 written in 8-bit two's complement?
-73 用 8 位 补码如何表示?
What are the sum and carry when a half adder adds 1 and 1?
半加器将 1 和 1 相加时,和与进位分别是多少?
By how much does the program counter advance after each instruction?
每执行一条指令后,程序计数器前进多少?
Which stage reports an undefined reference?
哪个阶段会报告未定义引用?
Pause, answer each one, then find it on the figure.
暂停,逐一回答,然后在图中找到答案。
Comments
No comments yet. Be the first to ask or share something.