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

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

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

逐題詳解

17%

1. [7%] Determine whether each of the following statements is true (T) or false (F).

(1) [1%] Program execution time reduces when the clock rate increases.

(2) [1%] Program execution time reduces when the CPI increases.

(3) [1%] Program execution time reduces when the instruction count (IC) increases.

(4) [2%] Suppose the floating point instructions are enhanced and can run 10 times faster. If the execution time before the floating point enhancement is 80 seconds and three-fourth of the execution time is spent executing floating-point instructions, the overall speed up is at least 3.

(5) [2%] Suppose the floating point instructions are enhanced and can run 20 times faster. If the execution time before the floating point enhancement is 80 seconds and one-half of the execution time is spent executing floating-point instruction, the overall speed up is at least 2.

\(\text{Time}=\dfrac{IC\times CPI}{\text{clock rate}}\)

(1) clock rate 變大,時間變小 → T

(2) CPI 變大,時間變大 → F

(3) IC 變大,時間變大 → F

(4) 浮點部分 \(80\times\tfrac34=60\) s,其餘 20 s;加速後 \(60/10+20=26\) s,speedup \(=80/26\approx3.08\ge3\) → T

(5) 浮點部分 40 s,其餘 40 s;加速後 \(40/20+40=42\) s,speedup \(=80/42\approx1.90\lt2\) → F

(5) 就算浮點部分時間降到 0,最多也只能 \(80/40=2\)(Amdahl 定律),實際一定小於 2

(1) T (2) F (3) F (4) T (5) F

27%

2. [7%] Determine whether each of the following statements is true (T) or false (F).

(1) [1%] R-type and I-type MIPS instruction can be distinguished by the opcode of an instruction.

(2) [1%] Base addressing mode is used by I-format instructions.

(3) [1%] PC-relative addressing is used by J-format.

(4) [2%] Suppose the program counter (PC) is at address 0x0000 0000. It is possible to use one single branch-on-equal (beq) MIPS instruction to get to address 0x00030000.

(5) [2%] Suppose the program counter (PC) is at address 0x0000 0000, it is possible to use the jump MIPS instruction to get to 0xFFFFFFB0.

(1) R-type 的 opcode 是 0,I-type 是別的值(J-type 是 2、3) → T

(2) I-format 的 lw/sw 用「base register+16-bit offset」 → T

(3) J-format 是 pseudo-direct addressing(26-bit 位址+PC 的高 4 bit);PC-relative 是 branch(I-format) → F

(4) beq 的 offset 是 16-bit 有號數、單位是字:target \(=PC+4+4\times\text{offset}\),範圍 \(\pm2^{15}\) 字 \(=\pm128\) KB(\(\pm0x20000\))

  \(0x30000-4=0x2FFFC\) → offset \(=0xBFFF=49151\gt32767\),超出範圍 → F

(5) J 指令的 target \(=\{PC+4[31{:}28],\ 26\text{-bit}\times4\}\),PC+4 \(=0x4\) 的高 4 bit 是 0,只能到 0x0xxxxxxx;0xFFFFFFB0 高 4 bit 是 F → F

(1) T (2) T (3) F (4) F (5) F

36%

3. [6%] The following descriptions are about IEEE 754 single precision float point format. Determine whether each of the following statements is true (T) or false (F)?

(1) [1%] The float point format has 1 sign bit, 8 exponent bits, and 23 fraction bits.

(2) [1%] The smallest positive number it can represent is 0000 0001 0000 0000 0000 0000 0000 00002.

(3) [1%] The result of "Divide 0 by 0" is 0111 1111 1000 0000 0000 0000 0000 00002.

(4) [1%] To improve the accuracy of the results, IEEE 754 has one extra bit for rounding.

(5) [2%] \(0.75_{10}\) is represented by 1011 1111 0100 0000 0000 0000 0000 00002.

(1) \(1+8+23=32\) → T

(2) 0000 0001 0000…0 拆成 sign \(=0\)、exponent \(=0000\,0010_2=2\)、fraction \(=0\),值是 \(2^{2-127}=2^{-125}\);最小的正 normalized 數是 exponent \(=1\) 的 \(2^{-126}\),加上 denormalized 數還能小到 \(2^{-149}\) → F

(3) 0 11111111 000…0 是 \(+\infty\);\(0/0\) 是 NaN(exponent 全 1、fraction 不為 0,例 0x7FC00000) → F

