免費詳解 / 成大 / 硬體 / 105

105 成大資工所考古題解答|硬體逐題詳解

105 成大資工所硬體考古題的逐題詳解,共 8 題,免費公開。

逐題詳解

120%

1. [20%] Consider a MIPS processor with an additional floating point unit. Assume functional unit delays in the processor are as follows: memory (2 ns), ALU and adders (2 ns), register file access (1 ns), FPU add (8 ns), FPU multiply (16 ns), and the remaining units (0 ns). Also assume instruction mix as follows: loads (31%), stores (21%), R-format instructions (27%), branches (5%), jumps (2%), FP adds and subtracts (7%), and FP multiplies and divides (7%).

(a) What is the delay in nanosecond to execute a load, store, R-format, branch, jump, FP add/subtract, and FP multiply/divide instruction in a single MIPS design?

(b) What is the averaged delay in nanosecond to execute a Load, store, R-format, branch, jump, FP add/subtract, and FP multiply/divide instruction in a multicycle MIPS design?

(a) 先算每一種指令自己的路徑延遲(只加有經過的元件):

指令取指令 memreg 讀ALUdata memreg 寫FPU合計 (ns)
load21221—8
store2122——7
R-format212—1—6
branch212———5
jump2—————2
FP add/sub21——1812
FP mul/div21——11620

  single-cycle 的 clock 要容納最慢的指令(FP mul/div,20 ns),每條指令都占一個 cycle → 每一條指令都是 20 ns(load 8、store 7、R-format 6、branch 5、jump 2、FP add/sub 12 的路徑都會被拉長到 20)

(b) multicycle:每個 cycle 只做一件事(取指令、讀 reg、ALU 或 FPU、存取資料記憶體、寫 reg),clock 要容納最慢的單一步驟 = FPU multiply 的 16 ns

  各指令用幾個 cycle(IF、ID、EX、MEM、WB;branch 在 EX 完成,jump 在第 3 cycle 完成,FP 指令沒有 MEM):

指令cycles比例
load50.31
store40.21
R-format40.27
branch30.05
jump30.02
FP add/sub40.07
FP mul/div40.07

  平均 CPI \(=0.31\times5+0.21\times4+0.27\times4+0.05\times3+0.02\times3+0.07\times4+0.07\times4=1.55+0.84+1.08+0.15+0.06+0.28+0.28=4.24\)

  平均延遲 \(=4.24\times16=67.84\) ns

multicycle 的 67.84 ns 比 single-cycle 的 20 ns 還慢:16 ns 的 FPU 步驟把每一個 cycle 都拉長到 16 ns,其他步驟只要 1~2 ns。若把 FPU 做成多個 2 ns 的 cycle(clock 2 ns),則 FP add 4、FP mul 8 個 EX cycle,平均約 9.88 ns

(a) 單週期:每條都是 20 ns(各路徑 8、7、6、5、2、12、20) (b) 多週期:CPI 4.24 × 16 ns \(=67.84\) ns

215%

2. [15%] Consider a pipelined processor that executes the MIPS code shown in Figure 1 using the logic of hazard detection and data forwarding unit shown in Figure 2. If the MIPS code cannot be executed correctly, then how do we revise the logic shown in Figure 2 such that the code can be correctly executed?

add $1, $1, $5
add $1, $1, $6
add $1, $1, $7
Figure 1: The MIPS code
if (MEM/WB.RegWrite
    and (MEM/WB.RegisterRd != 0)
    and (MEM/WB.RegisterRd = ID/EX.RegisterRs))
then ForwardA = 01
if (MEM/WB.RegWrite
    and (MEM/WB.RegisterRd != 0)
    and (MEM/WB.RegisterRd = ID/EX.RegisterRt))
then ForwardB = 01
Figure 2: The logic of hazard detection and data forwarding unit

三條指令都是 $1 當 Rs 又當 Rd,每條都要用前一條的結果:

指令1234567
add $1,$1,$5IFIDEXMEMWB
add $1,$1,$6IFIDEXMEMWB
add $1,$1,$7IFIDEXMEMWB

會出錯。圖 2 只有 MEM hazard(MEM/WB → EX,ForwardA/B=01),缺兩件事:

