111 年 國立中正大學電機工程學系碩士班計算機工程組《計算機組織》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題20 分

Draw a single-cycle datapath for MIPS R-type instructions SW and BNE (including instruction decoder and control signals to multiplexors and state elements).

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題要求設計 MIPS 單週期資料路徑,涵蓋:

  • R-type:暫存器對暫存器運算,例如 add $rd,$rs,$rt
  • SW:將暫存器資料寫入記憶體
  • BNE:兩個暫存器不相等時分支

題目將 SW 與 BNE 和 R-type 並列;嚴格分類上,SW 是 I-type、BNE 也是 I-type,因此本題應理解為「設計可支援 R-type、SW、BNE 的單週期資料路徑」。

MIPS 指令欄位如下:

op=[31:26],rs=[25:21],rt=[20:16],rd=[15:11]\text{op} = [31:26],\quad rs=[25:21],\quad rt=[20:16],\quad rd=[15:11]

I-type 指令的立即數為:

imm=[15:0]\text{imm}=[15:0]

分支位址計算公式為:

BranchTarget=PC+4+(SignExt(imm)≪2)\text{BranchTarget} = PC+4+\left(\text{SignExt}(\text{imm})\ll 2\right)

每條指令在一個 clock cycle 內完成,因此不需要多週期控制器;控制訊號由 instruction decoder 根據 opcode 與 funct 欄位產生。


指令語意與資料流

1. R-type 指令

R-type 指令的格式為:

op = 000000 | rs | rt | rd | shamt | funct

典型運算:

R[rd]←R[rs] ALU R[rt]R[rd]\leftarrow R[rs]\ \text{ALU}\ R[rt]

資料流:

  1. 從 register file 讀取 rs 與 rt
  2. ALU 的兩個輸入分別選擇 R[rs]R[rs] 與 R[rt]R[rt]
  3. 根據 funct 決定 ALU 操作
  4. 將結果寫回 rd

2. SW

SW rt, imm(rs) 的語意為:

Memory[R[rs]+SignExt(imm)]←R[rt]\text{Memory}[R[rs]+\text{SignExt}(\text{imm})]\leftarrow R[rt]

資料流:

  1. register file 讀出 R[rs]R[rs],作為 base address
  2. 將 16 位元立即數符號延伸成 32 位元
  3. ALU 計算有效位址:
Address=R[rs]+SignExt(imm)\text{Address}=R[rs]+\text{SignExt}(\text{imm})
  1. 將 R[rt]R[rt] 的資料寫入 data memory
  2. 不寫回 register file

3. BNE

BNE rs, rt, imm 的語意為:

若 R[rs]≠R[rt],則 PC←PC+4+(SignExt(imm)≪2)\text{若 }R[rs]\neq R[rt]\text{,則 }PC\leftarrow PC+4+\left(\text{SignExt}(\text{imm})\ll2\right)

資料流:

  1. 讀出 R[rs]R[rs] 與 $R[rt]
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題25 分

Given a 32-bit machine with byte-addressable memories.
(a) (10%) Draw a four-way set-associative cache that has a total of 1024 lines with one word per line.
(b) (5%) Calculate the total amount of cache memory in bytes.
(c) (10%) Find the binary values of (1)tag, (2)index, and (3)byte-offset for byte address 1200 in the cache of (a).

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查快取記憶體的位址切割與組織方式。32 位元、byte-addressable 機器中:

  • 一個 word = 3232 bits = 44 bytes。
  • Cache line 每行存放 11 word,因此區塊大小為 44 bytes。
  • 四路組合(four-way set associative)表示每個 set 包含 44 條 cache line。
  • 位址切割公式為:
位址位元數=Tag bits+Index bits+Byte-offset bits\text{位址位元數} = \text{Tag bits} + \text{Index bits} + \text{Byte-offset bits}