(4) IEEE 754 為了 rounding 多保留 guard、round,再加 sticky(共 3 個額外 bit,不是 1 個) → F

(5) \(0.75=0.11_2=1.1_2\times2^{-1}\):exponent \(=127-1=126=0111\,1110\),fraction \(=1000\ldots\),sign \(=0\) → 0011 1111 0100 0…0;題目 sign 是 1,那是 \(-0.75\) → F

(1) T (2) F (3) F (4) F (5) F

410%

4. [10%] Refer to the above table and Figure 1 [題目第 1 頁的 MIPS 編碼表]. The right side of the above table is the assembled instructions of the MIPS instructions in the left side. The starting address of the loop is \(40000_{10}\) in memory. What are the value of (1), (2), (3) and (4)? Express your answers in decimal numbers.

MIPS instructionsAddressCorresponding Assembled Instruction
Loop: sll $t1, $s3, 2400000019940
(1) add $t1, $t1, $s640004092290(4)
lw $t0, 0($t1)40008(3)980
bne $t0, $s5, Exit400125821(1)
addi $s3, $s3, 140016819191
j Loop400202(2)
Exit:40024…..

(1) bne 是 I-format:op rs rt offset,offset 是「從 PC+4 起算的字數」

  bne 在 40012,PC+4 \(=40016\);Exit 在 40024

  offset \(=\dfrac{40024-40016}{4}=2\)

(2) j 是 J-format,26-bit 位址欄放「目標位址 ÷ 4」:Loop \(=40000\) → \(40000/4=10000\)

(3) lw 的 opcode:表中 op[31:29]=4(100)、op[28:26]=3(011) → \(100\,011_2=35\)

(4) add 是 R-format,funct:表中 funct[5:3]=4(100)、funct[2:0]=0(000) → \(100\,000_2=32\)

sll 那列 shamt 欄印的是 4,實際 sll $t1,$s3,2 的 shamt 是 2;題目不問這格

(1) 2 (2) 10000 (3) 35 (4) 32

515%

5. [15%] Refer the following instruction sequence:

lw $1,40($2)
add $2,$3,$3
add $1,$1,$2
sw $1,20($2)

(1) [5%] Find all data dependences in this instruction sequence.

(2) [5%] Find all hazards in this instruction sequence for a 5-stage pipeline with and without forwarding.

(3) [5%] To reduce the clock cycle time, we are considering a split of the MEM stage into two stages. Repeat (2) for this 6-stage pipeline.

I1:lw $1,40($2) I2:add $2,$3,$3 I3:add $1,$1,$2 I4:sw $1,20($2)

(1) RAW(真依賴):

  I1→I3(\$1:I1 寫、I3 讀) I2→I3(\$2) I2→I4(\$2,sw 的 base) I3→I4(\$1,sw 要存的值)

  WAR(反依賴):I1→I2(\$2:I1 讀 \$2 當 base、I2 寫 \$2)

  WAW(輸出依賴):I1→I3(\$1:兩條都寫)

  WAR、WAW 在 5 段 in-order pipeline 不會變成 hazard(寫回的順序與指令順序相同、讀在 ID 早於後面指令的寫回)

(2) 5-stage(register file:前半 cycle 寫、後半 cycle 讀,所以結果寫回的那個 cycle,後面指令的 ID 就讀得到)

  沒有 forwarding:RAW 的兩條指令中間隔太近就會 hazard(producer 的 WB 在 cycle 5,consumer 的 ID 最早在同一個 cycle):

   I1→I3(隔 1 條)、I2→I3(相鄰)、I2→I4(隔 1 條)、I3→I4(相鄰),四個都是 data hazard,要 stall

   I3 要等 I2 的 \$2 寫回(cycle 6)才能 ID → stall 2;I4 要等 I3 的 \$1 寫回(cycle 9)才能 ID → 再 stall 2。共 4 個 stall,12 cycles 做完:

指令123456789101112
lw $1,40($2)IFIDEXMEMWB
add $2,$3,$3IFIDEXMEMWB
add $1,$1,$2IFstallstallIDEXMEMWB
sw $1,20($2)IFstallstallstallstallIDEXMEMWB

  有 forwarding:I1→I3:\(\mathrm{lw}\) 的資料在 MEM 末出來,I3 在 cycle 5 進 EX,從 MEM/WB 轉給 EX,夠用

   I2→I3、I3→I4:EX/MEM → EX(或 MEM);I2→I4:MEM/WB → EX。lw 後面第 2 條才用它,不是 load-use,全部由 forwarding 解決,0 個 stall,8 cycles:

