Suppose a cache is 10 times faster than main memory and has a hit rate of 90%. How much speedup is achieved by using the cache?
提示
设缓存访问时间 = 1,主存 = 10。按课程约定,一次缺失要先查缓存再去主存:缺失时间 = 1 + 10。
EE6156 Part 2 · Tutorial 7–12
三种用法:Tutorial 精讲收录 Tutorial 7–12 全部 48 题和老师课上补充的 1 题,题目有英文原题和中文,每题可以先填关键数值让页面自动核对,再展开提示或完整解答;课堂练习是讲义里 Exercise 7.1–12.17,题目和解答都是中英双语(讲义学生版的答案页是空白的);随机刷题每次随机出一道同类型的新题,适合考前热手。右上角可以切换"中英 / EN / 中"。
数值答案允许 ±0.5% 或 ±0.01 的误差;百分数直接填数字(例如 37.5% 填 37.5)。勾选"已掌握"会保存在这个浏览器里。
对应 Lesson 7。核心公式:AMAT = HR × HT + MR × (HT + MP);实际 CPI = 理想 CPI + 每条指令的停顿;每条指令访存 = 1 + load/store 比例。
Suppose a cache is 10 times faster than main memory and has a hit rate of 90%. How much speedup is achieved by using the cache?
设缓存访问时间 = 1,主存 = 10。按课程约定,一次缺失要先查缓存再去主存:缺失时间 = 1 + 10。
Determine how many times faster a cache memory must be compared to the main memory in order to achieve an overall speedup of 5. Assume that the cache hit rate is 0.85.
设缓存 = 1,主存 = k。写出 AMAT,令 k / AMAT = 5 解方程。
缓存要比主存快 20 倍。注意上限:k → ∞ 时加速比趋于 1/0.15 ≈ 6.67,所以 5 倍是可以达到的。若按"缺失只花 k"的口径,1/(0.15 + 0.85/k) = 5 得 k = 17。
(a) Assume a machine has an ideal CPI of 2.0 when all instruction and data accesses hit in the cache. Loads and stores account for 40% of all instructions. Every instruction fetch and every data access has a cache miss rate of 2%. The miss penalty is 25 clock cycles. How much slower is the machine compared with the ideal all-hit machine?
(b) What would happen if there were no cache at all, assuming every memory access incurs the same 25-cycle penalty?
(c) If the ideal CPI is reduced to 1.0, but we want to maintain the same performance ratio as in part (a), what miss penalty is required, assuming the miss rate remains unchanged?
每条指令访存 1.4 次。(c) 中"性能比相同"指实际 CPI / 理想 CPI 仍为 1.35。
理想 CPI 减半后,缺失代价也必须减半才能保持同样的相对损失:CPU 越快,缓存越要紧。
Consider the following program fragment:
sum = 0; i = 0; while (sum < 15 && i < 8) { sum += a[i]; i++; }
The contents of the array a are: a[0] = 3, a[1] = 4, a[2] = 2, a[3] = 6, a[4] = 5, a[5] = 1, a[6] = 7, a[7] = 2. Determine:
sum = 0; i = 0; while (sum < 15 && i < 8) { sum += a[i]; i++; },数组 a = [3, 4, 2, 6, 5, 1, 7, 2]。求最终 sum、访存次数、无缓存总时间、有缓存总时间、性能提升倍数。(按老师课上的解答:缓存访问 2 ms,主存访问 20 ms;sum、i 在寄存器;数组在同一块,首次访问缺失,之后全命中。)先模拟循环:每轮结束检查 sum < 15。sum 一旦等于 15 循环就停。
| 轮 | 读 a[i] | sum | 条件 sum < 15 且 i < 8 |
|---|---|---|---|
| 1 | a[0] = 3 | 3 | 真,继续 |
| 2 | a[1] = 4 | 7 | 真 |
| 3 | a[2] = 2 | 9 | 真 |
| 4 | a[3] = 6 | 15 | 15 < 15 为假,退出 |
A 500 MHz processor has a main memory access time of 60 ns. Assume that the processor has an ideal Cycles per Instruction (CPI) of 2 when using a perfect cache (i.e., all instruction and data accesses hit in the L1 cache). Suppose that 25% of the program instructions are load or store instructions. If the instruction cache miss rate is 2% and the data cache miss rate is 5%, compute the effective CPI of the processor.
500 MHz → 时钟周期 2 ns,先把 60 ns 换成周期数。
Suppose a cache memory is 5 times faster than the main memory. The cache hit time is 10 ns, while a cache miss requires 100 ns. A program takes 1 hour to execute, and 20% of the execution time is spent on memory and cache accesses. The cache system achieves an average miss rate of 0.1. Determine how much execution time would be saved or lost if the same program were executed without cache memory.
老师的策略:只有 20% 那 12 分钟受访存影响,48 分钟不变。先算有缓存和无缓存的 AMAT,再把 12 分钟按两者之比放大。主存 = 5 × 10 = 50 ns。
不用缓存会多花 28 分钟(88 分钟对 60 分钟)。老师特别说:没见过的题先想策略,这题的策略就是"只放大受访存影响的那部分时间"。
(Extra question shown in the lecturer's Tutorial 7 solutions.) Assume each instruction requires on average 1.2 instruction fetches, and the processor has an ideal CPI of 1.5. The instruction-cache miss rate is 2% with a miss penalty of 50 cycles. On average there are 0.3 data accesses per instruction, the data-cache miss rate is 5% and the data miss penalty is 80 cycles. Find the memory stall cycles per instruction and the actual CPI.
和平常唯一的区别:每条指令取指不是 1 次,而是 1.2 次。指令和数据的缺失率、代价不同,分开算再相加。
这题不在学生版 Tutorial 7 题单里,是老师第 8 周讲解答时多放的一题。老师的话:题目看着复杂,套的还是同一个式子。
A processor has an ideal CPI of 1.5 when all instruction fetches and data accesses hit in the cache. The actual CPI is measured to be 2.7. Assume that:
Find the instruction-cache miss rate, the data-cache miss rate, and the total memory stall cycles per instruction.
停顿 = 2.7 − 1.5。设指令缺失率为 m,写出停顿关于 m 的式子。
A processor has an ideal CPI of 1.8. The instruction mix consists of 60% arithmetic/logic instructions, 25% load/store instructions and 15% branch instructions. Assume:
Determine the average number of memory references per instruction, the memory stall cycles per instruction, the actual CPI, and the percentage of execution time spent on memory stalls.
对应 Lesson 8。块数 = 容量 ÷ 块大小;组数 = 块数 ÷ 相联度;Offset = log₂块大小,Index = log₂组数,Tag = 地址位 − 两者。比较器个数 = 相联度,每个比较器宽 Tag 位。
A cache has hit time = 2 cycles, miss rate = 4% and miss penalty = 80 cycles. Determine the miss time and the Average Memory Access Time (AMAT).
A computer has 4 GB of byte-addressable main memory and uses a 32-bit address bus. For each of the following cache organisations, determine:
(i) A direct-mapped cache with a total data size of 512 KB, using 8-word blocks.
(ii) A 2-way set-associative cache with a total data size of 256 KB, using 2-word blocks.
块大小先换成字节:8 字 = 32 B,2 字 = 8 B。XOR 门数 = 比较器个数 × 每个比较器的位数。
| (i) 直接映射 512 KB, 32 B 块 | (ii) 2 路 256 KB, 8 B 块 | |
|---|---|---|
| 块数 | 2¹⁹ / 2⁵ = 2¹⁴ = 16384 | 2¹⁸ / 2³ = 2¹⁵ = 32768 |
| 组数 | 16384(每组 1 块) | 32768 / 2 = 16384 |
| Offset | log₂32 = 5 | log₂8 = 3 |
| Index | log₂16384 = 14 | 14 |
| Tag | 32 − 14 − 5 = 13 | 32 − 14 − 3 = 15 |
| 标签总位数 | 16384 × 13 = 212,992 位(26 KB) | 32768 × 15 = 491,520 位(60 KB) |
| XOR 门 | 1 个 13 位比较器 = 13 | 2 个 15 位比较器 = 30 |
(f) 只是给出 1 字 = 4 字节的假设。每个比较器除了 XOR 还需要一个把所有 XOR 输出合并的或非(NOR)门树来判断"全为 0",题目只问 XOR 数。
Consider a program in which 20% of the instructions are load or store instructions. The processor has: main memory access time = 50 ns; processor clock rate = 500 MHz; base CPI with a perfect cache = 2.0; instruction-cache miss rate = 3%; data-cache miss rate = 10%. Assume that every instruction requires one instruction fetch, and only load/store instructions require a data-cache access. Also assume that the miss penalty is equal to the main memory access time. Find the effective CPI of the machine.
Assume a computer with a 32-bit byte-addressable address space.
Assume a computer with a 32-bit byte-addressable address space. A cache has capacity = 256 KB, block size = 64 bytes, 4-way set associativity. Determine:
A processor uses a cache memory with cache hit time = 2 ns, cache miss penalty = 80 ns and cache hit rate = 95%.
(a) Determine 1. the cache miss rate, 2. the cache miss time, 3. the AMAT. Use both of the following formulas and verify that they give the same answer: AMAT = HR × HT + MR × MT and AMAT = HT + MR × MP.
(b) A cache designer improves the cache and increases the hit rate from 95% to 98%. Assuming the hit time and miss penalty remain unchanged, determine the new AMAT.
(c) Another design reduces the miss penalty from 80 ns to 40 ns while keeping the original hit rate of 95%. Determine the new AMAT.
(d) Compare the results obtained in Parts (a), (b), and (c). Which improvement gives the larger reduction in AMAT?
(e) A second cache design has hit time = 1 ns, miss penalty = 120 ns, hit rate = 94%. Determine its AMAT. Which cache design would you choose based on AMAT? Explain your answer.
(d) 本题中提高命中率更有效:比较缺失项 MR × MP,(b) 为 0.02 × 80 = 1.6,(c) 为 0.05 × 40 = 2.0。(e) 第二种设计命中更快,但缺失率和代价都更高,缺失项 7.2 ns 远大于命中时间节省的 1 ns,AMAT 8.2 ns 比原设计的 6.0 ns 差,应选原设计(若可以做 (b) 的改进则更好)。
Consider a cache with tag size = 6 bits and number of cache lines = 16. Determine the number of comparators required for a direct-mapped cache, the total number of parallel bit-comparisons required for a direct-mapped cache, the number of comparators required for a fully-associative cache, and the total number of parallel bit-comparisons required for a fully-associative cache.
A computer system has main memory size 256 bytes, cache size 8 cache lines, block size 4 bytes per block, Least Recently Used (LRU) replacement (where applicable), and the cache is initially empty. The following sequence of memory references is generated: 20, 37, 54, 20, 70, 38. Repeat the following for each cache organization: (a) fully-associative; (b) direct-mapped; (c) 2-way set-associative.
(i) Determine the number of bits required for the memory address, tag, index (or set index) and offset, where applicable.
(ii) For each memory reference: determine the corresponding memory block; determine the cache set (or cache line) to which the block maps; state whether the access is a hit or a miss; if it is a miss, classify it as either a compulsory (cold) miss or a conflict miss; show the contents of the cache after the access, including the LRU information for each line (where applicable).
(iv) Using the same sequence, determine the total number of cache hits, cache misses, compulsory (cold) misses, conflict misses, and the cache hit rate.
(v) Compare the results obtained for the three cache organizations and comment on the effect of cache associativity on cache performance.
20, 37, 54, 20, 70, 38。分别对 (a) 全相联、(b) 直接映射、(c) 2 路组相联:求各字段位数;逐次写出块号、组/行、命中或缺失(缺失要分 compulsory / conflict)及缓存内容;统计命中、缺失、强制、冲突次数和命中率;最后比较三者。块号 = 地址 ÷ 4:20→5,37→9,54→13,70→17,38→9。直接映射行号 = 块号 mod 8,2 路组号 = 块号 mod 4。可以在学习手册的缓存模拟器里选这三个预设逐步对照。
| 组织 | Tag | Index | Offset |
|---|---|---|---|
| 全相联 | 6 | 0 | 2 |
| 直接映射(8 行) | 3 | 3 | 2 |
| 2 路(4 组) | 4 | 2 | 2 |
| # | 地址 | Tag Index Offset | 块 | 组/行 | Tag | 结果 | 被替换 | 访问后缓存(括号内为 TSLR) |
|---|---|---|---|---|---|---|---|---|
| 1 | 20 | 000101 00 | 20–23 | – | 000101 | MISS compulsory | 20–23(0) | |
| 2 | 37 | 001001 01 | 36–39 | – | 001001 | MISS compulsory | 20–23(1), 36–39(0) | |
| 3 | 54 | 001101 10 | 52–55 | – | 001101 | MISS compulsory | 20–23(2), 36–39(1), 52–55(0) | |
| 4 | 20 | 000101 00 | 20–23 | – | 000101 | HIT | 20–23(0), 36–39(2), 52–55(1) | |
| 5 | 70 | 010001 10 | 68–71 | – | 010001 | MISS compulsory | 20–23(1), 36–39(3), 52–55(2), 68–71(0) | |
| 6 | 38 | 001001 10 | 36–39 | – | 001001 | HIT | 20–23(2), 36–39(0), 52–55(3), 68–71(1) |
命中 2、缺失 4(全部强制)、冲突 0,命中率 2/6 = 33.3%。
| # | 地址 | Tag Index Offset | 块 | 组/行 | Tag | 结果 | 被替换 | 访问后缓存 |
|---|---|---|---|---|---|---|---|---|
| 1 | 20 | 000 101 00 | 20–23 | 5 | 000 | MISS compulsory | L5: 20–23 | |
| 2 | 37 | 001 001 01 | 36–39 | 1 | 001 | MISS compulsory | L1: 36–39 L5: 20–23 | |
| 3 | 54 | 001 101 10 | 52–55 | 5 | 001 | MISS compulsory | 20–23 | L1: 36–39 L5: 52–55 |
| 4 | 20 | 000 101 00 | 20–23 | 5 | 000 | MISS conflict | 52–55 | L1: 36–39 L5: 20–23 |
| 5 | 70 | 010 001 10 | 68–71 | 1 | 010 | MISS compulsory | 36–39 | L1: 68–71 L5: 20–23 |
| 6 | 38 | 001 001 10 | 36–39 | 1 | 001 | MISS conflict | 68–71 | L1: 36–39 L5: 20–23 |
块 5 与块 13 都映射到行 101,块 9 与块 17 都映射到行 001,互相踢出。命中 0、缺失 6(强制 4、冲突 2),命中率 0%。最终行 001 = 36–39(tag 001),行 101 = 20–23(tag 000),其余行无效。
| # | 地址 | Tag Index Offset | 块 | 组/行 | Tag | 结果 | 被替换 | 访问后缓存(括号内为 TSLR) |
|---|---|---|---|---|---|---|---|---|
| 1 | 20 | 0001 01 00 | 20–23 | 1 | 0001 | MISS compulsory | S1: 20–23(0) | |
| 2 | 37 | 0010 01 01 | 36–39 | 1 | 0010 | MISS compulsory | S1: 20–23(1), 36–39(0) | |
| 3 | 54 | 0011 01 10 | 52–55 | 1 | 0011 | MISS compulsory | 20–23 | S1: 52–55(0), 36–39(1) |
| 4 | 20 | 0001 01 00 | 20–23 | 1 | 0001 | MISS conflict | 36–39 | S1: 52–55(1), 20–23(0) |
| 5 | 70 | 0100 01 10 | 68–71 | 1 | 0100 | MISS compulsory | 52–55 | S1: 68–71(0), 20–23(1) |
| 6 | 38 | 0010 01 10 | 36–39 | 1 | 0010 | MISS conflict | 20–23 | S1: 68–71(1), 36–39(0) |
四个块号 5、9、13、17 除以 4 余数都是 1,全部落进组 1,而组 1 只有 2 路,三个以上的块轮流被 LRU 踢出。命中 0、缺失 6(强制 4、冲突 2),命中率 0%。
全相联命中率最高(33.3%),直接映射和这里的 2 路都是 0%。一般来说提高相联度能减少冲突缺失,但前提是争同一组的块数不超过路数;本题四个块全挤在 2 路组相联的同一组里,2 路帮不上忙。相联度越高,比较器和 LRU 硬件越多。
对应 Lesson 9。CPU 时间 = IC × (CPI × 时钟周期),比较缓存时别只看 AMAT;多级缓存:AMAT = HT₁ + MR₁ × (HT₂ + MR₂ × MP₂);物理地址 = PPN 拼接页内偏移。
Suppose a computer has a 12-bit byte-addressable address space and a direct-mapped cache with 16 cache blocks. Each cache block can store 16 bytes of data. A program generates the following sequence of memory accesses, given in hexadecimal: 3FE, 4FE, 4FD, 4F2, 111, 111, 2F1, 4FE, 2F7, 448, 2F9, 2A2.
3FE, 4FE, 4FD, 4F2, 111, 111, 2F1, 4FE, 2F7, 448, 2F9, 2A2。
Offset = log₂16 = 4,Index = log₂16 = 4,Tag = 12 − 8 = 4。正好每个十六进制位是一个字段:第 1 位是 Tag,第 2 位是行号,第 3 位是偏移。
(a) Offset 4 + Index 4,Tag = 12 − 4 − 4 = 4 位。以 3FE 为例:Tag = 3,行 = F,偏移 = E。
| # | 地址 | Tag Index Offset | 块 | 组/行 | Tag | 结果 | 被替换 | 访问后缓存 |
|---|---|---|---|---|---|---|---|---|
| 1 | 3FE | 0011 1111 1110 | 1008–1023 | 15 | 0011 | MISS compulsory | L15: 1008–1023 | |
| 2 | 4FE | 0100 1111 1110 | 1264–1279 | 15 | 0100 | MISS compulsory | 1008–1023 | L15: 1264–1279 |
| 3 | 4FD | 0100 1111 1101 | 1264–1279 | 15 | 0100 | HIT | L15: 1264–1279 | |
| 4 | 4F2 | 0100 1111 0010 | 1264–1279 | 15 | 0100 | HIT | L15: 1264–1279 | |
| 5 | 111 | 0001 0001 0001 | 272–287 | 1 | 0001 | MISS compulsory | L1: 272–287 L15: 1264–1279 | |
| 6 | 111 | 0001 0001 0001 | 272–287 | 1 | 0001 | HIT | L1: 272–287 L15: 1264–1279 | |
| 7 | 2F1 | 0010 1111 0001 | 752–767 | 15 | 0010 | MISS compulsory | 1264–1279 | L1: 272–287 L15: 752–767 |
| 8 | 4FE | 0100 1111 1110 | 1264–1279 | 15 | 0100 | MISS conflict | 752–767 | L1: 272–287 L15: 1264–1279 |
| 9 | 2F7 | 0010 1111 0111 | 752–767 | 15 | 0010 | MISS conflict | 1264–1279 | L1: 272–287 L15: 752–767 |
| 10 | 448 | 0100 0100 1000 | 1088–1103 | 4 | 0100 | MISS compulsory | L1: 272–287 L4: 1088–1103 L15: 752–767 | |
| 11 | 2F9 | 0010 1111 1001 | 752–767 | 15 | 0010 | HIT | L1: 272–287 L4: 1088–1103 L15: 752–767 | |
| 12 | 2A2 | 0010 1010 0010 | 672–687 | 10 | 0010 | MISS compulsory | L1: 272–287 L4: 1088–1103 L10: 672–687 L15: 752–767 |
(b) 最终只有 4 行有效:行 1:Tag 1(块 110–11F);行 4:Tag 4(440–44F);行 A:Tag 2(2A0–2AF);行 F:Tag 2(2F0–2FF)。其余 12 行有效位为 0。画图时每行画出 Valid | Tag | Data 三栏,只填 Tag。
(d) 空间局部性:4FE 之后访问 4FD、4F2,都在同一块 4F0–4FF 里,第一次缺失把整块搬进来,后两次就命中(2F1、2F7、2F9 同理)。时间局部性:111 连续被访问两次;4FE 在第 2 次和第 8 次都被访问。
Assume that a CPU has a base CPI of 2.0 with a perfect cache and a clock cycle time of 2 ns. The program has an average of 1.3 memory references per instruction. Two cache organizations are considered:
Assume cache hit time = 1 clock cycle and cache miss penalty = 70 ns for both organizations. Calculate (a) the AMAT for each cache organization; (b) the CPU time for each cache organization in terms of instruction count, IC; (c) which cache organization gives better overall CPU performance? Explain briefly.
2 路的时钟 = 2.2 ns,命中时间也是一个时钟 = 2.2 ns。CPU 时间 = IC × (理想 CPI × 时钟 + 访存次数 × 缺失率 × 缺失代价 ns)。
(c) 直接映射更好。2 路的 AMAT 更低,但它把时钟周期拉长了 10%,而时钟影响的是每条指令的所有周期(理想 CPI 那 2 个周期每条都变慢了 0.4 ns),访存上省下的 0.364 ns 抵不过。教训:AMAT 只衡量访存,比较整体性能要算 CPU 时间。
如果把 2 路的缺失代价换算成整数周期(70/2.2 = 31.8 → 32 周期 = 70.4 ns),CPU 时间是 5.315 × IC,结论不变。
A processor has two levels of data cache: L1 — block size 8 bytes, hit time 1 cycle, local miss rate 5%; L2 — block size 32 bytes, hit time 19 cycles, local miss rate 2%. Assume that it takes 50 clock cycles to request and complete a 32-byte transfer between main memory and the L2 cache.
(a) Find the AMAT for the L2 cache. (b) Find the AMAT for the L1 cache. (c) Assume the ideal CPI is 2.0 and 25% of the instructions are loads or stores. Find 1. the average memory stall cycles per instruction; 2. the overall CPI.
Consider a cache that initially contains memory block 100–103 with cache contents A, B, C, D. The CPU performs: 1. Store X to address 101; 2. Store Y to address 102; 3. Store Z to address 101.
| 步骤 | 写直达:缓存 | 写直达:主存 | 写回:缓存 | 写回:主存 | 脏位 |
|---|---|---|---|---|---|
| 初始 | A B C D | A B C D | A B C D | A B C D | 0 |
| Store X → 101 | A X C D | A X C D | A X C D | A B C D | 1 |
| Store Y → 102 | A X Y D | A X Y D | A X Y D | A B C D | 1 |
| Store Z → 101 | A Z Y D | A Z Y D | A Z Y D | A B C D | 1 |
| 块被替换 | 主存已是最新,直接丢弃:A Z Y D | 脏位为 1,整块写回:主存变为 A Z Y D | |||
(d) 两种策略最终主存都是 A Z Y D,但写直达写了 3 次主存,写回只在替换时写 1 次。
(e) 写回的优点:① 对同一块的多次写合并成一次写回,内存流量和总线带宽占用小(这里 3 次变 1 次,X 根本没写到主存);② 写操作以缓存速度完成,不用每次等主存,性能更高、功耗更低。
(f) 写直达的优点:主存永远是最新的,一致性简单(例如 DMA 等 I/O 直接读主存不会读到旧值);不需要脏位,替换块时也不用先写回,控制更简单。
A computer uses a 32-bit address space and a 64 KB cache with 64-byte blocks. Answer for each organization: 1. direct-mapped; 2. 4-way set-associative; 3. fully associative.
(a) Determine the number of cache blocks, cache sets, tag bits, index bits and offset bits. (b) How many tag comparators are required for each organization during a cache lookup? (c) If each cache block requires 1 valid bit and tag storage only (ignore data storage), calculate the total number of bits required to store all tags and valid bits for each organization. (d) Which organization is expected to have 1. the lowest conflict miss rate, 2. the lowest hardware complexity, 3. the largest tag storage overhead? Explain briefly.
| 直接映射 | 4 路组相联 | 全相联 | |
|---|---|---|---|
| 块数 | 1024 | 1024 | 1024 |
| 组数 | 1024 | 256 | 1 |
| Offset / Index / Tag | 6 / 10 / 16 | 6 / 8 / 18 | 6 / 0 / 26 |
| 比较器 | 1 | 4 | 1024 |
| (Tag + 有效位) × 1024 | 17 × 1024 = 17,408 | 19 × 1024 = 19,456 | 27 × 1024 = 27,648 |
(d) ① 冲突缺失最少:全相联,块可以放任意位置,根本没有冲突缺失。② 硬件最简单:直接映射,1 个比较器,不需要替换逻辑。③ 标签开销最大:全相联,没有 Index 位,每个标签 26 位最长。
A program accesses the byte addresses 8, 12, 16, 20, 24, 28, 32, 36. Assume the cache is initially empty.
(a) Suppose the cache contains only one cache block and uses a block size of 8 bytes. For each access, indicate hit or miss, and determine the overall miss rate. (b) Repeat (a) with a block size of 16 bytes. (c) Repeat (a) with a block size of 32 bytes. (d) Compare the miss rates in (a)–(c). Explain how increasing block size exploits spatial locality. (e) Although larger blocks may reduce compulsory misses, explain why excessively large blocks can increase the overall miss rate in a fixed-size cache. (f) A designer is considering increasing the block size from 64 B to 256 B while keeping the cache size fixed. State one advantage and two disadvantages of the larger block size.
8, 12, 16, 20, 24, 28, 32, 36,缓存只有一个块,初始为空。分别用 8 B、16 B、32 B 的块求每次命中/缺失和缺失率;比较并解释;说明为什么固定容量下块过大反而会提高缺失率;块从 64 B 增大到 256 B(容量不变)的一个优点和两个缺点。| 地址 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 缺失率 |
|---|---|---|---|---|---|---|---|---|---|
| 8 B 块 | M 8–15 | H | M 16–23 | H | M 24–31 | H | M 32–39 | H | 4/8 = 50% |
| 16 B 块 | M 0–15 | H | M 16–31 | H | H | H | M 32–47 | H | 3/8 = 37.5% |
| 32 B 块 | M 0–31 | H | H | H | H | H | M 32–63 | H | 2/8 = 25% |
(d) 块越大,一次缺失搬进来的相邻字节越多;顺序访问马上用到它们,于是缺失率从 50% 降到 25%。这就是利用空间局部性。
(e) 容量固定时,块越大块数越少。程序同时活跃的不同区域放不下,块之间互相踢出,冲突和容量缺失增加;而且大块里很多字节可能根本不会被用到,却占着空间(也削弱了时间局部性)。所以缺失率随块大小先降后升。
(f) 64 B → 256 B。优点:更好地利用空间局部性,减少强制缺失(顺带减少标签存储)。缺点:① 缺失代价增大,每次缺失要传 4 倍的数据;② 块数减为 1/4,冲突/容量缺失增加,还可能搬进大量用不到的数据,浪费带宽。
A computer system uses 32-bit virtual addresses, 30-bit physical addresses and a page size of 4 KB. Page table entries: VPN 0x12345 → PPN 0x1A234 (valid 1); VPN 0x12346 → PPN 0x0B567 (valid 1); VPN 0xABCDE → PPN 0x1F890 (valid 1); VPN 0x22222 → — (valid 0).
(a) Determine the number of bits used for 1. the page offset, 2. the VPN, 3. the PPN. (b) Translate the virtual addresses 0x12345678, 0x12346ABC and 0xABCDEFFF into physical addresses. Show all working. (c) What happens when the CPU attempts to access virtual address 0x22222000? Explain the role of the valid bit. (d) Explain why the page offset remains unchanged during address translation. (e) State two advantages of virtual memory. (f) A page fault requires 5 ms to service, whereas a TLB miss requires only 50 ns. Calculate how many times more expensive a page fault is than a TLB miss.
4 KB 页的偏移正好是 12 位 = 3 个十六进制位,所以直接把最后 3 位抄下来,前面换成 PPN 即可。
(c) VPN 0x22222 的有效位 = 0,页不在物理内存里,发生页缺失(page fault):硬件触发异常,操作系统从磁盘把这一页调入某个物理页框,填写 PPN、把有效位置 1,然后重新执行这条访存指令。有效位标记"这个页表项的翻译是否可用",防止使用无效的 PPN。
(d) 页和物理页框大小相同且按页对齐,翻译只决定"哪一页",字节在页内的位置不变,所以低 12 位原样保留。
(e) 任选两个:能运行比物理内存还大的程序;每个进程有独立地址空间,相互隔离、受保护,又能有控制地共享;程序可以装到内存任意位置(重定位方便);避免物理内存碎片问题。
A computer system uses a cache. The following cache misses are observed: 1. the program accesses memory block A for the first time and incurs a miss; 2. memory blocks B and C repeatedly replace each other in the cache because they map to the same cache location; 3. a program repeatedly accesses a working set of 2 MB, but the cache size is only 64 KB. For each scenario, (a) state whether the miss is compulsory, conflict or capacity; (b) suggest one method to reduce the miss rate.
| 情形 | 类型 | 降低方法 |
|---|---|---|
| 1 首次访问 A | 强制缺失 | 预取;适当增大块(一次带进相邻数据) |
| 2 B、C 互踢 | 冲突缺失 | 提高相联度(如 2 路、4 路);或加大缓存 |
| 3 2 MB 工作集 vs 64 KB | 容量缺失 | 加大缓存或增加一级更大的缓存(L2/L3);也可以改程序提高局部性(分块) |
对应 Lesson 10。任务流水:T = ΣTᵢ + (n − 1)·maxTᵢ;时钟流水:T = (k + n − 1)·T_clk;大量指令时加速比 = 单周期时间 / 流水时钟周期;完全平衡时上限 = 级数。
(i) Consider a task that can be divided into m equal-duration subtasks, each requiring one clock cycle. The task is executed using an m-stage pipeline, where each stage performs one subtask. Derive an expression for the total number of clock cycles required for the pipeline to complete n tasks. Hence, show that Tpipeline = m + n − 1 clock cycles.
(ii) Figure 1 shows a sequential machine and its pipelined counterpart. Each widget must pass through three processing steps A, B and C, each requiring one clock cycle. The sequential machine completes the three steps one after another; the pipelined machine has three stages (A, B, C), each requiring one clock cycle. Determine the minimum number of widgets that must be processed for the pipelined machine to achieve a speedup of at least 2.5 over the sequential machine. Justify your answer.
(i) 第 1 个任务要走完 m 级,第 m 个周期结束时完成(填充流水线)。之后流水线已满,每个周期都有一个任务从最后一级出来,剩下 n − 1 个任务再用 n − 1 个周期。总计 m + (n − 1) = m + n − 1 个周期。
至少 10 个工件。n 越大加速比越接近 3(级数),但永远到不了 3,因为有 2 个周期的填充开销。
A processor can be implemented either as a single-stage (non-pipelined) processor with a clock cycle time of 5 ns, or as a 5-stage pipelined processor with a clock cycle time of 1 ns. Both execute the same instruction set, and the program is long enough that pipeline filling and draining effects may be ignored.
(a) Assuming no pipeline stalls, determine the speedup of the 5-stage pipelined processor relative to the single-stage processor. (b) Suppose the pipelined processor incurs a one-cycle stall for 40% of all instructions: i. determine its average CPI; ii. determine its speedup relative to the single-stage processor. (c) Now consider an alternative 4-stage pipelined processor with a clock cycle time of 1.1 ns and no stalls: i. determine the execution time per instruction for both pipelined processors; ii. determine which implementation delivers better performance and explain why.
(c) 5 级更好,快 10%。两者 CPI 都是 1,执行时间只由时钟周期决定,5 级把工作切得更细,时钟更短。
Component delays: instruction fetch 200 ps, register read 100 ps, ALU operation 200 ps, data memory access 200 ps, register write 100 ps. The processor can be single-cycle or a 5-stage pipeline (one stage per component), with no stalls or hazards; the pipelined clock is set by the slowest stage, and the LW instruction (the longest path) sets the single-cycle clock.
(a) Suppose the ALU delay is reduced by 25%: i. the new ALU delay; ii. the LW execution time in the single-cycle processor; iii. the pipelined clock cycle time; iv. the speedup achieved by pipelining, compared with the original speedup. (b) Repeat with the ALU delay increased by 25%.
| ALU | LW 单周期 | 流水时钟 | 加速比 | |
|---|---|---|---|---|
| 原设计 | 200 | 800 ps | 200 ps | 4 |
| (a) −25% | 150 | 750 ps | 200 ps | 3.75 |
| (b) +25% | 250 | 850 ps | 250 ps | 3.4 |
(a) ALU 不是瓶颈,缩短它对流水线没用(时钟仍是 200 ps,由取指和访存决定),但单周期机器变快了,所以相对加速比从 4 降到 3.75。(b) ALU 变成唯一的瓶颈,流水时钟升到 250 ps(流水线慢了 25%),单周期只慢了 6.25%,加速比降到 3.4。结论:流水线的性能由最慢一级决定,优化要针对瓶颈级,各级越均衡越好。
A processor performs a simplified image-processing task. Each image block passes through four stages: load image block from memory 10 ns; apply filter operation 20 ns; compute feature value 15 ns; store result to memory 5 ns. A pipelined implementation is used, and each stage can process only one image block at a time.
(a) Determine the pipeline cycle time. (b) How long does it take to complete the first image block? (c) Determine the total time required to complete 8 image blocks. (d) If the blocks were processed sequentially, determine the total time for 8 blocks. (e) Determine the speedup achieved by pipelining. (f) Explain why the speedup is less than the number of stages.
(f) ① 各级不均衡:最慢一级 20 ns,平均只有 12.5 ns,其他级大部分时间在等滤波级;② 填充流水线的开销(第一块要 50 ns),块数只有 8,这部分占比不小。即使 n 很大,加速比也只能趋近 50/20 = 2.5,而不是 4。
A simple pipelined system consists of three stages: X read input 10 s; Y process input 25 s; Z write output 15 s. Assume that a task must pass through X, Y and Z in sequence; once a stage finishes a task it can immediately begin the next; different stages can work on different tasks simultaneously; there are no resource conflicts or dependencies; the pipeline cycle time is determined by the slowest stage; pipeline register overhead is negligible; a batch job contains 1000 independent tasks.
(The tutorial slide does not list the quantities to compute; the solution works out the standard set.)
三级流水理想加速比是 3,这里只有 2,原因是 Y 太慢(25 s,而平均 16.7 s)。要提速就得把 Y 拆开或加速 Y,见 T10.6。
A simple pipelined system consists of four stages: A read input 22 s; B decode input 38 s; C process input 34 s; D write output 30 s. A batch job contains 1000 independent tasks. The system operates as an ideal pipeline, and the cycle time is determined by the slowest stage. An engineer is allowed to reduce the total execution time across all stages by 24 seconds; the reduction may be distributed across one or more stages. Determine:
i. the original pipeline cycle time and the total time for all 1000 tasks; ii. how the 20-second reduction should be distributed among the stages to maximize throughput; iii. the new cycle time and total time after optimization; iv. the percentage improvement in throughput.
吞吐只看最慢一级。把预算全花在"削峰"上:从最高的一级开始往下削,直到几级一样高("注水法"的反向)。削 A 没有任何用处。
为什么这样最优:吞吐 = 1/最慢级。如果只削 B,削到 34 以下 C 就成了新瓶颈;把 B、C、D 一起削到同一高度,24 s 预算换来的最大级时间最低。A 本来就比别人快,削它完全浪费。
Consider a 5-stage DLX pipeline IF → ID → EX → MEM → WB with stage delays IF 220 ps, ID 180 ps, EX 250 ps, MEM 200 ps, WB 150 ps.
(a) Determine the pipeline clock period. (b) Determine the latency of a single instruction in the pipelined processor. (c) Determine the throughput after the pipeline is filled. (d) A student claims: "Since the pipelined instruction latency is larger than the single-cycle instruction latency, pipelining makes the processor slower." Do you agree? Explain using latency and throughput. (e) Determine the theoretical maximum speedup if all stages could be perfectly balanced.
(d) 不对。延迟描述的是一条指令从进到出要多久,流水线确实让它从 1000 ps 变成 1250 ps(每级都被拉长到 250 ps)。但程序有成千上万条指令,决定程序运行时间的是吞吐量:单周期每 1000 ps 完成 1 条(10⁹ 条/秒),流水线每 250 ps 完成 1 条(4 × 10⁹ 条/秒),大程序快 4 倍。
Registers: R1 = 100, R2 = 20, R3 = 5, R4 = 200. Memory: [120] = 50, [208] = 80. Instructions: LW R5, 20(R1), ADD R6, R2, R3, ADDI R7, R2, #10, BEQZ R3, LOOP, J EXIT.
(a) For each instruction, identify 1. the format (R-type, I-type or J-type) and 2. the addressing mode. (b) Determine the final value produced by each instruction (each executes independently). (c) For LW R5, 20(R1), identify the meaning of the opcode, RS1, RD and the immediate field. (d) Explain why ADD R8, R2, 20(R1) is illegal in DLX and rewrite it using valid DLX instructions.
LW R5, 20(R1)、ADD R6, R2, R3、ADDI R7, R2, #10、BEQZ R3, LOOP、J EXIT。(a) 每条的格式和寻址模式;(b) 各自独立执行的结果;(c) LW 的 Opcode、RS1、RD、立即数分别是什么;(d) 为什么 ADD R8, R2, 20(R1) 非法?改写。| 指令 | 格式 | 寻址模式 | 结果 |
|---|---|---|---|
| LW R5, 20(R1) | I 型 | 偏移(基址)寻址 | EA = 100 + 20 = 120 → R5 ← Mem[120] = 50 |
| ADD R6, R2, R3 | R 型 | 寄存器寻址 | R6 ← 20 + 5 = 25 |
| ADDI R7, R2, #10 | I 型 | 立即数寻址 | R7 ← 20 + 10 = 30 |
| BEQZ R3, LOOP | I 型 | PC 相对(立即数作位移);条件看寄存器 | R3 = 5 ≠ 0 → 不跳转,PC ← PC + 4 |
| J EXIT | J 型 | PC 相对(26 位偏移加到 PC) | 无条件跳到 EXIT,不读写寄存器 |
(c) Opcode = LW(取字);RS1 = R1,基址寄存器;RD = R5,接收数据的目的寄存器(在 I 型编码里放在 Rt 字段);立即数 = 20,地址偏移。
(d) DLX 是 load-store 架构,ALU 指令的操作数只能是寄存器(或立即数),不能直接用内存操作数。改写为:
对应 Lesson 11。IF:IR ← Mem[PC],NPC ← PC + 4;ID:A、B、Imm、b;EX:ALUOut;MEM:LMD 或写存储器或更新 PC;WB:写 Rd/Rt。学习手册的"DLX 数据通路步进器"里有本 Tutorial 的预设,可以逐拍对照。
Consider the DLX instruction ADD R7, R3, R4 with R3 = 25, R4 = 17 and PC = 200. For each pipeline stage (IF, ID, EX, MEM, WB): 1. describe the operation performed; 2. determine the key outputs generated; 3. state whether data memory is accessed. Use a table with columns Stage, Operation Performed, Key Output.
ADD R7, R3, R4,R3 = 25,R4 = 17,PC = 200。对每一级写出所做操作、关键输出、是否访问数据存储器。| 级 | 操作 | 关键输出 | 数据存储器 |
|---|---|---|---|
| IF | IR ← Mem[200];NPC ← PC + 4 | IR = ADD R7,R3,R4;NPC = 204 | 否(读的是指令存储器) |
| ID | 译码;A ← Regs[R3],B ← Regs[R4];目的号 b ← Rd = R7 | A = 25,B = 17,b = R7 | 否 |
| EX | ALUOut ← A + B(MUX 选 B) | ALUOut = 42 | 否 |
| MEM | 不访存,ALUOut 直接传到 M/W | ALUOut = 42 | 否 |
| WB | Regs[R7] ← ALUOut | R7 = 42 | 否 |
Refer to the 5-stage DLX datapath and consider ADDI R5, R2, #-12 with R2 = 40. Determine: 1. the 16-bit two's complement representation of −12; 2. the 32-bit sign-extended value; 3. the value of A after the ID stage; 4. the value of Imm after sign extension; 5. the value of ALUOut after the EX stage; 6. the final value written into R5 during WB. Show all working and intermediate results.
ADDI R5, R2, #-12,R2 = 40。求 −12 的 16 位补码、32 位符号扩展值、ID 后的 A、符号扩展后的 Imm、EX 后的 ALUOut、WB 写入 R5 的值。Register and memory contents: R2 = 500, R3 = 88, Mem[520] = 123. Consider LW R1,20(R2) and SW 20(R2),R3. For each instruction determine: 1. the effective address; 2. ALUOut; 3. whether memory is read or written; 4. the value of LMD (if any); 5. the final value stored in memory or register.
LW R1, 20(R2) 和 SW 20(R2), R3 分别写出有效地址、ALUOut、读还是写存储器、LMD、最终存入存储器或寄存器的值。| LW R1, 20(R2) | SW 20(R2), R3 | |
|---|---|---|
| 有效地址 | 500 + 20 = 520 | 500 + 20 = 520 |
| ALUOut | 520 | 520 |
| 存储器 | 读 | 写 |
| LMD | 123 | 无(store 不用 LMD) |
| 最终结果 | R1 ← 123(WB) | Mem[520] ← R3 = 88(MEM),不写回寄存器 |
SW 的 B 来自 R3(要存的数据),经 D/A、A/M 一路传到 MEM 级。如果两条按顺序执行,SW 会把 123 覆盖成 88。
Refer to the 5-stage DLX datapath and consider LW R8,40(R4). The pipeline uses the inter-stage buffers F/D, D/A, A/M and M/W. Assume R4 = 1000 and Memory[1040] = 250. For each pipeline buffer, identify the information that must be stored after the corresponding stage, and complete the table (Buffer, Information Stored).
LW R8, 40(R4),R4 = 1000,Mem[1040] = 250。写出每个级间缓冲 F/D、D/A、A/M、M/W 在对应级结束后存了什么。| 缓冲 | 存的内容 |
|---|---|
| F/D | IR = LW R8, 40(R4);NPC = PC + 4 |
| D/A | A = Regs[R4] = 1000;Imm = 40(符号扩展后);b = R8(目的寄存器号);OP = LW;B = R8 的旧值(读了但不用);NPC |
| A/M | ALUOut = 1000 + 40 = 1040(有效地址);b = R8;OP = LW |
| M/W | LMD = Mem[1040] = 250;b = R8;OP = LW(ALUOut 也一并传下) |
WB 级:WB MUX 选 LMD,R8 ← 250。讲义的缓冲清单里 D/A 还列了 Cond 和 NPC,A/M 有 NPC、cond,这些对 LW 没有意义,写上也不扣分。
Consider the instruction sequence LW R1,0(R2); ADD R3,R4,R5; ADDI R6,R7,#10; SW 20(R8),R9; BEQZ R10,LOOP. The 5 DLX stages are IF → ID → EX → MEM → WB; assume ideal pipelining with no hazards or stalls. Draw the pipeline timing diagram showing the stage occupied by each instruction during every clock cycle (rows: cycle 1, 2, …; columns: I1–I5).
LW R1,0(R2)、ADD R3,R4,R5、ADDI R6,R7,#10、SW 20(R8),R9、BEQZ R10,LOOP。理想流水、无冒险,画出每个周期每条指令所在的级。| 周期 | I1 LW | I2 ADD | I3 ADDI | I4 SW | I5 BEQZ |
|---|---|---|---|---|---|
| 1 | IF | ||||
| 2 | ID | IF | |||
| 3 | EX | ID | IF | ||
| 4 | MEM | EX | ID | IF | |
| 5 | WB | MEM | EX | ID | IF |
| 6 | WB | MEM | EX | ID | |
| 7 | WB | MEM | EX | ||
| 8 | WB | MEM | |||
| 9 | WB |
共 5 + 5 − 1 = 9 个周期。第 5 周期五级全满。SW 和 BEQZ 的 WB 级什么也不写,但仍占一个周期(固定五级结构)。这五条指令之间没有寄存器依赖,所以理想流水成立。
Consider a processor with five pipeline stages IF → ID → EX → MEM → WB with delays IF 250 ns, ID 350 ns, EX 150 ns, MEM 300 ns, WB 200 ns. The unpipelined design executes one instruction at a time; the pipelined design uses the same five stages; hazards, stalls and branch penalties are ignored; pipeline register overhead is negligible unless stated otherwise. Determine: a) the average instruction execution time of the unpipelined processor; b) the average instruction execution time of the pipelined processor; c) the speedup achieved by pipelining; d) the theoretical maximum speedup of this 5-stage pipeline if overhead is ignored and all stages are perfectly balanced. Show all calculations.
Consider LW R1, 20(R2) with R2 = 100 and Memory[120] = 55. For each pipeline register (F/D, D/A, A/M, M/W), determine the contents stored at the end of the cycle.
LW R1, 20(R2),R2 = 100,Mem[120] = 55。写出每个流水线寄存器在该周期结束时的内容。| 寄存器 | 内容 |
|---|---|
| F/D | IR = LW R1, 20(R2);NPC = PC + 4 |
| D/A | A = 100;Imm = 20;b = R1;OP = LW(B = R1 旧值,不用) |
| A/M | ALUOut = 120;b = R1;OP = LW |
| M/W | LMD = 55;b = R1;OP = LW |
WB:R1 ← 55。
Assume: unpipelined CPI = 4.6, clock cycle = 0.8 ns; pipelined CPI = 1.2, clock cycle = 1.0 ns. Determine 1. the average instruction execution time (unpipelined); 2. the average instruction execution time (pipelined); 3. the pipeline speedup.
流水线的时钟反而更长(1.0 vs 0.8 ns,流水线寄存器和时钟偏移带来的开销),但 CPI 从 4.6 降到 1.2,整体仍快 3 倍。
对应 Lesson 12。CPI = 1 + 平均停顿;分支附加 CPI = 分支频率 × 平均分支代价;转发后 ALU→ALU 无停顿,load-use 仍停 1 拍;讲义口径:EX 判定分支代价 3,ID 判定代价 1。
For an R4000-style pipeline, the branch target address is known after 3 pipeline stages and the branch condition is evaluated 1 cycle later; assume no stalls due to register dependencies in the branch comparison. Branch penalties (unconditional / conditional not taken / conditional taken): flush pipeline 2 / 3 / 3; predict taken 2 / 3 / 2; predict not taken 2 / 0 / 3. SPEC branch frequencies: unconditional 4%, conditional not taken 6%, conditional taken 10%. Find the effective addition to CPI due to branches for each branch handling scheme.
附加 CPI = Σ(每类分支频率 × 该类代价)。预测不跳转最好:不跳的 6% 完全没有代价。如果理想 CPI = 1,三者的 CPI 分别是 1.56、1.46、1.38。
The PowerPC 601 has a 6-stage pipeline: Fetch → Dispatch → Decode → Execute → Cache Access → Write. A branch or jump is detected in Fetch; the target address for a jump or conditional branch is available at the end of Decode; the conditional branch outcome is decided in Execute. Benchmark profile: conditional branches 20% of all instructions; jumps or calls 5%; 60% of conditional branches are taken.
a) Find the expected CPI if the CPU stalls until the branch outcome is known. b) Suppose two-slot delayed branching is added for unconditional branches. If 60% of the delay slots are filled with useful instructions, what is the new CPI? Assume conditional branches use the "Predict Not Taken" strategy.
某个信息在第 k 级结束时可用,下一条正确指令最早在第 k + 1 个周期取指,代价 = k − 1。Decode 是第 3 级,Execute 是第 4 级。
Assume delayed branching is implemented: the instruction immediately after the branch is always executed before the branch takes effect. Reorder the following code to make better use of the branch delay slot. Assume R3 is initialized to 8 before the loop starts; the code may need to be slightly modified to preserve correctness.
Loop: LW R2, 100(R3) / SW 200(R3), R2 / SUBI R3, R3, #4 / BNEZ R3, Loop / NOP ; branch delay slot
SUBI 不能挪进槽,因为 BNEZ 要用它的结果。看看 SW 能不能挪到分支后面,挪过去之后 R3 已经被减了 4。
原循环每轮把 Mem[R3+100] 复制到 Mem[R3+200]。SW 被挪到 SUBI 之后执行,此时 R3 已经少了 4,所以偏移要加 4 才能写到原来的地址:(R3 − 4) + 204 = R3 + 200。
| 轮 | LW 读 | R3 变为 | BNEZ | 槽里 SW 写 |
|---|---|---|---|---|
| 1 | Mem[108] | 4 | 跳回 Loop | Mem[4 + 204] = Mem[208] ✓ |
| 2 | Mem[104] | 0 | 不跳 | Mem[0 + 204] = Mem[204] ✓ |
和原代码效果相同,每轮从 5 条(含 NOP)减到 4 条,延迟槽做了有用的工作。SW 不影响分支条件,所以 from-before 安全。
A pipelined processor uses a single-port memory shared by instruction fetches and data accesses. 40% of instructions are loads/stores. Assume ideal CPI = 1 and each load/store causes 1 structural stall cycle. a) Compute the average stall cycles per instruction. b) Determine the effective CPI. c) Determine the speedup relative to a non-pipelined processor with CPI = 5.
改用分离的指令/数据存储器(或双端口存储器)就能消除这 0.4 的停顿,加速比回到 5。
For each instruction pair, identify the hazard type (RAW, WAR, WAW, or None): 1. ADD R1,R2,R3 / SUB R4,R1,R5; 2. ADD R1,R2,R3 / LW R2,0(R5); 3. LW R1,0(R2) / ADD R1,R3,R4; 4. ADD R1,R2,R3 / OR R4,R5,R6. For each dependency, state whether it can occur in a basic DLX pipeline.
ADD R1,R2,R3 / SUB R4,R1,R5ADD R1,R2,R3 / LW R2,0(R5)LW R1,0(R2) / ADD R1,R3,R4ADD R1,R2,R3 / OR R4,R5,R6| # | 类型 | 原因 | DLX 会发生吗 |
|---|---|---|---|
| 1 | RAW(R1) | ADD 写 R1,SUB 读 R1 | 会;靠转发解决,0 停顿 |
| 2 | WAR(R2) | ADD 读 R2,LW 写 R2 | 不会:读在 ID,写在 WB,读永远先发生 |
| 3 | WAW(R1) | 两条都写 R1 | 不会:都在 WB 写,按程序顺序写回 |
| 4 | None | 寄存器没有交集 | — |
Consider LW R1,0(R2); ADD R3,R1,R4; SUB R5,R3,R6; AND R7,R5,R8 on a DLX pipeline with forwarding. a) Identify all RAW hazards. b) Determine which hazards can be eliminated by forwarding. c) Determine the number of stall cycles required. d) Draw the pipeline timing diagram.
LW R1,0(R2)、ADD R3,R1,R4、SUB R5,R3,R6、AND R7,R5,R8,DLX 有转发。(a) 找出所有 RAW;(b) 哪些能被转发消除;(c) 需要几个停顿周期;(d) 画时序图。(a) 三个 RAW:LW → ADD(R1)、ADD → SUB(R3)、SUB → AND(R5)。
(b) ADD → SUB、SUB → AND 是 ALU 到 ALU,从 EX/MEM 转发即可,无停顿。LW → ADD 是 load-use:数据在 LW 的 MEM 结束才出来,ADD 的 EX 已经在同一周期开始,转发来不及。
(c) 需要 1 个停顿周期,硬件互锁在 ADD 的 ID 之后插入一个气泡。
| 指令 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| LW R1,0(R2) | IF | ID | EX | MEM | WB | ||||
| ADD R3,R1,R4 | IF | ID | stall | EX | MEM | WB | |||
| SUB R5,R3,R6 | IF | stall | ID | EX | MEM | WB | |||
| AND R7,R5,R8 | stall | IF | ID | EX | MEM | WB |
LW 的结果在第 4 周期末出来,第 5 周期转发给 ADD 的 EX(MEM/WB → EX);ADD 的结果第 5 周期末出来,第 6 周期转发给 SUB;SUB 第 6 周期末出来,第 7 周期转发给 AND。共 4 + 4 + 1 = 9 个周期。
Consider LW R1,0(R2); ADD R3,R1,R4; BEQZ R3,Target; OR R5,R6,R7. Assume forwarding is available, the branch is resolved in the EX stage, and there is no branch prediction. a) Draw the pipeline timing diagram. b) Identify all data hazards. c) Identify all control hazards. d) Determine the total number of stall cycles.
LW R1,0(R2)、ADD R3,R1,R4、BEQZ R3,Target、OR R5,R6,R7。有转发;分支在 EX 级判定;没有分支预测。(a) 画时序图;(b) 所有数据冒险;(c) 所有控制冒险;(d) 总停顿周期。(b) 数据冒险:LW → ADD(R1,load-use,停 1 拍);ADD → BEQZ(R3,分支在 EX 比较,ADD 的结果从 EX/MEM 转发过来,0 停顿)。
(c) 控制冒险:BEQZ 在 EX 才知道跳不跳,没有预测就只能等。按讲义口径(原始 DLX,EX 判定)分支代价 3 周期,下一条(OR 或 Target 处的指令)要等 BEQZ 的结果出来后才取指。
| 指令 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| LW R1,0(R2) | IF | ID | EX | MEM | WB | |||||||
| ADD R3,R1,R4 | IF | ID | stall | EX | MEM | WB | ||||||
| BEQZ R3,Target | IF | stall | ID | EX | MEM | WB | ||||||
| OR R5,R6,R7 | stall | bubble | bubble | bubble | IF | ID | EX | MEM | WB |
(d) 理想 4 + 4 = 8 个周期,实际 12 个,共 4 个停顿周期:1 个 load-use + 3 个分支。
A benchmark contains 15% branch instructions, of which 60% are taken and 40% not taken. The processor uses the Predict Not Taken scheme, with a branch penalty of 3 cycles when the prediction is incorrect and ideal CPI = 1. a) Compute the average branch penalty. b) Determine the additional CPI caused by branches. c) Determine the effective CPI. d) Compare the result with a processor that always stalls 3 cycles for every branch.
预测不跳转比全停顿快约 14%:40% 不跳的分支省掉了全部代价。不过分支多数会跳(60%),所以预测不跳转猜错的比例较高,这也是硬件转向动态分支预测的原因。
讲义里每个 Exercise 后面的答案页在学生版中是空白的。下面每题都有英文原题和中文翻译,点"解答 · Solution"展开中英双语解答。
A processor has an ideal CPI of 1.2. The instruction mix ratio is 4 : 3 : 1 for arithmetic/logic, load/store and control instructions. The instruction-cache miss rate is 2%, the data-cache miss rate is 15%, and the miss penalty for both is 40 cycles. Compute (1) the total average miss penalty per instruction, (2) the actual CPI, (3) the percentage of execution time spent on memory stalls.
理想 CPI 1.2,算术逻辑 : load/store : 控制 = 4 : 3 : 1。指令缓存缺失率 2%,数据缓存缺失率 15%,两者缺失代价都是 40 周期。求 (1) 每条指令的平均缺失代价总和;(2) 实际 CPI;(3) 存储停顿占执行时间的百分比。
Convert the ratio into a fraction first. With a 15% data miss rate, data stalls dominate.
比例要先换成百分比。数据缺失率高达 15%,所以数据停顿占了大头。
Consider sum = 0; i = 0; while (i < 5) { if (a[i] > 0) sum += a[i]; else sum -= a[i]; i++; } with a = [4, −2, 7, −5, 3]. Assume each array element a[i] used in an iteration costs one memory access, sum and i are in registers, cache access = 1 ms, main-memory access = 10 ms, and the first array access misses while all later ones hit. Determine (1) the final sum, (2) the number of memory accesses, (3) the total access time without a cache, (4) with a cache, (5) the performance improvement factor.
程序:sum = 0; i = 0; while (i < 5) { if (a[i] > 0) sum += a[i]; else sum -= a[i]; i++; },a = [4, −2, 7, −5, 3]。假设每轮用到的数组元素 a[i] 算一次访存,sum、i 在寄存器里;缓存 1 ms,主存 10 ms;第一次访问数组缺失,之后全部命中。求 (1) 最终 sum;(2) 访存次数;(3) 无缓存总时间;(4) 有缓存总时间;(5) 性能提升倍数。
The lecturer's in-class answer (week 7) is 5 accesses, 50 ms, 15 ms, 3.3×: one access per array element. Miss time = cache 1 ms + memory 10 ms = 11 ms (MT = HT + MP). Counting the if test and the add/subtract separately (2 per iteration) would give 10 accesses, 100 ms, 20 ms and 5×; in the exam use the lecturer's count and state the assumption.
老师课上(第 7 周)给的答案就是 5 次、50 ms、15 ms、3.3 倍:每轮按"访问一个数组元素"算一次。缺失时间 = 缓存 1 ms + 主存 10 ms = 11 ms(MT = HT + MP)。如果把 if 判断和加减各算一次(每轮 2 次),会得到 10 次、100 ms、20 ms、5 倍;考试按老师的数法写 5 次,必要时注明假设。
Ideal CPI = 1.5, instruction count IC = 2 × 106, 30% of instructions are loads/stores, the miss rate for both instructions and data is 4%, and the miss penalty is 20 cycles. Each instruction needs one instruction fetch and only loads/stores access data memory. Compute (1) memory references per instruction, (2) memory stall cycles per instruction, (3) total memory stall cycles, (4) actual CPI, (5) total CPU execution cycles.
理想 CPI 1.5,指令数 IC = 2 × 106,load/store 占 30%,指令和数据缺失率都是 4%,缺失代价 20 周期。每条指令取指一次,只有 load/store 访问数据存储器。求 (1) 每条指令访存次数;(2) 每条指令停顿;(3) 总停顿周期;(4) 实际 CPI;(5) CPU 总周期数。
Ideal CPI = 1.5. Instruction mix: 50% arithmetic/logic, 30% load/store, 20% branch/control. I-cache miss rate 1% with a 30-cycle penalty; D-cache miss rate 4% (of data accesses) with a 50-cycle penalty. Compute the I-cache stall cycles per instruction, the D-cache stall cycles per instruction, the total memory stall cycles per instruction, the actual CPI and the percentage of execution time spent on memory stalls.
理想 CPI 1.5;50% 算术逻辑、30% load/store、20% 分支控制。指令缓存缺失率 1%,代价 30 周期;数据缓存缺失率 4%(按数据访问计),代价 50 周期。求指令停顿、数据停顿、总停顿、实际 CPI 和停顿占比。
Fully associative cache: main memory 256 bytes, 4 cache lines, 4-byte blocks, LRU replacement, initially empty. Reference sequence: 08, 124, 77, 45, 09, 126, 44, 211, 08, 212, 77, 241. (1) Number of bits for the address, offset and tag. (2) For each reference, update the cache and show the tag and time since last referenced. (3) Hit or miss; for a miss, compulsory or capacity. (4) Total hits, misses and hit rate.
全相联缓存:主存 256 字节,4 行,块 4 字节,LRU 替换,初始为空。访问序列:08, 124, 77, 45, 09, 126, 44, 211, 08, 212, 77, 241。(1) 地址、偏移、标签各几位;(2) 每次访问后的缓存内容(标签与距上次访问次数 TSLR);(3) 命中还是缺失,缺失是强制还是容量;(4) 总命中、总缺失和命中率。
| # | Addr 地址 | Tag Index Offset | Block 块 | Set/Line 组/行 | Tag | Result 结果 | Evicted 被替换 | Cache after 访问后缓存 (TSLR) |
|---|---|---|---|---|---|---|---|---|
| 1 | 8 | 000010 00 | 8–11 | – | 000010 | MISS compulsory | 8–11(0) | |
| 2 | 124 | 011111 00 | 124–127 | – | 011111 | MISS compulsory | 8–11(1), 124–127(0) | |
| 3 | 77 | 010011 01 | 76–79 | – | 010011 | MISS compulsory | 8–11(2), 124–127(1), 76–79(0) | |
| 4 | 45 | 001011 01 | 44–47 | – | 001011 | MISS compulsory | 8–11(3), 124–127(2), 76–79(1), 44–47(0) | |
| 5 | 9 | 000010 01 | 8–11 | – | 000010 | HIT | 8–11(0), 124–127(3), 76–79(2), 44–47(1) | |
| 6 | 126 | 011111 10 | 124–127 | – | 011111 | HIT | 8–11(1), 124–127(0), 76–79(3), 44–47(2) | |
| 7 | 44 | 001011 00 | 44–47 | – | 001011 | HIT | 8–11(2), 124–127(1), 76–79(4), 44–47(0) | |
| 8 | 211 | 110100 11 | 208–211 | – | 110100 | MISS compulsory | 76–79 | 8–11(3), 124–127(2), 208–211(0), 44–47(1) |
| 9 | 8 | 000010 00 | 8–11 | – | 000010 | HIT | 8–11(0), 124–127(3), 208–211(1), 44–47(2) | |
| 10 | 212 | 110101 00 | 212–215 | – | 110101 | MISS compulsory | 124–127 | 8–11(1), 212–215(0), 208–211(2), 44–47(3) |
| 11 | 77 | 010011 01 | 76–79 | – | 010011 | MISS capacity | 44–47 | 8–11(2), 212–215(1), 208–211(3), 76–79(0) |
| 12 | 241 | 111100 01 | 240–243 | – | 111100 | MISS compulsory | 208–211 | 8–11(3), 212–215(2), 240–243(0), 76–79(1) |
(1) Address = log₂256 = 8 bits, offset = log₂4 = 2 bits, no index in a fully associative cache, tag = 8 − 2 = 6 bits.
(4) 4 hits (09, 126, 44, 08) and 8 misses (7 compulsory, 1 capacity: the second access to 77). Hit rate = 4/12 = 33.3%. 09 shares block 8–11 with 08, 126 shares block 124–127 with 124, and 44 shares block 44–47 with 45: these hits come from spatial locality.
(1) 地址 log₂256 = 8 位,偏移 log₂4 = 2 位,全相联没有索引,标签 = 8 − 2 = 6 位。
(4) 命中 4 次(09、126、44、08),缺失 8 次(强制 7、容量 1:最后一次访问 77),命中率 4/12 = 33.3%。09 与 08 同在块 8–11,126 与 124 同在块 124–127,44 与 45 同在块 44–47,这些命中来自空间局部性。
Same memory and sequence as Exercise 8.1, but a direct-mapped cache with 4 lines and 4-byte blocks (no replacement policy needed). (1) Number of bits for the address, offset, index and tag. (2) Update the cache for each reference. (3) Hit or miss; compulsory or conflict. (4) Total hits, misses and hit rate.
Sequence: 08, 124, 77, 45, 09, 126, 44, 211, 08, 212, 77, 241
主存和序列同 Exercise 8.1,改为直接映射缓存,4 行,块 4 字节(不需要替换策略)。(1) 地址、偏移、索引、标签各几位;(2) 每次访问后的缓存内容;(3) 命中还是缺失,缺失是强制还是冲突;(4) 总命中、总缺失和命中率。
序列:08, 124, 77, 45, 09, 126, 44, 211, 08, 212, 77, 241
| # | Addr 地址 | Tag Index Offset | Block 块 | Set/Line 组/行 | Tag | Result 结果 | Evicted 被替换 | Cache after 访问后缓存 |
|---|---|---|---|---|---|---|---|---|
| 1 | 8 | 0000 10 00 | 8–11 | 2 | 0000 | MISS compulsory | L2: 8–11 | |
| 2 | 124 | 0111 11 00 | 124–127 | 3 | 0111 | MISS compulsory | L2: 8–11 L3: 124–127 | |
| 3 | 77 | 0100 11 01 | 76–79 | 3 | 0100 | MISS compulsory | 124–127 | L2: 8–11 L3: 76–79 |
| 4 | 45 | 0010 11 01 | 44–47 | 3 | 0010 | MISS compulsory | 76–79 | L2: 8–11 L3: 44–47 |
| 5 | 9 | 0000 10 01 | 8–11 | 2 | 0000 | HIT | L2: 8–11 L3: 44–47 | |
| 6 | 126 | 0111 11 10 | 124–127 | 3 | 0111 | MISS conflict | 44–47 | L2: 8–11 L3: 124–127 |
| 7 | 44 | 0010 11 00 | 44–47 | 3 | 0010 | MISS conflict | 124–127 | L2: 8–11 L3: 44–47 |
| 8 | 211 | 1101 00 11 | 208–211 | 0 | 1101 | MISS compulsory | L0: 208–211 L2: 8–11 L3: 44–47 | |
| 9 | 8 | 0000 10 00 | 8–11 | 2 | 0000 | HIT | L0: 208–211 L2: 8–11 L3: 44–47 | |
| 10 | 212 | 1101 01 00 | 212–215 | 1 | 1101 | MISS compulsory | L0: 208–211 L1: 212–215 L2: 8–11 L3: 44–47 | |
| 11 | 77 | 0100 11 01 | 76–79 | 3 | 0100 | MISS conflict * | 44–47 | L0: 208–211 L1: 212–215 L2: 8–11 L3: 76–79 |
| 12 | 241 | 1111 00 01 | 240–243 | 0 | 1111 | MISS compulsory | 208–211 | L0: 240–243 L1: 212–215 L2: 8–11 L3: 76–79 |
(1) Address 8 bits, offset 2 bits, index log₂4 = 2 bits, tag 8 − 2 − 2 = 4 bits. Line = block number mod 4.
(4) 2 hits (09, 08) and 10 misses (7 compulsory, 3 conflict). Hit rate = 2/12 = 16.7%. Blocks 31 (124–127), 19 (76–79) and 11 (44–47) all map to line 3 and keep evicting each other. The last access to 77 (marked *) would also miss in a fully associative cache of the same size (it is a capacity miss in Ex 8.1), so strict 3C calls it capacity; this exercise only asks compulsory vs conflict, so it is recorded as conflict.
(1) 地址 8 位,偏移 2 位,索引 log₂4 = 2 位,标签 8 − 2 − 2 = 4 位。行号 = 块号 mod 4。
(4) 命中 2 次(09、08),缺失 10 次(强制 7、冲突 3),命中率 2/12 = 16.7%。块 31(124–127)、19(76–79)、11(44–47)全都映射到行 3,反复互相踢出。标 * 的最后一次 77:同样大小的全相联缓存也会缺失(Ex 8.1 里它是容量缺失),严格按 3C 应记 capacity;本题只要求区分强制与冲突,记 conflict。
Same memory and sequence again, with a 2-way set-associative cache of 4 lines, 4-byte blocks and LRU replacement. (1) Bits for the address, offset, index and tag. (2) Update the cache (tag and time since last referenced). (3) Hit or miss; compulsory or conflict. (4) Total hits, misses and hit rate.
Sequence: 08, 124, 77, 45, 09, 126, 44, 211, 08, 212, 77, 241
主存和序列不变,改为 2 路组相联,4 行,块 4 字节,LRU。(1) 地址、偏移、索引、标签各几位;(2) 每次访问后的缓存内容(标签与 TSLR);(3) 命中还是缺失,强制还是冲突;(4) 总命中、总缺失和命中率。
序列:08, 124, 77, 45, 09, 126, 44, 211, 08, 212, 77, 241
| # | Addr 地址 | Tag Index Offset | Block 块 | Set/Line 组/行 | Tag | Result 结果 | Evicted 被替换 | Cache after 访问后缓存 (TSLR) |
|---|---|---|---|---|---|---|---|---|
| 1 | 8 | 00001 0 00 | 8–11 | 0 | 00001 | MISS compulsory | S0: 8–11(0) | |
| 2 | 124 | 01111 1 00 | 124–127 | 1 | 01111 | MISS compulsory | S0: 8–11(1) S1: 124–127(0) | |
| 3 | 77 | 01001 1 01 | 76–79 | 1 | 01001 | MISS compulsory | S0: 8–11(2) S1: 124–127(1), 76–79(0) | |
| 4 | 45 | 00101 1 01 | 44–47 | 1 | 00101 | MISS compulsory | 124–127 | S0: 8–11(3) S1: 44–47(0), 76–79(1) |
| 5 | 9 | 00001 0 01 | 8–11 | 0 | 00001 | HIT | S0: 8–11(0) S1: 44–47(1), 76–79(2) | |
| 6 | 126 | 01111 1 10 | 124–127 | 1 | 01111 | MISS conflict | 76–79 | S0: 8–11(1) S1: 44–47(2), 124–127(0) |
| 7 | 44 | 00101 1 00 | 44–47 | 1 | 00101 | HIT | S0: 8–11(2) S1: 44–47(0), 124–127(1) | |
| 8 | 211 | 11010 0 11 | 208–211 | 0 | 11010 | MISS compulsory | S0: 8–11(3), 208–211(0) S1: 44–47(1), 124–127(2) | |
| 9 | 8 | 00001 0 00 | 8–11 | 0 | 00001 | HIT | S0: 8–11(0), 208–211(1) S1: 44–47(2), 124–127(3) | |
| 10 | 212 | 11010 1 00 | 212–215 | 1 | 11010 | MISS compulsory | 124–127 | S0: 8–11(1), 208–211(2) S1: 44–47(3), 212–215(0) |
| 11 | 77 | 01001 1 01 | 76–79 | 1 | 01001 | MISS conflict * | 44–47 | S0: 8–11(2), 208–211(3) S1: 76–79(0), 212–215(1) |
| 12 | 241 | 11110 0 01 | 240–243 | 0 | 11110 | MISS compulsory | 208–211 | S0: 8–11(3), 240–243(0) S1: 76–79(1), 212–215(2) |
(1) 4 lines ÷ 2 ways = 2 sets: address 8 bits, offset 2 bits, index 1 bit, tag 8 − 1 − 2 = 5 bits. Set = block number mod 2.
(4) 3 hits (09, 44, 08) and 9 misses (7 compulsory, 2 conflict). Hit rate = 3/12 = 25%. Comparing the three organizations: fully associative 33.3% > 2-way 25% > direct-mapped 16.7%; higher associativity removes conflict misses.
(1) 4 行 ÷ 2 路 = 2 组:地址 8 位,偏移 2 位,索引 1 位,标签 8 − 1 − 2 = 5 位。组号 = 块号 mod 2。
(4) 命中 3 次(09、44、08),缺失 9 次(强制 7、冲突 2),命中率 3/12 = 25%。三种组织对比:全相联 33.3% > 2 路 25% > 直接映射 16.7%,相联度越高,冲突缺失越少。
A cache has a capacity of 32 KB and a block size of 256 bytes, with 32-bit addresses. Determine the number of tag, index and block-offset bits for (1) a direct-mapped cache, (2) a four-way set-associative cache, (3) a fully associative cache.
缓存容量 32 KB,块 256 字节,32 位地址。分别求 (1) 直接映射;(2) 4 路组相联;(3) 全相联时的标签、索引、块内偏移位数。
Pattern: with capacity and block size fixed, doubling the associativity removes one index bit and adds one tag bit.
规律:容量和块大小不变时,相联度每翻一倍,索引少 1 位、标签多 1 位。
For the logic circuit below, (a) determine and clearly show the critical path delay using the gate delays NOT = 9, AND = 13, OR = 11, XOR = 23; (b) compute the latency and throughput of the circuit.
对下面的电路:(a) 用门延迟 NOT = 9、AND = 13、OR = 11、XOR = 23 找出并标明关键路径延迟;(b) 求电路的延迟和吞吐量。
Boxes show gate type · delay 方框内为门类型 · 延迟
Highlighted = critical path 高亮为关键路径;t = arrival time 到达时间
| Path 路径 | Delay 延迟 |
|---|---|
| A → OR → XOR | 11 + 23 = 34 |
| C/D → OR → NOT → OR → XOR | 11 + 9 + 11 + 23 = 54 |
| B/C → AND → OR → XOR | 13 + 11 + 23 = 47 |
| B/D → XOR → OR → XOR | 23 + 11 + 23 = 57 |
Critical path: B (or D) → XOR → OR → XOR → Z, delay = 57 time units. One result is produced every 57 units, so throughput = 1/57 computations per unit time.
Method: label each gate output with its earliest settle time = latest input settle time + gate delay; the time at the output is the latency.
关键路径:B(或 D)→ XOR → OR → XOR → Z,延迟 = 57 个时间单位。电路每 57 个单位完成一次计算,吞吐量 = 1/57(每单位时间完成的计算次数)。
方法:给每个门的输出标"最早稳定时间" = 各输入最晚稳定时间 + 本门延迟,输出端的时间就是延迟。
For the circuit below (a small circle at a gate input is an inverter, i.e. a NOT gate), (a) determine and clearly show the critical path delay using NOT = 10, AND = 15, OR = 17; (b) compute the latency and throughput.
对下面的电路(门输入端的小圆圈表示非门):(a) 用 NOT = 10、AND = 15、OR = 17 找出并标明关键路径延迟;(b) 求延迟和吞吐量。
Boxes show gate type · delay 方框内为门类型 · 延迟
Highlighted = critical path 高亮为关键路径;t = arrival time 到达时间
Latency = 74, throughput = 1/74. Do not forget the NOT delay hidden in the input bubbles.
延迟 74,吞吐量 1/74。别漏掉输入端小圆圈代表的非门延迟。
Stage delays: IF 180 ps, ID 220 ps, EX 250 ps, MEM 300 ps, WB 150 ps. (a) For a single-cycle processor, find the instruction latency and the throughput. (b) For a 5-stage pipeline, find the clock period, the instruction latency and the throughput once the pipeline is full. (c) For a very large program, find the speedup of pipelining. (d) Explain why the speedup is less than the number of stages.
各级延迟:IF 180、ID 220、EX 250、MEM 300、WB 150 ps。(a) 单周期处理器的单条延迟和吞吐量;(b) 五级流水线的时钟周期、单条延迟、填满后的吞吐量;(c) 程序很大时的加速比;(d) 为什么加速比小于级数?
(d) The stages are unbalanced: the clock is set by the slowest stage (MEM, 300 ps) while the others idle (the average stage is only 220 ps). Pipeline-register overhead, fill/drain time and hazard stalls reduce it further.
(d) 各级不均衡:时钟由最慢的 MEM(300 ps)决定,其他级在空等(平均每级只有 220 ps)。此外还有流水线寄存器开销、填充排空和冒险停顿。
R1 = 10, R2 = 20, R3 = 5, R4 = 100. Determine the value written to the destination register (each instruction executes independently): (a) ADD R5, R1, R2 (b) SUB R6, R2, R3 (c) ADDI R7, R1, 15
R1 = 10、R2 = 20、R3 = 5、R4 = 100。各指令独立执行,求写入目的寄存器的值:(a) ADD R5, R1, R2 (b) SUB R6, R2, R3 (c) ADDI R7, R1, 15
(a) R5 = 10 + 20 = 30 (b) R6 = 20 − 5 = 15 (c) R7 = 10 + 15 = 25
(a) R5 = 10 + 20 = 30 (b) R6 = 20 − 5 = 15 (c) R7 = 10 + 15 = 25
For LW R1, 0(R2), ADD R3, R1, R4, SW R3, 8(R2), BEQZ R3, LOOP on the 5-stage DLX pipeline (IF → ID → EX → MEM → WB): identify R-type or I-type, the stages used, whether a register is written back, and briefly describe the operation.
对 LW R1, 0(R2)、ADD R3, R1, R4、SW R3, 8(R2)、BEQZ R3, LOOP(五级 DLX):判断 R 型或 I 型、用到的流水级、是否写回寄存器,并简述操作。
| Instruction 指令 | Type 类型 | Stages 流水级 | Writes reg? 写寄存器 | Operation 操作 |
|---|---|---|---|---|
LW R1,0(R2) | I | IF ID EX MEM WB | Yes 是(R1) | R1 ← Mem[R2 + 0] |
ADD R3,R1,R4 | R | IF ID EX (MEM pass) WB | Yes 是(R3) | R3 ← R1 + R4 |
SW R3,8(R2) | I | IF ID EX MEM | No 否 | Mem[R2 + 8] ← R3 |
BEQZ R3,LOOP | I | IF ID EX MEM (branch completion) | No 否 | if R3 = 0 then PC ← LOOP |
Only LW uses all five stages for real work. ADD passes through MEM doing nothing; SW writes memory but no register; BEQZ completes the branch in MEM (updates the PC) and writes no register.
只有 LW 走满五级并且真正用到 MEM 和 WB;ADD 的 MEM 级空过;SW 写存储器但不写寄存器;BEQZ 在 MEM 级完成分支(更新 PC),也不写回。
R1 = 10, R2 = 100, R3 = 20, R4 = 5, Mem[104] = 30. Program: LW R5, 4(R2); ADD R6, R5, R1; SUB R7, R6, R4; SW R7, 8(R2). (a) Effective address of LW. (b) Value loaded into R5. (c) Values in R6 and R7. (d) Memory location and value written by SW. (e) Pipeline stages used by each instruction.
R1 = 10、R2 = 100、R3 = 20、R4 = 5,Mem[104] = 30。程序:LW R5, 4(R2);ADD R6, R5, R1;SUB R7, R6, R4;SW R7, 8(R2)。求 (a) LW 的有效地址;(b) R5;(c) R6、R7;(d) SW 写到哪个地址、写什么值;(e) 每条用到的流水级。
(e) LW: IF ID EX MEM WB; ADD and SUB: IF ID EX WB (MEM passes through); SW: IF ID EX MEM.
(e) LW:IF ID EX MEM WB;ADD、SUB:IF ID EX WB(MEM 空过);SW:IF ID EX MEM。
Identify whether each instruction uses the R-type, I-type or J-type format: ADD R1,R2,R3, LW R4,8(R5), J LOOP, ADDI R6,R7,#10, SUB R8,R9,R10, BEQZ R1,EXIT, JAL FUNCTION, SW R3,16(R4).
判断格式(R / I / J):ADD R1,R2,R3、LW R4,8(R5)、J LOOP、ADDI R6,R7,#10、SUB R8,R9,R10、BEQZ R1,EXIT、JAL FUNCTION、SW R3,16(R4)。
R-type: ADD, SUB (three registers). I-type: LW, ADDI, BEQZ, SW (register + immediate / offset / displacement). J-type: J, JAL (26-bit offset only).
R 型:ADD、SUB(三个寄存器)。I 型:LW、ADDI、BEQZ、SW(寄存器 + 立即数/偏移/位移)。J 型:J、JAL(只有 26 位偏移)。
For each instruction, fill in the fields used by its format (Opcode, Rs, Rt, Rd, Immediate/Target): ADDI R1,R2,#12, LW R3,40(R4), ADD R5,R6,R7, J LOOP, plus SUB R8,R9,R10 and BEQZ R1,EXIT.
填写每条指令格式中用到的字段(Opcode、Rs、Rt、Rd、立即数/目标):ADDI R1,R2,#12、LW R3,40(R4)、ADD R5,R6,R7、J LOOP,以及 SUB R8,R9,R10、BEQZ R1,EXIT。
| Instruction 指令 | Opcode | Rs | Rt | Rd | Imm / Target 立即数/目标 |
|---|---|---|---|---|---|
ADDI R1,R2,#12 | ADDI | R2 | R1 | — | 12 |
LW R3,40(R4) | LW | R4 | R3 | — | 40 |
ADD R5,R6,R7 | ALU (func = ADD) | R6 | R7 | R5 | — |
SUB R8,R9,R10 | ALU (func = SUB) | R9 | R10 | R8 | — |
BEQZ R1,EXIT | BEQZ | R1 | — | — | offset to EXIT 到 EXIT 的位移 |
J LOOP | J | — | — | — | LOOP (26-bit offset) |
In I-type instructions the role of Rt depends on the instruction: for ADDI and LW it is the destination. For R-type, the opcode only says "ALU operation"; the function field selects the exact operation.
I 型里 Rt 的角色随指令变化:ADDI、LW 中 Rt 是目的寄存器。R 型的 Opcode 只说明是 ALU 类,具体运算由 Function 字段指定。
Program: LW R1, 0(R2); ADD R3, R1, R4; SW R3, 8(R2); J DONE. For each instruction state the format and whether it reads memory, writes memory and writes a register.
程序:LW R1, 0(R2);ADD R3, R1, R4;SW R3, 8(R2);J DONE。对每条写出格式,以及是否读存储器、写存储器、写寄存器。
| Instruction 指令 | Format 格式 | Reads mem 读存储器 | Writes mem 写存储器 | Writes reg 写寄存器 |
|---|---|---|---|---|
LW R1,0(R2) | I | Yes 是 | No 否 | Yes 是 |
ADD R3,R1,R4 | R | No 否 | No 否 | Yes 是 |
SW R3,8(R2) | I | No 否 | Yes 是 | No 否 |
J DONE | J | No 否 | No 否 | No 否 |
"Memory" here means data memory; every instruction still reads instruction memory in IF.
这里的"读写存储器"指数据存储器;每条指令在 IF 都要读指令存储器。
Consider ADDI R5, R2, #12 with R2 = 20. For each stage (IF, ID, EX, MEM, WB): describe the operation, give the key signal values, and state whether memory is accessed.
ADDI R5, R2, #12,R2 = 20。对每一级(IF、ID、EX、MEM、WB)写出所做操作、关键信号值,以及是否访问存储器。
| Stage 级 | Operation 操作 | Key output 关键输出 | Memory 存储器 |
|---|---|---|---|
| IF | IR ← Mem[PC]; NPC ← PC + 4 | IR = ADDI R5,R2,#12; NPC = PC + 4 | Instruction memory 指令存储器 |
| ID | A ← Regs[R2]; Imm ← SignExt(12); b ← Rt = R5 | A = 20, Imm = 12, b = R5 | — |
| EX | ALUOut ← A + Imm (MUX selects Imm) | ALUOut = 32 | — |
| MEM | no data access, pass ALUOut 不访存,直接传递 | ALUOut = 32 | No 否 |
| WB | Regs[R5] ← ALUOut | R5 = 32 | — |
Data memory is never accessed; only IF reads instruction memory. For an I-type ALU instruction the destination register is Rt (R5).
数据存储器全程没有被访问,只有 IF 读了指令存储器。I 型运算指令的目的寄存器是 Rt(R5)。
LW R1, 30(R2) with R2 = 1000 and Mem[1030] = 500. Determine (1) A after ID, (2) Imm after sign extension, (3) ALUOut after EX, (4) the memory location accessed in MEM, (5) the value loaded into LMD, (6) the value written into R1 in WB.
LW R1, 30(R2),R2 = 1000,Mem[1030] = 500。求 (1) ID 后的 A;(2) 符号扩展后的 Imm;(3) EX 后的 ALUOut;(4) MEM 访问的地址;(5) LMD;(6) WB 写入 R1 的值。
(1) A = 1000 (2) Imm = 30 (3) ALUOut = 1030 (4) address 1030 (5) LMD = 500 (6) R1 = 500
(1) A = 1000 (2) Imm = 30 (3) ALUOut = 1030 (4) 地址 1030 (5) LMD = 500 (6) R1 = 500
For LW R1, 30(R2), identify the information that must be stored in each inter-stage register (F/D, D/A, A/M, M/W) so that the next stage can continue.
对 LW R1, 30(R2),写出每个级间寄存器(F/D、D/A、A/M、M/W)必须保存的信息,使下一级能继续执行。
| Register 寄存器 | Contents 内容 |
|---|---|
| F/D | IR (the instruction 指令本身), NPC |
| D/A | A = Regs[R2], Imm = 30, b = R1 (destination 目的号), OP = LW (also B, NPC) |
| A/M | ALUOut = R2 + 30 (effective address 有效地址), b, OP |
| M/W | LMD = Mem[R2 + 30], b, OP (ALUOut) |
Each buffer keeps what the next stage needs plus what later stages still need. The destination register number b and OP travel all the way to WB, where the write happens.
每个缓冲存"下一级要用、后面还要接着用"的东西。目的寄存器号 b 和 OP 一路带到 WB,因为写回时才用。
For ADD R1,R2,R3 and LW R1,40(R2): which stages are used, is data memory accessed, what does the ALU do, what value is written back, and which MUX input is selected in EX?
对 ADD R1,R2,R3 和 LW R1,40(R2):用到哪些级?是否访问数据存储器?ALU 做什么?写回什么值?EX 级的 MUX 选哪个输入?
ADD R1,R2,R3 | LW R1,40(R2) | |
|---|---|---|
| Stages 级 | IF ID EX (MEM) WB | IF ID EX MEM WB |
| ALU inputs ALU 输入 | A = R2, B = R3 (MUX → B) | A = R2, Imm = 40 (MUX → Imm) |
| ALU operation ALU 操作 | add → result 加法得结果 | add → effective address 加法得有效地址 |
| Data memory 数据存储器 | No 否 | Read 读 |
| Written back 写回 | ALUOut = R2 + R3 | LMD = Mem[R2 + 40] |
Both add in the ALU; the difference is the second ALU input (B for R-type, Imm for loads/stores) and the WB multiplexer (ALUOut for ADD, LMD for LW).
两条都在 ALU 里做加法,区别在第二个输入:R 型选 B,load/store 选 Imm;写回时 WB 的 MUX 一个选 ALUOut,一个选 LMD。
Register R2 contains 0 and PC = 1000. Consider BEQZ R3, +40. Determine (1) NPC, (2) the ALU inputs, (3) the branch target address, (4) the output of the Zero? block, (5) whether the branch is taken, (6) the new PC.
R2 = 0,PC = 1000。对 BEQZ R3, +40 求 (1) NPC;(2) ALU 输入;(3) 分支目标地址;(4) Zero? 的输出;(5) 是否跳转;(6) 新 PC。
The question gives R2 = 0 but the branch tests R3, which looks like a typo; assume R3 = 0:
If R3 ≠ 0: Zero? = 0, not taken, new PC = NPC = 1004.
题目给的是 R2 = 0,但指令测试 R3,应是笔误,按 R3 = 0 理解:
若 R3 ≠ 0:Zero? = 0,不跳转,新 PC = NPC = 1004。
R1 = 10, R2 = 100, R3 = 20, Mem[104] = 50. Sequence: LW R4, 4(R2); ADDI R5, R4, #10; ADD R6, R5, R1. For each: the values of A, B (if used) and Imm, ALUOut, the value written back, and the datapath components used.
R1 = 10、R2 = 100、R3 = 20,Mem[104] = 50。序列:LW R4, 4(R2);ADDI R5, R4, #10;ADD R6, R5, R1。对每条写出 A、B(若用到)、Imm、ALUOut、写回值和用到的部件。
| Instruction 指令 | A / B / Imm | ALUOut | Write-back 写回 | Components 部件 |
|---|---|---|---|---|
LW R4,4(R2) | A = 100, Imm = 4 | 104 | R4 ← 50 | IM, RegFile, SignExt, ALU, DM, LMD, WB MUX |
ADDI R5,R4,#10 | A = 50, Imm = 10 | 60 | R5 ← 60 | IM, RegFile, SignExt, ALU (MUX → Imm), WB |
ADD R6,R5,R1 | A = 60, B = 10 | 70 | R6 ← 70 | IM, RegFile, ALU (MUX → B), WB |
Values are computed as if the instructions run one after another. In a real pipeline LW → ADDI is a load-use hazard and ADDI → ADD is a RAW hazard, handled by forwarding and stalls (Lesson 12).
这里按顺序执行、不考虑冒险来算数值。真正流水起来,LW → ADDI 是 load-use,ADDI → ADD 也是 RAW,要靠转发和停顿(Lesson 12)。
Draw a timing diagram for LW R1, 0(R2); ADD R3, R4, R5; ADDI R6, R7, #8; BEQZ R8, LOOP, showing the stage of each instruction in every clock cycle. Assume ideal pipelining with no stalls.
画出 LW R1, 0(R2);ADD R3, R4, R5;ADDI R6, R7, #8;BEQZ R8, LOOP 的时序图,标出每个周期每条指令所在的级。假设理想流水、无停顿。
| Cycle 周期 | I1 LW | I2 ADD | I3 ADDI | I4 BEQZ |
|---|---|---|---|---|
| 1 | IF | |||
| 2 | ID | IF | ||
| 3 | EX | ID | IF | |
| 4 | MEM | EX | ID | IF |
| 5 | WB | MEM | EX | ID |
| 6 | WB | MEM | EX | |
| 7 | WB | MEM | ||
| 8 | WB |
Total 4 + 5 − 1 = 8 cycles. The four instructions share no registers, so ideal pipelining holds.
共 4 + 5 − 1 = 8 个周期。四条指令之间没有寄存器依赖,所以理想流水成立。
R2 = 1000, Mem[1030] = 300, R3 = 25, PC = 4000. Sequence: LW R1, 30(R2); ADDI R4, R1, #5; BEQZ R3, +20. For each: the values entering the ALU, ALUOut, whether data memory is accessed, the value written back (if any), and the final result after WB.
R2 = 1000,Mem[1030] = 300,R3 = 25,PC = 4000。序列:LW R1, 30(R2);ADDI R4, R1, #5;BEQZ R3, +20。对每条写出 ALU 输入、ALUOut、是否访问数据存储器、写回值(若有)和最终结果。
| Instruction 指令 | ALU inputs ALU 输入 | ALUOut | Data mem 数据存储器 | Result 结果 |
|---|---|---|---|---|
LW R1,30(R2) @4000 | A = 1000, Imm = 30 | 1030 | Read 读 | R1 ← 300 |
ADDI R4,R1,#5 @4004 | A = 300, Imm = 5 | 305 | No 否 | R4 ← 305 |
BEQZ R3,+20 @4008 | NPC = 4012, Imm = 20 | 4032 | No 否 | R3 = 25 ≠ 0 → not taken 不跳, PC = 4012 |
BEQZ sits at 4008, so NPC = 4012 and the target is 4012 + 20 = 4032; R3 = 25 is not zero, so the branch is not taken and no register is written.
BEQZ 位于 4008,NPC = 4012,目标 4012 + 20 = 4032;R3 = 25 不为 0,所以不跳转,也不写任何寄存器。
Identify the hazard type: (1) a load instruction and an instruction fetch both need the same memory in the same cycle; (2) an instruction uses a register value before a previous instruction writes it; (3) the processor does not yet know whether a branch will be taken.
判断冒险类型:(1) load 和取指在同一周期要用同一个存储器;(2) 一条指令在前一条写寄存器之前就读了它;(3) 处理器还不知道分支跳不跳。
(1) Structural hazard (2) Data hazard (RAW) (3) Control hazard
(1) 结构冒险 (2) 数据冒险(RAW) (3) 控制冒险
A pipelined processor has an ideal CPI of 1 and an average of 0.25 stall cycles per instruction. Compute (1) the pipeline CPI, (2) the speedup relative to an unpipelined processor with CPI = 5.
流水线理想 CPI 1,平均每条停顿 0.25 周期。求 (1) 流水线 CPI;(2) 相对 CPI = 5 的非流水处理器的加速比。
A processor uses a single-port memory. Can a structural hazard occur between LW R1,0(R2) and ADD R3,R4,R5 when LW is in the MEM stage and ADD is in the IF stage?
处理器使用单端口存储器。当 LW R1,0(R2) 处于 MEM 级、ADD R3,R4,R5 处于 IF 级时,会发生结构冒险吗?
Yes. LW needs a data read and ADD needs an instruction fetch in the same cycle, but there is only one memory port, so the fetch must be delayed by one cycle (a stall). Separate instruction/data memories or a dual-port memory remove the hazard.
会。LW 要读数据、ADD 要取指,两者在同一周期都要用唯一的存储器端口,只能把取指推迟一拍(插入停顿)。用分离的指令/数据存储器或双端口存储器可以消除。
25% of instructions are loads/stores and each causes one structural stall. Find (1) the average stall cycles per instruction, (2) the CPI.
25% 的指令是 load/store,每条造成 1 个结构停顿。求 (1) 平均每条停顿;(2) CPI。
Determine which instructions have RAW hazards: ADD R1,R2,R3; SUB R4,R1,R5; AND R6,R1,R7; OR R8,R9,R10.
判断哪些指令有 RAW 冒险:ADD R1,R2,R3;SUB R4,R1,R5;AND R6,R1,R7;OR R8,R9,R10。
SUB and AND: both read R1, which ADD writes, before ADD has written it back. OR does not use R1, so it has no hazard.
SUB 和 AND:都读 ADD 写的 R1,而且在 ADD 写回之前读。OR 不用 R1,没有冒险。
Assume the register write occurs in the first half of a cycle and the read in the second half. ADD writes R1 in CC5. Which instruction can safely read R1 in CC5?
假设寄存器前半周期写、后半周期读。ADD 在 CC5 写 R1。哪条指令可以在 CC5 安全地读 R1?
The instruction that is in ID during CC5, i.e. the third instruction after ADD (OR R8,R1,R9 in the slide example): ADD writes in the first half of the cycle and OR reads the new value in the second half.
在 CC5 处于 ID 级的那条,也就是 ADD 之后的第三条指令(讲义例子里的 OR R8,R1,R9):ADD 前半周期写入,它后半周期读出,拿到的是新值。
Classify each dependency: (1) ADD R1,R2,R3 / SUB R4,R1,R5; (2) ADD R1,R2,R3 / LW R2,0(R5); (3) LW R1,0(R2) / ADD R1,R3,R4.
分类:(1) ADD R1,R2,R3 / SUB R4,R1,R5;(2) ADD R1,R2,R3 / LW R2,0(R5);(3) LW R1,0(R2) / ADD R1,R3,R4。
(1) RAW (ADD writes R1, SUB reads it) (2) WAR (ADD reads R2, LW writes it) (3) WAW (both write R1)
(1) RAW(ADD 写 R1,SUB 读 R1) (2) WAR(ADD 读 R2,LW 写 R2) (3) WAW(两条都写 R1)
Which of RAW, WAR and WAW can occur in a basic DLX 5-stage pipeline? Explain briefly.
RAW、WAR、WAW 中哪些会出现在基本 DLX 五级流水线里?简要说明。
Only RAW. WAR cannot occur: all reads happen in ID (stage 2) and all writes in WB (stage 5), so an earlier instruction always reads first. WAW cannot occur: all instructions have the same length and write in WB, so writes complete in program order.
只有 RAW。WAR 不会:所有读都在 ID(第 2 级),所有写都在 WB(第 5 级),前一条一定先读完。WAW 不会:所有指令长度相同、都在 WB 写,写回顺序与程序顺序一致。
Determine whether forwarding eliminates the hazard: (1) ADD R1,R2,R3 / SUB R4,R1,R5; (2) SUB R1,R2,R3 / AND R4,R1,R5.
判断转发能否消除冒险:(1) ADD R1,R2,R3 / SUB R4,R1,R5;(2) SUB R1,R2,R3 / AND R4,R1,R5。
Yes for both. Each is an ALU result used by the next ALU instruction, forwarded from EX/MEM to the ALU input with zero stalls.
两个都能。都是 ALU 结果给紧跟的 ALU 指令用,从 EX/MEM 转发到 ALU 输入,零停顿。
ADD R1,R2,R3 followed by SUB R4,R1,R5 requires forwarding. Which pipeline register provides the forwarded value? A. IF/ID B. EX/MEM C. Register file
ADD R1,R2,R3 后紧跟 SUB R4,R1,R5,需要转发。转发值来自哪个流水线寄存器?A. IF/ID B. EX/MEM C. 寄存器堆
B. EX/MEM. When SUB is in EX, ADD is in MEM and its result sits in the EX/MEM pipeline register (A/M in the slides).
B. EX/MEM。SUB 处于 EX 时,ADD 正好在 MEM,结果存在 EX/MEM(讲义叫 A/M)流水线寄存器里。
Determine whether a stall is needed for LW R1,0(R2) followed by SUB R4,R1,R5, assuming forwarding is available.
有转发时,LW R1,0(R2) 后紧跟 SUB R4,R1,R5,需要停顿吗?
Yes, one cycle. The loaded data is only available at the end of MEM, but SUB needs it at the start of its EX in that same cycle, so even forwarding is too late; after a one-cycle stall it is forwarded from MEM/WB.
需要,1 拍。load 的数据在 MEM 结束才从存储器读出,紧跟的 SUB 在同一周期的 EX 开始就要用,转发也来不及;停 1 拍后从 MEM/WB 转发。
20% of instructions are loads, half of them are immediately followed by a dependent instruction, and each load-use hazard causes one stall. Find the CPI.
20% 的指令是 load,其中一半紧跟着依赖它的指令,每次 load-use 停 1 拍。求 CPI。
Branch frequency = 30%, branch penalty = 3 cycles. Find the CPI.
分支占 30%,分支代价 3 周期。求 CPI。
The enhanced DLX reduces the branch penalty from 3 cycles to 1 cycle. With a branch frequency of 30%, compute the new CPI.
改进 DLX 把分支代价从 3 周期降到 1 周期。分支占 30%,求新的 CPI。
Branch frequency = 30%, predict-not-taken accuracy = 47%. Compute the CPI (misprediction penalty = 1 cycle).
分支占 30%,预测不跳转的准确率 47%。求 CPI(猜错代价 1 周期)。
Code: ADD R10,R11,R12; BEQZ R1,Label; NOP; SUB R4,R5,R6. Which instruction could be moved into the delay slot? A. ADD R10,R11,R12 B. SUB R4,R5,R6
代码:ADD R10,R11,R12;BEQZ R1,Label;NOP;SUB R4,R5,R6。哪条可以挪进延迟槽?A. ADD R10,R11,R12 B. SUB R4,R5,R6
A (from before): ADD executes anyway and does not affect the branch condition R1, so it is correct whether or not the branch is taken. SUB belongs to the fall-through path; in the slot it would also run when the branch is taken, which is unsafe.
A(from before):ADD 本来就要执行,又与分支条件 R1 无关,放进槽里无论跳不跳都正确。SUB 只在不跳转时执行,挪进槽里会在跳转时也被执行,不安全。
Consider LW R1,0(R2); ADD R3,R1,R4; BEQZ R3,Label; SUB R5,R6,R7. Identify (1) structural hazards, (2) data hazards, (3) control hazards, (4) the number of stall cycles assuming forwarding and a 1-cycle branch penalty.
代码:LW R1,0(R2);ADD R3,R1,R4;BEQZ R3,Label;SUB R5,R6,R7。找出 (1) 结构冒险;(2) 数据冒险;(3) 控制冒险;(4) 有转发、分支代价 1 拍时的停顿周期数。
(1) None if instruction and data memories are separate; with a single-port memory, LW's MEM would clash with a later IF. (2) LW → ADD (R1, load-use, 1 stall); ADD → BEQZ (R3, resolved by forwarding in the slides' model). (3) SUB after BEQZ must wait for the branch outcome: 1-cycle penalty. (4) 2 stall cycles in total.
Note: if the branch really compares in ID (enhanced DLX), R3 is needed in ID but ADD produces it only at the end of EX, adding one more stall (3 in total). The slides do not discuss this; answer 2.
(1) 指令与数据存储器分离时没有;若是单端口存储器,LW 的 MEM 会和后面某条的 IF 冲突。(2) LW → ADD(R1,load-use,停 1 拍);ADD → BEQZ(R3,按讲义口径靠转发解决)。(3) BEQZ 之后的 SUB 要等分支结果,代价 1 拍。(4) 共 2 个停顿周期。
补充:如果分支真在 ID 级比较(改进 DLX),R3 必须在 ID 就拿到,而 ADD 的结果在 EX 结束才有,还要再停 1 拍,共 3 拍。讲义没有讨论这一点,按 2 拍答。
每点一次"换一题"就随机生成一道新题。先填答案再核对,核对后可以看完整推导。得分只记在这个浏览器里。