109 年 國立臺灣聯合大學系統(清華、政治、陽明交通、中央四校聯招)研究所電機類《計算機系統(計算機組織)》
第 1 題9 分
Consider two different implementations of the same instruction set architecture. The instructions can be divided into four classes according to their CPI (class A, B, C, and D). P1 with a clock rate of 2.5GHz and CPIs of 1, 2, 3, and 3, and P2 with a clock rate of 3 GHz and CPIs of 2, 2, 2, and 2.
(a) (3%) Given a program with a dynamic instruction count of 1.0E6 instructions divided into classes as follows: 10% class A, 20% class B, 50% class C, and 20% class D. What is the ratio of CPI for P1/P2?
(b) (3%) The result of the benchmark running on the machine has an instruction count of 2.5E12, and execution time of 900 s, and a reference time of 9000 s. Find the CPI if the clock cycle time is 0.25 ns.
(c) (3%) Find the increase in CPU time if the number of instructions of the benchmark is increased by 20% and the CPI is increased by 5%.
登入後即可作答並保存紀錄。
核心觀念
本題考查計算機效能評估的核心觀念——CPU 效能方程式(CPU Performance Equation),以及指令混合比例(Instruction Mix)對加權平均每指令週期數(Average Cycles Per Instruction, CPI)的計算影響。
主要使用的公式如下:
- 加權平均 CPI(Average CPI):
當程式包含多種不同類型的指令時,整體平均 CPI 為各類別指令出現比例(Instruction Proportion, )與其對應 CPI()的加權總和:
- CPU 執行時間方程式(CPU Time Equation):
其中 為動態指令數(Dynamic Instruction Count), 為平均每指令週期數, 為時脈週期時間(Clock Cycle Time), 為時脈頻率(Clock Rate)。 - 相對變化率推導:
若指令數變更為 ,CPI 變更為 ,且時脈週期時間 保持不變,則新 CPU 執行時間為:
解題方法
- (a) 小題:利用動態指令混合比例分別計算處理器 P1 與 P2 的加權平均 CPI,再計算兩者之比值 。
- (b) 小題:利用已知條件(指令數 、執行時間 、時脈週期時間 )帶入 CPU 效能方程式逆推 CPI。題目給定的參考時間(Reference Time)為計算 SPECratio 所用,與求算 CPI 無關。
- (c) 小題:利用積的相對變化計算新舊 CPU 執行時間的比值關係,導出 CPU 執行時間的增加百分比與絕對時間增加量。
各小題詳細解析
(a) 小題解析
-
給定條件:
- 指令混合比例:Class A 為 、Class B 為 、Class C 為 、Class D 為 。
- P1 的各類指令 CPI 分別為:, , , 。
- P2 的各類指令 CPI 分別為:, , , 。
-
推導與計算:
-
計算 P1 的平均 CPI ():
-
計算 P2 的平均 CPI ():
-
計算 P1 與 P2 的 CPI 比值 ():
-
(b) 小題解析
- 給定條件:
- 動態指令數
- 執行時間
第 2 題12 分
For the following calculation:
(a) (4%) Assume 152 and 213 are signed 8-bit decimal integers store in two's complement format. Calculate 151+214. The result should be written in decimal.
(b) (4%) Assume 152 and 213 are unsigned 8-bit integers. Calculate 151+214 using saturating arithmetic. The result should be written in decimal.
(c) (4%) Given by a floating-point number 427D0000(hex) that is represented by IEEE 754 standard. What decimal number does it?
登入後即可作答並保存紀錄。
核心觀念
本題涵蓋計算機系統底層數值表示法與運算機制的三大重點:
- 8 位元二補數(Two's Complement)表示法與溢位處理:
- 在 8 位元二補數格式中,最高位(MSB)為符號位( 代表正數或零, 代表負數),數值表示範圍為 。
- 若以無號十進位數值 表示一個 8 位元二補數的暫存器內容,其對應的實際帶號值為 。
- 典型硬體加法器採用模數加法(Modulo Arithmetic),運算產生之溢出進位(Carry out)在固定寬度下會被截斷(Truncated)。
- 飽和運算(Saturating Arithmetic):
- 常見於多媒體與數位訊號處理(DSP / SIMD 指令集,如 MMX、SSE、NEON)。
- 運算結果若超出該資料型態所能表示的最大值或最小值時,不會發生繞回(Wrap-around),而是直接截斷並「飽和」至可表示的極端值。
- 8 位元無號整數(Unsigned 8-bit Integer)之表示範圍為 ,飽和上限為 。
- IEEE 754 單精準度(Single-Precision)浮點數標準:
- 總長度為 32 位元,欄位分配為:
- 符號位元(Sign, ):( 為正, 為負)。
- 指數欄位(Exponent, ):,偏差值(Bias)為 。實際指數為 。
- 小數欄位(Fraction / Mantissa, ):,正規化(Normalized)數值的有效數(Significand)為 。
- 正規化浮點數之真值公式為:
- 總長度為 32 位元,欄位分配為:
解題方法與推導
(a) 8-bit 二補數帶號整數加法
- 解讀暫存器內儲存的數值與二進位樣式:
- 題目給定以 8 位元儲存之十進位編碼數值:
- 第一個運算元:
- 帶號解讀:符號位為 (負數),實際值為 。
- 第二個運算元:
- 帶號解讀:符號位為 (負數),實際值為 。
- 第一個運算元:
- 題目給定以 8 位元儲存之十進位編碼數值:
- 進行 8 位元二進位加法:
- 最前端產生的進位 超出 8 位元範圍,硬體自動捨棄(Truncated),保留低 8 位元:。
- 將結果轉換為十進位:
- 結果 的最高位(MSB)為 (正數),其十進位真值為:
(註:若題幹敘述中前句假設之 與 參與計算,因 ,二進位加法截斷後結果亦同為 )。
- 結果 的最高位(MSB)為 (正數),其十進位真值為:
(b) 8-bit 無號整數飽和運算(Saturating Arithmetic)
- 無號數運算範圍:
- 8 位元無號整數的數值範圍為 。
- 計算算術總和:
- 套用飽和機制:
第 3 題12 分
For a direct-mapped cache design with 32-bit address [31-0], the following bits of the address are used to access the cache:
| Tag field | Index field | Offset field |
|---|---|---|
| [31-14] | [13-6] | [5-0] |
(a) (3%) What is the block size (in words)?
(b) (3%) How many entries does the cache have?
(c) (3%) If 1 bit for the valid field is used, what is the total number of Kibits in a direct-mapped cache?
(d) (3%) Assume the miss rate of an instruction cache is 3% and the miss rate of the data cache is 2%. The frequency of all loads and stores instruction is 36%. If a processor has a CPI of 2 without any memory stalls and the miss penalty is 100 cycles for all misses, how much faster a processor would run with a perfect cache that never missed.
登入後即可作答並保存紀錄。
(a) Block size (words)
Offset 欄位有 6 位 ⇒ 每塊位元組數 = 。
1 word = 4 bytes ⇒ block size = 。
(b) Cache 條目數目
Index 欄位有 8 位 ⇒ 條目數 = 。
(c) 總位元數 (Kibits)
- Tag 位元 = 18 位
- Valid 位元 = 1 位
- Data 位元 = block size 位
單一條目位元 = 位。
全部位元 = 位。
第 4 題10 分
Caches are important to providing a high-performance memory hierarchy to processors. Below is a list of 32-bit memory address-references, given as word addresses.
3, 180, 43, 2, 191, 88, 190, 14, 181, 44
(a) (3%) Assuming the cache is initially empty. For each of these references, given a direct-mapped cache with two-word blocks and a total size of 8 blocks. What is the miss rate?
(b) (3%) If the miss stall time is 25 cycles and 3 cycles for access time, what is the total cycles for this cache?
(c) (4%) For a three-way set associative cache with two-word blocks and a total size of 24 words. Use LRU replacement. What is the hit ratio?
登入後即可作答並保存紀錄。
(a) Direct‑mapped、2 word / 8 blocks
| 參考位址 | Block # | Index (mod 8) | Tag | 命中/未命中 |
|---|---|---|---|---|
| 3 | 1 | 1 | 0 | 未命中 |
| 180 | 90 | 2 | 11 | 未命中 |
| 43 | 21 | 5 | 2 | 未命中 |
| 2 | 1 | 1 | 0 | 命中 |
| 191 | 95 | 7 | 11 | 未命中 |
| 88 | 44 | 4 | 5 | 未命中 |
| 190 | 95 | 7 | 11 | 命中 |
| 14 | 7 | 7 | 0 | 未命中 |
| 181 | 90 | 2 | 11 | 命中 |
| 44 | 22 | 6 | 2 | 未命中 |
未命中次數 = 7,參考次數 = 10
(b) 總週期數
每個存取的基礎存取時間=3 cycles,未命中額外延遲=25 cycles
(c) 三路組合集(2 word / 24 words)
第 5 題7 分
Listed below are key page table parameters.
| Virtual address size | Page size | Page table entry size |
|---|---|---|
| 32 bits | 8 KiB | 4 bytes |
(a) (3%) For a single-level page table, how much physical memory is needed for storing the page table?
(b) (4%) Calculate the total page table size for a system running 6 applications that utilize half of the memory available (it means that half of 32-virtual address for each running application).
登入後即可作答並保存紀錄。
核心觀念
本題考驗計算機系統與作業系統中**虛擬記憶體(Virtual Memory)之分頁機制(Paging System)**的基礎計算與頁表結構特性。核心概念包含:
-
虛擬位址分割(Virtual Address Partitioning):
位元的虛擬位址(Virtual Address, VA)在分頁架構下會被劃分為兩部分:- 虛擬頁號(Virtual Page Number, VPN):用於索引分頁表(Page Table)。
- 頁內位移(Page Offset, PO):決定頁面內的具體位址,其位元數由**分頁大小(Page Size)**決定。
-
單階分頁表(Single-Level Page Table)之連續陣列特性:
單階分頁表本質上是一個以 VPN 作為陣列索引(Index)的直接尋址表格。為了使硬體記憶體管理單元(MMU)能夠以 時間進行位址轉換,單階頁表必須為虛擬位址空間中的每一個潛在頁面都預留一個分頁表項目(Page Table Entry, PTE),不論該頁面實際上是否被程式分配或使用(未使用的頁面標記為 Invalid)。 -
行程獨立性(Per-Process Address Space):
作業系統中每個獨立運行的應用程式(Application / Process)都擁有自己獨立的虛擬位址空間與專屬的分頁表,以達到行程間記憶體隔離與保護的目的。
解題方法
子題 (a) 推導步驟
需求:計算單一單階分頁表佔用的實體記憶體大小。
-
計算頁內位移(Page Offset)位元數:
題目給定分頁大小為 。
因此,頁內位移需要 。 -
計算虛擬頁號(VPN)位元數:
虛擬位址長度為 。
-
計算分頁表項目總數(Total PTEs):
的 VPN 代表虛擬位址空間最多可劃分為 個頁面。單階分頁表必須包含 個 PTE。
-
計算分頁表記憶體容量:
每個 PTE 大小為 。
子題 (b) 推導步驟
需求:計算系統同時執行 6 個應用程式,且每個應用程式僅使用一半虛擬位址空間時,全系統總分頁表大小。
- 分析單一應用程式的分頁表大小:
單階分頁表採用靜態陣列結構,MMU 透過 直接尋址。
第 6 題10 分
Translate the following C codes into MIPS instructions. Assume that the variables f, g, h, i, and j are assigned to registers s1, s3, and s6 and $s7, respectively.
int leaf_example(int g, int h, int i, int j) {
int f;
f = (A[B[g]+1] + h ) - (i + j);
return f;
}
Be sure to handle the stack pointer appropriately. Indicate the names of registers and variables stored on the stack and mark the location of $sp.
登入後即可作答並保存紀錄。
核心觀念
本題旨在考驗考生對 MIPS 組合語言翻譯、暫存器呼叫約定(Register Calling Convention)、Stack 堆疊空間管理 以及 多重陣列索引定址計算 的理解。
-
暫存器呼叫約定(Calling Convention):
- Callee-saved Registers(
$s0–$s7):被呼叫函式(Callee)若欲寫入或修改這些暫存器,必須在進入函式時(Prologue)先將其舊值壓入堆疊(Stack)備份,並在離開函式前(Epilogue)將其還原,以確保呼叫者(Caller)的資料不受破壞。 - Caller-saved / Temporary Registers(
$t0–$t9):暫存用暫存器,被呼叫函式可自由使用與覆寫,無須在 Stack 中保留其值。 - Return Value Register(
$v0):依據 MIPS ABI 規範,函式回傳值應置於$v0(或包含$v1)。 - Return Address Register(
$ra):儲存返回呼叫者的位址。本題函式為葉函式(Leaf Function),未再呼叫其他子函式,因此$ra不會被覆蓋,無須備份至 Stack。
- Callee-saved Registers(
-
記憶體定址與陣列索引(Array Indexing & Address Calculation):
- C 語言中
int資料型態占 4 個位元組(4 bytes / 32-bit word)。 - 記憶體位址以 Byte 為單位定址,故陣列索引 對應之位元組偏移量(Byte Offset)為 。
- 在 MIPS 中,乘以 4 可直接透過邏輯左移指令
sll rd, rs, 2(移位 2 位元,)以更高的效能達成。 - 陣列 之記憶體位址公式:
- 巢狀陣列 之記憶體位址公式:
- C 語言中
解題方法
將 C 語言程式碼拆解為五個主要步驟進行推導:
步驟一:處理 Stack Pointer 與暫存器備份(Prologue)
題目指定變數 分配於暫存器 $s0。由於 $s0 屬於 Callee-saved 暫存器,且在本函式中將會進行指派寫入(),因此必須在 Stack 上配置 4 位元組空間,並將舊的 $s0 值備份:
addi $sp, $sp, -4 # 將 Stack Pointer 向低位址移動 4 bytes
sw $s0, 0($sp) # 將呼叫者的舊 $s0 值寫入 0($sp) 保留
步驟二:計算並載入
- 變數 位於
$s1$,陣列 的基底位址位於$s7$。 - 計算 取得偏移量:
sll $t0, $s1, 2 - 計算 的記憶體位址:
add $t0, $s7, $t0 - 從記憶體取出 的值:
lw $t0, 0($t0)
步驟三:計算索引 並載入
- 將取得的 值加 1 得到新索引:
addi $t0, $t0, 1 - 計算 取得陣列 的位元組偏移量:
sll $t0, $t0, 2 - 計算 的記憶體位址(陣列 的基底位址位於
$s6$):add $t0, $s6, $t0 - 從記憶體取出 的值:
lw $t1, 0($t0)
步驟四:執行算術運算與結果賦值
- 計算 ,其中 位於
$s2$:add $t1, $t1, $s2 - 計算 ,其中 分別位於
$s3,$s4$:add $t2, $s3, $s4 - 執行減法並將結果存回 (即
$s0$):sub $s0, $t1, $t2 - 將回傳值寫入回傳暫存器
$v0$:move $v0, $s0
步驟五:還原 Stack 與返回(Epilogue)
在函式結束離開前,還原原本的 $s0 並歸還 Stack 空間,最後跳轉回 Caller:
lw $s0, 0($sp) # 還原舊的 $s0 值
addi $sp, $sp, 4 # 釋放 Stack 4 bytes 空間
jr $ra # 返回呼叫者
暫存器與 Stack 保存策略分析
為了完整驗證編譯邏輯,針對各暫存器於本題中是否需要壓入 Stack 保存進行逐一分析:
$s0(變數 ):需保存。`
第 7 題10 分
Consider the following MIPS loop:
LOOP:
slt $t2, $0, $t1
beq $t2, $0, DONE
subi $t1, $t1, 1
addi $s2, $s2, 2
j LOOP
DONE:
(a) (3%) Assume that the register s2 assuming t1 is initialized to the value N. How many MIPS instructions are executed?
(c) (4%) Assume that the registers s2, t2 are integers A, B, i, and temp, respectively, write the equivalent C code routine.
登入後即可作答並保存紀錄。
核心觀念
本題主要測驗 MIPS 組合語言的控制流程(Control Flow)、指令執行計數(Instruction Count)以及組合語言與 C 語言的結構轉換:
- 比較與條件分支指令:
slt rd, rs, rt(Set on Less Than):若 則 ,否則 。beq rs, rt, label(Branch on Equal):若 則跳躍至指定標籤。
- 基本迴圈結構:
- 迴圈體(Loop Body)包含條件測試(Test)、迴圈本體運算(Loop Payload)與無條件跳躍(
j LOOP)。 - 計算指令執行總數時,必須區分「進入迴圈成功執行的次數」與「最後一次條件不成立跳出迴圈所消耗的指令數」。
- 迴圈體(Loop Body)包含條件測試(Test)、迴圈本體運算(Loop Payload)與無條件跳躍(
解題方法與詳細推導
組合語言邏輯分析
分析 LOOP 內部的指令行為:
slt $t2, $0, $t1:檢查 $$(即 $ 是否成立)。若成立則 $,否則 $。beq $t2, $0, DONE:若 $(即 $),條件不滿足,跳出迴圈至DONE。subi $t1, $t1, 1:$$(遞減計數器)。addi $s2, $s2, 2:$$(累加變數)。j LOOP:無條件跳回迴圈開頭繼續執行。
(a) 求暫存器 $s2 最終的值
-
初始狀態:
- $
- $
-
迴圈執行過程:
- 第 1 次迭代:$$ 變為 ,$ 變為
- 第 2 次迭代:$$ 變為 ,$ 變為
- 第 10 次迭代:$$ 變為 ,$ 變為
- 第 11 次測試:$,
slt設定 $,beq成立直接跳至DONE。
-
結論:
迴圈共完整執行 10 次,暫存器 `
第 8 題25 分
Consider the following fragment of MIPS code:
sw r16,12(r6)
lw r16,8(16)
beq r5,r4,Label # Assume r5!=r4
add r5,r1,r4
slt r5,r15,r4
Assume that the individual pipeline stage of IF, ID, Exe, Mem, and WB has the latency of 200ps, 120ps, 150ps, 190ps, and 100ps, respectively.
(a) (4%) Assume that all branches are perfectly predicted and that no delay slots are used. If we only have one memory (for both instructions and data), there is a structural hazard every time we need to fetch an instruction in the same cycle in which another instruction accesses data. To guarantee forward progress, this hazard must always be resolved in favor of the instruction that accesses data. What is the total execution time of this instruction sequence in the 5-stage pipeline that only has one memory? We have seen that data hazards can be eliminated by adding nops to the code. Can you do the same with this structural hazard? Why?
(b) (5%) Assume that all branches are perfectly predicted and that no delay slots are used. If we change load/store instructions to use a register (without an offset) as the address, these instructions no longer need to use the ALU. As a result, Mem and Exe stages can be overlapped and the pipeline has only 4 stages. Change this code to accommodate this changed ISA. Assuming this change does not affect clock cycle time, what speedup is achieved in this instruction sequence?
(c) (4%) Assuming stall-on-branch and no delay slots, what speedup is achieved on this code if branch outcomes are determined in the, ID stage, relative to the execution where branch outcomes are determined in the Exe stage?
(d) (4%) Given these pipeline stage latencies, repeat the speedup calculation from (b), but take into account the (possible) change in clock cycle time.
(e) (4%) Given these pipeline stage latencies, repeat the speedup calculation from (c), taking into account the (possible) change in clock cycle time. Assume that the latency ID stage increases by 50% and the latency of the Exe stage decreases by 10ps when branch outcome resolution is moved from Exe to ID.
(f) (4%) Assuming stall-on-branch and no delay slots, what is the new clock cycle time and execution time of this instruction sequence if beq address computation is moved to the Mem stage? What is the speedup from this change?
登入後即可作答並保存紀錄。
(a)
5 階段 。
時鐘週期取最長階段,即 。
因為唯一記憶體同時服務 IF 與 MEM,若有指令在 MEM 時另一指令正於 IF,必須讓 IF 停止。
在本序列中
- 第 1 條 sw 在第 4 週期使用 MEM,第 4 週期的 IF 會衝突 → 必須插入 1 個泡沫。
- 第 2 條 lw 在第 5 週期使用 MEM,第 5 週期的 IF 也衝突 → 再插入 1 個泡沫。
加入兩個結構性停頓後的週期表 (省略暫停週期的空格) 為 11 週期:
可以在每條 load/store 後插入一條 nop,使其在 MEM 週期時後面的指令不會同時取指,等效於上述兩個泡沫。因此 可以 用 nop 消除結構性危險,但仍會多耗兩個週期。
【答案】 總執行時間 2200 ps;結構危險可透過在每個 memory 指令後插入 nop 解除,仍需兩個額外週期。
(b)
ISA 改為 load/store 只需寄存器位址,故不使用 EX,MEM 與 EX 合併成一個階段,流水線變為 4 階段 。
改寫程式碼(去除位移):
sw r16, r6
lw r16, r16
beq r5, r4, Label
add r5, r1, r4
slt r5, r15, r4
在 4 階段流水線中理想週期為 。
仍有相同的結構危險 (在 MEM/EX 週期與 IF 同時使用記憶體),每條 memory 指令仍需插入 1 個停頓 → 再加 2 個泡沫,總週期 10。
時鐘週期仍為 200 ps(最長仍是 IF),故
【答案】 重新編寫如上,執行時間 2000 ps,速度提升 10 %。
第 9 題5 分
Answer the following questions related a multiple processors system.
(a) (2%) Suppose you want to achieve a speed-up of 80 times faster with 100 processors. What fraction of the original computation can be sequential?
(b) (3%) One major problem in the multiple processors system is the remote access, because communications of data between separate processors are overheads. Suppose we have a program running on a 32-processor system, which has an average of 200 ns to handle a remote data access. And, the clock period of each processor is 0.3 ns. For the simplicity, in this program we assume that all the data accesses, except remote ones, hit in their local cache and the processor will be stalled on a remote request. If the base CPI is 0.5, how much faster is the multiprocessor if there is no remote access versus if 0.2% of the instructions involve a remote access?
登入後即可作答並保存紀錄。
核心觀念
本題考查多處理器系統(Multiprocessor Systems)效能評估的兩大核心指標:
-
阿姆達爾定律(Amdahl's Law)與平行化瓶頸:
- 用於評估改善系統某一部分(如增加處理器數量)對整體系統所能帶來的極限加速比(Speedup)。
- 阿姆達爾定律公式:
其中 為整體加速比、 為處理器數量、 為無法平行化的串行運算比例(Sequential Fraction)、 為可完全平行化的運算比例。
-
遠端記憶體存取開銷(Remote Access Overhead)對平均 CPI 的影響:
- 在多處理器系統(如 NUMA 架構)中,存取遠端處理器記憶體會產生額外的通訊延遲。
- 考量遠端存取停頓(Stall)時的有效每指令週期數(Effective CPI):
- 每次遠端存取的停頓週期數計算方式:
解題方法
(a) 小題:
- 已知處理器數量 、目標加速比 。
- 代入阿姆達爾定律公式 。
- 透過代數移項求出串行比例 。
(b) 小題:
- 計算單次遠端存取的處理器停頓週期數(Stall Cycles):
- 計算有 0.2% 遠端存取時的實際 CPI():
- 完全無遠端存取的理想 CPI 即為 Base CPI()。
- 求無遠端存取相較於有遠端存取時的加速倍數:
題目解析與推導
(a) 求解串行比例
將 與 代入阿姆達爾定律:
兩邊同乘以分母:
通分簡化:
求得串行比例 :
(b) 求解遠端存取對效能的影響
- 計算每次遠端存取之停頓週期數:
- 時脈週期(Clock Period)
- 遠端存取延遲(Remote Access Time)