指令12345678
lw $1,40($2)IFIDEXMEMWB
add $2,$3,$3IFIDEXMEMWB
add $1,$1,$2IFIDEXMEMWB
sw $1,20($2)IFIDEXMEMWB

(3) 6-stage(IF、ID、EX、MEM1、MEM2、WB):WB 變在第 6 stage,結果更晚寫回

  沒有 forwarding:producer 的 WB 在 cycle \(k+5\),consumer 的 ID 要 ≥ 那個 cycle,相隔 1、2、3 條都有 hazard(本序列的四個 RAW 都在這個範圍)

   I3 等 I2 的 \$2(I2 WB 在 cycle 7)→ ID 在 cycle 7,stall 3;I4 等 I3 的 \$1(I3 WB 在 cycle 11)→ ID 在 cycle 11,再 stall 3。共 6 個 stall

指令123456789101112131415
lw $1,40($2)IFIDEXM1M2WB
add $2,$3,$3IFIDEXM1M2WB
add $1,$1,$2IFstallstallstallIDEXM1M2WB
sw $1,20($2)IFstallstallstallstallstallstallIDEXM1M2WB

  有 forwarding:資料要等 lw 的 MEM2 結束(cycle 5 末)才有,I3 原本 cycle 5 要用 \$1,還差一個 cycle → I1→I3 變成 load-use,1 個 stall(I3 的 EX 從 cycle 5 延到 6,I4 跟著延 1 個 cycle);其餘 RAW 都由 EX 之後的 forwarding 解決

指令12345678910
lw $1,40($2)IFIDEXM1M2WB
add $2,$3,$3IFIDEXM1M2WB
add $1,$1,$2IFIDstallEXM1M2WB
sw $1,20($2)IFstallIDEXM1M2WB

(1) RAW:I1→I3、I2→I3、I2→I4、I3→I4;WAR:I1→I2;WAW:I1→I3 (2) 無 forwarding 4 stall、有 forwarding 0 stall (3) 無 forwarding 6 stall、有 forwarding 1 stall

65%

6. [5%] Suppose that in 1000 memory references there are 50 misses in the first-level cache, 20 misses in the second-level cache, and 5 misses in the third-level cache. Assume the miss penalty from the L3 cache to memory is 100 clock cycle, the hit time of the L3 cache is 10 clocks, the hit time of the L2 cache is 4 clocks, the hit time of L1 is 1 clock cycle. What is the average memory access time? Ignore the impact of writes.

每 1000 次 reference:所有 reference 都要花 L1 hit time 1;L1 miss 的 50 次再花 L2 的 4;L2 miss 的 20 次再花 L3 的 10;L3 miss 的 5 次再花 100

總 cycles \(=1000\times1+50\times4+20\times10+5\times100=1000+200+200+500=1900\)

AMAT \(=1900/1000=1.9\) clock cycles

用 miss rate 檢查:\(1+0.05\times\big(4+0.4\times(10+0.25\times100)\big)=1+0.05\times18=1.9\)(L2、L3 的 local miss rate 是 \(20/50=0.4\)、\(5/20=0.25\))

AMAT \(=1.9\) clock cycles

715%

7. [15%] The C-like pseudocode of two programs: programP and programA is shown below.

programP
main() {
    int x;
    x = fork();
    if (x > 0) { fork(); printf("1"); }
    else if (x == 0) {
        printf("2");
        fork();
        exec("programA");
        printf("3");
    }
    while (1);
}

programA
main() {
    printf("A");
    while (1);
}

(1) [5%] Please draw the parent-child process relationship graph resulting from the execution of the programP. Explain your answer.

(2) [10%] What is the output of the program? Explain your answer.

(1) 設 programP 的原 process 為 P

  • 第一個 fork():P 產生 C。P 得到 \(x\gt0\),C 得到 \(x=0\)

  • P(\(x\gt0\))再 fork():產生 C′(C′ 從這個 fork 之後繼續,也在 if 內,印 1)

  • C(\(x=0\))印 2,fork() 產生 G;C 與 G 都 exec("programA"),換成 programA 的程式碼,永遠不會回來執行 printf("3")

        P
       / \
      C   C'
      |
      G

  共 4 個 process:P、C、C′、G(P 的兩個子 C、C′;C 的子 G)