① 沒有 EX hazard:第 2 條在 EX 時,第 1 條在 MEM,結果在 EX/MEM;圖 2 只看 MEM/WB,EX/MEM 的結果沒轉過來,第 2 條用到舊的 $1

② MEM hazard 沒有排除 EX hazard:第 3 條在 EX 時,第 2 條在 MEM(EX/MEM,\(\$1\) 最新)、第 1 條在 WB(MEM/WB,\(\$1\) 已過時)。兩個都符合,圖 2 會選 MEM/WB,轉出第 1 條的舊結果,而不是第 2 條的新結果

修正:加入 EX hazard 並讓它優先(ForwardA=10 表示取自 EX/MEM):

/* EX hazard */
if (EX/MEM.RegWrite
    and (EX/MEM.RegisterRd != 0)
    and (EX/MEM.RegisterRd = ID/EX.RegisterRs))
then ForwardA = 10
if (EX/MEM.RegWrite and (EX/MEM.RegisterRd != 0)
    and (EX/MEM.RegisterRd = ID/EX.RegisterRt))
then ForwardB = 10

/* MEM hazard:EX hazard 沒成立時才用 */
if (MEM/WB.RegWrite and (MEM/WB.RegisterRd != 0)
    and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd != 0)
             and (EX/MEM.RegisterRd = ID/EX.RegisterRs))
    and (MEM/WB.RegisterRd = ID/EX.RegisterRs))
then ForwardA = 01
if (MEM/WB.RegWrite and (MEM/WB.RegisterRd != 0)
    and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd != 0)
             and (EX/MEM.RegisterRd = ID/EX.RegisterRt))
    and (MEM/WB.RegisterRd = ID/EX.RegisterRt))
then ForwardB = 01

  這樣第 2 條從 EX/MEM 取第 1 條的結果、第 3 條從 EX/MEM 取第 2 條的結果(第 1 條的舊值不會被選到),三條都正確

會出錯;要加 EX hazard(EX/MEM → EX,10),並讓 MEM hazard 在 EX hazard 成立時不選

310%

3. [10%] What is the biased single precision IEEE 754 floating point format of 0.9375? What is the purpose to bias the exponent of the floating point numbers?

\(0.9375=\tfrac{15}{16}=0.1111_2=1.111_2\times2^{-1}\)

sign \(=0\);exponent \(=-1+127=126=0111\,1110_2\);fraction \(=111\,0000\cdots0\)

0 | 01111110 | 11100000000000000000000 = 0011 1111 0111 0000 0000 0000 0000 0000 = 0x3F700000

bias 的目的:exponent 要能表示負的次方。用 bias(加 127)把範圍 \(-126..127\) 存成不帶符號的 \(1..254\),

 • 不需要另外的 exponent sign bit,也不用 two's complement

 • 同符號的浮點數,exponent 欄位在最高位之後、fraction 之前,整個 bit pattern 當成無號整數比較大小就等於比較浮點數大小(先比 exponent 再比 fraction),可以直接用整數比較器做 floating-point 比較與排序

0x3F700000(0 01111110 111 0…0);bias 讓 exponent 變成無號數,浮點數可直接用整數比較

45%

4. [5%] Which of the following techniques can resolve control hazards? (a) Branch prediction (b) Stall (c) Delayed branch

(a) branch prediction:猜 taken/not taken 先取指令,猜對沒損失、猜錯再清掉 → 能

(b) stall:branch 結果出來以前先不取下一條 → 能(最簡單,但每次 branch 都慢)

(c) delayed branch:branch 之後的 delay slot 放一條無論如何都要做的指令,把 stall 的 cycle 填滿 → 能

(a)、(b)、(c) 都可以

515%

5. [15%] In a demand-paging system using a system-wide inverted page table,

(a) [10%] why are per-process page tables still required?

(b) [5%] when is a per-process page table accessed?

inverted page table 整個系統一張,每個 physical frame 一筆(記 \(\langle\)pid, page number\(\rangle\)),只記得「現在在記憶體裡」的 page

(a) demand paging 中,process 的大部分 page 不在記憶體(在 backing store)。inverted page table 沒有這些 page 的資訊,發生 page fault 時 OS 不知道 page 在 disk 的哪裡。所以每個 process 還要一張 per-process(external)page table,記每個 page 在 backing store 的位置(和狀態)

  (另外 inverted page table 查 page 要 hash 搜尋,且不方便做 shared memory;external page table 可被 page 出去,不佔常駐記憶體)