其中:

  • Byte offset:指定一個 word 中的哪一個 byte。
  • Index:指定 cache 中的哪一個 set。
  • Tag:用來與該 set 中各 way 的 tag 比對。

解題方法

(a) 四路組合快取的結構

Cache 總共有 10241024 條 lines,每個 set 有 44 條 lines,因此 set 數量為:

Sets=10244=256=28\text{Sets} = \frac{1024}{4} = 256 = 2^8

所以需要 88 個 index bits。

每條 cache line 存放 11 word,而一個 word 為 44 bytes,因此需要:

log⁡24=2\log_2 4 = 2

個 byte-offset bits。

Tag 位元數為:

32−8−2=2232 - 8 - 2 = 22

因此 32 位元位址格式如下:

Tag (22 bits)∣Index (8 bits)∣Byte offset (2 bits)\boxed{ \text{Tag }(22\text{ bits}) \mid \text{Index }(8\text{ bits}) \mid \text{Byte offset }(2\text{ bits}) }

Cache 可表示如下:

                         32-bit byte address
              ┌────────────────┬──────────┬────────────┐
              │   Tag 22 bits  │ Index 8  │ Offset 2   │
              └────────────────┴──────────┴────────────┘
                                      │
                                      ▼
                              選擇 256 個 sets 之一

Set 0       ┌─────────────────────────────────────────┐
            │ Way 0 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 1 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 2 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 3 │ Valid │ Tag │ 1 word = 32 bits │
            └─────────────────────────────────────────┘

Set 1       ┌─────────────────────────────────────────┐
            │ Way 0 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 1 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 2 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 3 │ Valid │ Tag │ 1 word = 32 bits │
            └─────────────────────────────────────────┘

   ⋮

Set 255     ┌─────────────────────────────────────────┐
            │ Way 0 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 1 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 2 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 3 │ Valid │ Tag │ 1 word = 32 bits │
            └─────────────────────────────────────────┘
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題25 分

Assume a 5-stage pipelined processor similar to MIPS is going to execute the following code fragment.

Loop: LW R2, 0(R1)
ADDI R1, R1, 4
ADD R3, R3, R2
SUBI R4, R4, 1
BNEZ R4, Loop # if R4 != 0 go to Loop

The initial value of R4 is 10. Assume that all possible forwarding paths are present in the pipeline.
The cache is direct-mapped with a line size of 4 words. Initially the cache has all entries marked as invalid. The cache has a 1-cycle hit time and takes an additional 10 cycles on a cache miss. The branch prediction unit predicts each branch as the same as its last seen outcome. Each branch mis-prediction yields a 2-cycle penalty.

Note: answers without clear reasoning are unacceptable!

(a) (15%) What is the runtime in cycles to execute the above code?
(b) (5%) If the order of the ADDI and ADD instructions is switched in the code shown above, will the runtime change? If so, by how much?
(c) (5%) If the order of the ADD and SUBI instructions is switched in the code shown above, will the runtime change? If so, by how much?

登入後即可作答並保存紀錄。

這一題的完整詳解

參考書等級:Computer Organization & Design (5 版) – 中等難度,適合作為碩二考前快速複習。


(a) 執行總循環數

指令總數:5  instr×10=505\;\text{instr}\times 10=50
pipeline 起始填充:44 cycles

1. 資料冒險

  • LW → ADD 產生 load‑use hazard,需插入 1 cycle bubble。
    1010 次迭代 ⇒10\Rightarrow 10 cycles。

2. 快取未命中

  • 直映快取,行長 44 words → 每 44 個存取一次 miss。
    起始地址假設為 00,則第 1,5,91,5,9 次 LW miss。
    每次 miss 需額外 1010 cycles(加上 1‑cycle hit)。
    33 次 miss ⇒3×10=30\Rightarrow 3\times10=30 cycles。