(2) P 印 1;C′ 印 1;C 印 2;C 與 G 在 exec 之後各印一個 A。printf("3") 在 exec 之後不會執行

  輸出共 5 個字元:兩個 1、一個 2、兩個 A。順序由排程決定,限制只有:2 一定在兩個 A 之前(C 先印 2 才 fork、exec,G 才會印 A)。兩個 1 與 2、A 之間沒有先後限制

  可能的輸出,例:11 2 AA、2 A A 11、1 2 1 AA、2 1 A 1 A … 共 10 種

程式 exec 與 printf 之間若 stdout 有 buffer,沒有 \n 的字元不一定真的送出;這裡照題意當作每個 printf 立即輸出

(1) P → C、C′;C → G(共 4 個 process) (2) 字元 1、1、2、A、A,2 在兩個 A 之前,共 10 種排列

815%

8. [15%] Assume that a process currently runs in the user mode of an x86 system. Which of the following situation(s) always cause a transition from the user mode to the kernel mode? (a) a timer interrupt occurs and the time quantum of the process has not expired, (b) a timer interrupt occurs and the time quantum of the process has expired, (c) TLB hits, (d) TLB misses, (e) the process executes a memory access instruction. Please briefly explain your answer.

(a) 會。interrupt 發生,CPU 一定跳到 kernel 的 interrupt handler(kernel mode),handler 發現 quantum 還沒用完就回到原來的 process,但「進 kernel」這件事一定發生

(b) 會。同樣先進 kernel handler,之後排程器做 context switch

(c) 不會。TLB hit 由硬體直接完成位址轉換,沒有軟體介入

(d) 不一定。x86 的 TLB miss 由硬體 page-table walker 自己走 page table,找到有效的 entry 就補進 TLB,不需要進 kernel;只有 page table 裡 entry 無效才會 page fault 進 kernel

(e) 不一定。一般的 load/store 只要位址轉換成功(TLB hit,或 miss 但 page table 有效)就不進 kernel;只有 page fault 或違反保護才會

(a)、(b)

910%

9. [10%] For a memory access in a virtual memory system,

(1) [5%] is it possible that TLB hits and page fault occurs? Explain your answer.

(2) [5%] is it possible that TLB misses and page fault does not occur? Explain your answer.

(1) 若指「page 不在記憶體」的 page fault:不可能。TLB entry 是有效 page table entry 的快取,OS 把 page 換出時會把對應的 TLB entry 失效,所以 TLB hit 代表 page 在記憶體

  但存取權限不符(例如對 TLB 裡唯讀的 page 做 write、copy-on-write 的 page)時,TLB hit 也會發出 page fault(protection fault) → 廣義上可能

(2) 可能。TLB miss 只是 entry 沒在 TLB 裡:page table 裡該 page 有效且在記憶體,就由硬體/OS 走 page table 把 entry 載入 TLB,再重試存取,沒有 page fault

  (TLB miss 的機率遠比 page fault 高,所以大多數 TLB miss 都沒有 page fault)

(1) page 不在記憶體的 fault 不可能(只有 protection fault 可能) (2) 可能

1010%

10. [10%] Please explain why processor affinity is considered when an operating system schedules processes in a multi-processor system.

process 跑在某個處理器時,它的資料與指令會留在那顆處理器的 cache(以及 TLB、branch predictor)裡,cache 越熱 hit rate 越高

若把 process 換到別顆處理器:原處理器的 cache 內容用不到(還要 invalidate 或搬資料)、新處理器的 cache 是冷的,要重新填,產生大量 cache miss,效能降低

NUMA 系統中,process 的記憶體配置在原處理器的 local memory;搬到別顆處理器後要存取 remote memory,延遲更長

所以排程器盡量讓 process 留在同一顆處理器,這就是 processor affinity:soft affinity(盡量留,但可以為了 load balancing 搬)與 hard affinity(強制只能跑指定的處理器)

代價:affinity 與 load balancing 衝突,過度堅持會讓某些處理器忙、其他閒置

保留 cache(與 TLB)內容、避免 cache miss 與 NUMA 的 remote access;與 load balancing 取捨

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com