(b) 只有在 page fault 時才會用到:查 inverted page table 找不到(miss)→ page fault → OS 查這個 process 的 per-process page table,找到 page 在 disk 的位置,載入記憶體、挑 frame 更新 inverted page table。一般位址轉換(page 在記憶體)靠 TLB 與 inverted page table 完成,不碰 per-process page table

(a) inverted page table 只記在記憶體的 page,page 在 disk 的位置要靠 per-process page table (b) 只在 page fault 時

615%

6. [15%] Which of the following may reduce TLB misses? Explain your answers briefly.

(a) [5%] increase the level of paging (b) [5%] use pre-paging (c) [5%] decrease the page size

TLB 快取的是「virtual page → frame」的轉換;TLB miss 的次數取決於:TLB 能涵蓋多少記憶體(TLB reach = entry 數 × page size)與程式的 locality

(a) 不能。增加 paging 層數只讓 TLB miss 時要多走幾層 page table(miss penalty 變大),TLB 存的是最後的轉換結果,hit/miss 次數不變

(b) 一般不能。pre-paging 是把 page 先載入記憶體,減少的是 page fault;TLB 不會因此預先填好,第一次存取那個 page 仍是 TLB miss(只是不再 page fault)。只有實作上預載 page 時順便把 TLB entry 也填好,才可能少幾次 miss

(c) 不能,反而更糟。page 變小,同樣的 working set 需要更多 page、更多 TLB entry,TLB reach 縮小,TLB miss 增加

真正能減少 TLB miss 的是:增大 page size(或 huge page)、增加 TLB entry 數、改善 locality

(a)、(c) 不能((c) 反而增加);(b) 一般不能,只有連 TLB 一起預載才可能

710%

7. [10%] In Linux, a process cannot hold a spinlock while attempting to acquire a semaphore. Please explain why this policy is in place.

spinlock 是 busy-waiting:拿不到鎖的 CPU 原地空轉,所以只能用在極短的 critical section,而且持有期間 kernel 關掉 preemption,process 不能睡覺

semaphore 的 down/acquire 拿不到時會讓 process sleep(放進等待佇列、讓出 CPU)

若持有 spinlock 又去等 semaphore:

① process 帶著 spinlock 睡著,其他 CPU 上要拿這把 spinlock 的 process 全部空轉,直到睡著的 process 被喚醒、放掉鎖——可能很久,浪費 CPU

② 單處理器:睡著後排程器換另一個 process(或 interrupt handler)跑,它要拿同一把 spinlock,拿不到就空轉;原持有者要得到 CPU 才能釋放,但 CPU 被空轉者占住 → deadlock

③ 睡眠要呼叫 scheduler,而持有 spinlock 時 preemption 被關閉、處在 atomic context,睡覺是不允許的("scheduling while atomic")

semaphore 會讓 process sleep,持有 spinlock 時 sleep 會造成其他等待者空轉甚至 deadlock

810%

8. [10%] Please explain why UNIX inodes support large files while allowing fast accesses to small files.

inode 裡有固定數量的指標(典型 15 個):12 個 direct block 指標,加 1 個 single indirect、1 個 double indirect、1 個 triple indirect

小檔案快:不超過 12 個 block(4 KB block 時 48 KB)的檔案,block 位址都直接放在 inode 裡。inode 在 open 時就載入記憶體,要讀任何 block 不用再讀索引,只要 1 次 disk access。大部分檔案都很小,所以大多數存取是最快的

大檔案也行:較大的 offset 才多走一層索引:single indirect 指向一個放 \(N\) 個位址的 block(\(N=1024\) 時多 1024 個 block)、double indirect \(N^2\)、triple indirect \(N^3\)。4 KB block、4-byte 位址時最大約 \(12+N+N^2+N^3\) 個 block,約 4 TB

代價只在離檔頭越遠、多讀 1~3 個索引 block(常被 cache);小檔案不必浪費空間存很大的索引,也不用為大檔案預留

direct 指標讓小檔案 1 次 access,多層 indirect 指標(single/double/triple)讓檔案可以很大

109–115 的詳解也是這樣寫的。

國立成功大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 289 頁,逐題推導。

購買 · NT$ 850 看成大其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com