3. 分支預測

  • 第一次分支未見歷史,預測為 not‑taken → 失誤 22 cycles。
  • 第 1010 次迭代不再跳回,預測仍為 taken → 再失誤 22 cycles。
    共 22 次失誤 ⇒4\Rightarrow 4 cycles。

4. 合計

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題30 分

Assume a fully-connected neural network containing 1 input layer, 3 hidden layers, and 1 output layer.

The input layer has 256 nodes, each of which represents an 8-bit pixel of a 16*16 grayscale image.
Each of the 3 hidden layers contains 256 neurons, which computes a weighted sum of all nodes in its precedent layer, adds a bias value, and performs ReLU activation (i.e. the result remains the same if it is positive; otherwise it becomes 0). In other words, the j-th neuron of the i-th layer: xi,jx_{i,j} compute max⁡(∑k=0255xi−1,k×wi,j,k+bi,j,0)\max\left(\sum_{k=0}^{255} x_{i-1,k} \times w_{i,j,k} + b_{i,j}, 0\right).
The output layer has 10 nodes, each of which represents a digit (i.e. 0, 1, 2, ..., 9). The j-th output node x4,jx_{4,j} computes ∑k=0255x3,k×w4,j,k+b4,j\sum_{k=0}^{255} x_{3,k} \times w_{4,j,k} + b_{4,j} without ReLU.

(a) (5%) What is the amount of storage (in bytes) that are needed if the weights and biases are both represented as IEEE 754 single-precision floating-point numbers?
(b) (10%) Due to cost issues, only a 1KByte SRAM is allowed to store the weights on chip (assume biases are handled independently), and the design team decides to implement a direct-mapped cache mechanism to simplify the management of on-chip (i.e. 1Kbyte SRAM) and off-chip (i.e. containing all weights) storages. Assume one cache block stores 32-byte data. What are the on-chip storage requirements in addition to the 1 Kbyte SRAM for weights?
(c) (5%) What is the miss rate of the weight cache in (b)?
(d) (10%) Describe how to improve the weight memory organization in (b) under the same cost constraint. Note: answers without clear reasoning are unacceptable!

登入後即可作答並保存紀錄。

這一題的完整詳解

(a) 存儲需求

  • 隱藏層 1‒3:每層 256 × 256 = 65 536 個權重
    → 3 × 65 536 = 196 608 個權重
  • 輸出層:256 × 10 = 2 560 個權重
  • 總權重數 = 196 608 + 2 560 = 199 168 個

每個權重、偏置皆為 IEEE 754 single‑precision (4 Byte)

  • 權重儲存量:199 168 × 4 = 796 672 Byte
  • 偏置數量:隱藏層 3 × 256 + 10 = 778 個
    → 778 × 4 = 3 112 Byte

總儲存量
796 672 Byte+3 112 Byte=799 784 Byte≈781.4 KiB796\,672\ \text{Byte} + 3\,112\ \text{Byte}=799\,784\ \text{Byte}\approx 781.4\ \text{KiB}

【答案】799 784 Byte (≈ 781 KiB)


(b) 1 KiB 權重 SRAM 之外的片上需求

  • SRAM 大小:1 KiB = 1 024 Byte
  • 快取區塊大小:32 Byte → 每塊 8 個權重

位址位元數:
⌈log⁡2(796 672)⌉=20 bits\lceil\log_2(796\,672)\rceil = 20\ \text{bits}

快取結構:

  • 內部資料行數 = 1 024 / 32 = 32 行
  • 內部索引位元 = log⁡232=5\log_2 32 = 5 bits
  • 區塊位元 = log⁡232=5\log_2 32 = 5 bits

Tag 位元 = 20 − 5 − 5 = 10 bits
每行需儲存 Tag (10 b) + Valid (1 b) = 11 bits

全部 32 行的額外儲存:
32×11 bits=352 bits=44 Byte32 \times 11\ \text{bits}=352\ \text{bits}=44\ \text{Byte}

(若以最小位元組為單位,可視為 48 Byte。)

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題