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

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

第 1 題15 分

For the digital arithmetic and logic designs, please select which statements are correct?
(Hints: Only five statements are correct)

(A) The full-adder can be implemented with two half adders and one NOR gate.
(B) For an nn-bit Ripple Carry Adder, there are 2n2n gate levels for the carry to propagate from input to output.
(C) To design an unsigned 16-bit ×\times 10-bit multiplier, the maximum bits width for the output is 26.
(D) We can design a combinational circuit by using a ROM. For example, the circuit accepts a two-bit number and outputs a binary number equal to the quadruple of the input number.
(E) The SR latch is a circuit with two cross-coupled OR gates or two cross-coupled AND gates.
(F) The D flip-flop can be constructed with a SR latch and combinational gates.
(G) The D flip-flop can be controlled by the positive-edge clock or negative-edge clock.
(H) The D latch can be controlled by the positive-level response.

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

這一題的完整詳解

核心觀念

本題涵蓋數位邏輯設計與計算機組織基礎運算單元(Arithmetic and Logic Design)的核心主題:

  1. 加法器與漣波進位加法器(Ripple Carry Adder, RCA):
    • 全加法器(Full Adder, FA)由兩個半加法器(Half Adder, HA)與一個 OR 閘組成。
    • 傳統由標準邏輯閘(AND/OR/XOR)實現的 nn-bit RCA,其進位鏈(Carry Chain)傳播延遲為 2n2n 級邏輯閘延遲(Gate Levels)。
  2. 乘法器輸出寬度:
    • 兩個無號整數 AA(mm 位元)與 BB(nn 位元)相乘,其最大乘積長度為 m+nm + n 位元。
  3. 唯讀記憶體(ROM)作為組合邏輯電路:
    • ROM 本質上為解碼器(Decoder)加上 OR 陣列,可做為查表法(Look-Up Table, LUT)實現任何給定輸入輸出位元的組合邏輯函式。
  4. 正反器(Flip-Flop)與閂鎖器(Latch)之結構與觸發機制:
    • 基本 SR Latch 可由兩組交叉耦合(Cross-Coupled)的 NOR 閘或 NAND 閘構成,不可由純 OR 閘或純 AND 閘構成。
    • D Flip-Flop(邊緣觸發)可由 SR Latch / D Latch 與額外邏輯閘(如主從式 Master-Slave 架構或傳輸閘/邏輯閘架構)實現。
    • 邊緣觸發正反器可設計為正緣觸發(Positive-edge triggered)或負緣觸發(Negative-edge triggered)。
    • 閂鎖器(Latch)為準位感應(Level-sensitive)元件,可操作於高準位觸發(Positive-level response)或低準位觸發。

解題方法與選項分析

題目提示共有 5 個正確陳述(Only five statements are correct),我們逐一檢視各選項:

  • (A) 錯誤

    • 半加法器之輸出為:
      S1=A⊕B,C1=A⋅BS_1 = A \oplus B, \quad C_1 = A \cdot B
    • 將第二個半加法器加入進位輸入 CinC_{\text{in}} 後:
      S=S1⊕Cin=A⊕B⊕CinS = S_1 \oplus C_{\text{in}} = A \oplus B \oplus C_{\text{in}}
      C2=S1⋅Cin=(A⊕B)⋅CinC_2 = S_1 \cdot C_{\text{in}} = (A \oplus B) \cdot C_{\text{in}}
    • 最終進位輸出為:
      Cout=C1+C2=(A⋅B)+(A⊕B)CinC_{\text{out}} = C_1 + C_2 = (A \cdot B) + (A \oplus B)C_{\text{in}}
    • 結合兩個半加法器的進位項需要一個 OR 閘(或經 De Morgan 定律推導時為 NOR 搭配反相),而非直接由一個 NOR 閘達成。因此本選項敘述錯誤。
  • (B) 正確

    • 在標準 1-bit 全加法器中,進位輸出邏輯式通常化簡為 Ci+1=AiBi+(Ai⊕Bi)CiC_{i+1} = A_i B_i + (A_i \oplus B_i)C_i(由一個 AND 閘與一個 OR 閘串接傳播進位),因此進位從 CiC_i 到 Ci+1C_{i+1} 必須穿過 2 級邏輯閘(Gate Levels,即 1 級 AND 加上 1 級 OR,或以 NAND-NAND 實現亦為 2 級)。
    • 對於 nn-bit 漣波進位加法器(RCA),進位訊號從第 0 級傳遞到第 n−1n-1 級的進位輸出,共需穿過:
      2×n=2n gate levels2 \times n = 2n \text{ gate levels}
    • 因此本選項敘述正確。
  • (C) 正確

    • 無號數乘法器兩輸入分別為 16-bit 與 10-bit:
      • 16-bit 無號數最大值為 216−12^{16} - 1。
      • 10-bit 無號數最大值為 210−12^{10} - 1。
    • 兩者乘積的最大值為:
      (216−1)×(210−1)=226−216−210+1<226(2^{16} - 1) \times (2^{10} - 1) = 2^{26} - 2^{16} - 2^{10} + 1 < 2^{26}
    • 表示該乘積必須使用 26 個位元才能保證不發生溢位(即輸出位元寬度為 16+10=2616 + 10 = 26 位元)。因此 maximum bits width 為 26 是正確的。
  • (D) 正確

    • 任何組合邏輯真值表皆可儲存在 ROM 中以查表(LUT)方式實現。
    • 題目中輸入為 2-bit 整數 X∈{0,1,2,3}X \in \{0, 1, 2, 3\},輸出為輸入的 4 倍(Quadruple):
      Y=4×X=X≪2Y = 4 \times X = X \ll 2
    • 當 XX 最大為 3(10)=11(2)3_{(10)} = 11_{(2)} 時,Y=12(10)=1100(2)Y = 12_{(10)} = 1100_{(2)},輸出僅需 4-bit。使用容量為 4×44 \times 4 位元的 ROM 即可完整實現此組合邏輯電路。因此本選項正確。
🔒

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

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

免費註冊

第 2 題10 分

For MIPS assembly languages, please select proper "Meaning" by using M1, M2, M3, M4, and M5. Copy the following table (Table 1) to your answer sheet and fill in the five corresponding "Meaning", where the five "Meaning" are listed as follows:

M1: $s1=$s2+200;
M2: if($s1!=$s2) goto PC+100
M3: if($s2 < $s3) $s1=1; else $s1=0;
M4: Memory[$s2+100]=$s1;
M5: $s1=Memory[$s2+100];

Table 1

Instructions"Meaning"
Store word
Load word
Set on less than
Branch on not equal
Add immediate unsigned

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

這一題的完整詳解

核心觀念

本題考查 MIPS 架構之核心指令語意(Instruction Semantics) 與對應的 RTL(Register Transfer Language)硬體暫存器傳輸語意。主要涵蓋五種最基本的指令類型:

  1. 資料傳輸(Data Transfer):
    • sw(Store Word):將暫存器的值寫入記憶體位址(Memory[Base + Offset] = rt)。
    • lw(Load Word):自記憶體位址讀取值並載入暫存器(rt = Memory[Base + Offset])。
  2. 條件分支與比較(Branch & Comparison):
    • slt / sltu(Set on Less Than):比較兩暫存器大小,若前者小於後者則將目的暫存器設為 1,否則設為 0。
    • bne(Branch on Not Equal):兩暫存器值不相等時跳躍至目標位址(PC 相對定址)。
  3. 算術運算(Arithmetic):
    • addiu(Add Immediate Unsigned):將暫存器加上立即值,結果寫入目的暫存器(忽略溢位處理)。

解題方法

逐一將 Table 1 中的五個指令名稱對照其標準 MIPS 組合語言語意,並從題目提供的候選描述(M1~M5)中選出對應的陳述:

  1. Store word (sw):
    • 功能是將暫存器的內容存入記憶體中。
    • 對照 M4:Memory[$s2+100]=$s1;(將暫存器 $s1 的值存入以 $s2 為基底加上偏移量 100 的記憶體位址)。
  2. Load word (lw):
    • 功能是從記憶體讀取資料載入至暫存器。
    • 對照 M5:$s1=Memory[$s2+100];(從位址 $s2+100 讀取資料並寫入暫存器 $s1)。
  3. Set on less than (slt):
    • 語法形式為 slt $s1, $s2, $s3,若 $s2 < $s3 成立則 $s1 = 1,否則 $s1 = 0。
    • 對照 M3:if($s2 < $s3) $s1=1; else $s1=0;。
  4. Branch on not equal (bne):
    • 語法形式為 bne $s1, $s2, Label,當兩暫存器不相等時發生跳躍(更新 Program Counter)。
    • 對照 M2:if($s1!=$s2) goto PC+100。
  5. Add immediate unsigned (addiu):
    • 語法形式為 addiu $s1, $s2, 200,代表將 $s2 加上立即值常數 200 後存入 $s1。
    • 對照 M1:$s1=$s2+200;。

選項(語意)分析

題目所給之五個 Meaning(M1~M5)細節分析如下:

  • M1($s1=$s2+200;):
    屬於 I-Format 算術指令,將暫存器與常數(Immediate)相加。對應指令為 Add immediate unsigned(或 addi)。
  • M2(if($s1!=$s2) goto PC+100):
    屬於條件分支指令,比較兩暫存器是否不相等,不相等則轉移控制權至目標位址。對應指令為 Branch on not equal。
  • **M3(`if(s2<s2 < s3) $s1=1; else
🔒

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

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

免費註冊

第 3 題10 分

The following C codes are compiled into the corresponding MIPS assembly codes. Assume that the two parameters array and size are found in the registers $a0 and $a1, and allocates p to register $t0.

C codes:

clear(int *array, int size)
{
    int *p;
    for (p = &array[0]; p < &array[size]; Code 1)
        Code 2;
}

MIPS assembly codes:

move $t0, a0
loop : sw $zero, 0($t0)
OP1 $t0, $t0, 4
add $t1, $a1, $a1
add $t1, $t1, $t1
OP2 $t2, $a0, $t1
OP3 $t3, $t0, $t2
bne $t3, $zero, loop

Please determine proper C codes for (C1, C2) and proper instructions for (OP1, OP2, OP3). Copy the following table (Table 2) to your answer sheet and fill in the two C codes and the three instructions.

Table 2

C Codes / Instructions
Code 1
Code 2
OP1
OP2
OP3

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

這一題的完整詳解

核心觀念

本題考查 C 語言指標運算(Pointer Arithmetic)與 MIPS 組合語言指令的對應關係,特別是經典教科書(Computer Organization and Design: The Hardware/Software Interface, Patterson & Hennessy)中的「陣列(Array)vs. 指標(Pointer)」實作機制。

涉及的計算機組織與程式設計核心觀念包括:

  1. 指標步進(Pointer Increment)與記憶體定址:在 32 位元架構下,int 佔 4 個位元組(bytes)。指標遞增(p++ 或 p = p + 1)在機器語言層次代表位址增加 4(即 +4 bytes)。
  2. 指標歸零清空陣列:將指標指向的記憶體內容填入 0,在 MIPS 中使用 sw $zero, 0($reg)。
  3. 基底位址與邊界計算:
    • 陣列第 size 個元素的位址為 &array[size]=array+size×4\&array[size] = array + size \times 4。
    • 乘以 4 可透過兩次連續的加法(add $t1, $a1, $a1 相當於 ×2\times 2,再 add $t1, $t1, $t1 相當於 ×4\times 4)或左移 2 位元(sll)達成。
  4. 迴圈條件判斷:C 語言的條件判斷式 p < &array[size] 在 MIPS 中使用無號數小於比較指令 sltu(Set on Less Than Unsigned)搭配條件分支指令 bne 來實現。

解題方法

透過逐行比對 C 語言原始碼與產生的 MIPS 組合語言程式碼,推導出各空白欄位:

1. 推導 C 語言程式碼(Code 1 與 Code 2)

  • 分析 MIPS 迴圈主體:
    loop : sw $zero, 0($t0)
           OP1 $t0, $t0, 4
    
    • 第一行 sw $zero, 0($t0) 將暫存器 $zero(值為 0)存入 $t0 所指向的記憶體位址。因 $t0 配置給指標 p,此行對應 C 語言的 *p = 0;。因此 Code 2 為 *p = 0。
    • 第二行 OP1 $t0, $t0, 4 將指標位址加 4 位元組,使指標指向下一個 int 元素。在 C 語言的 for 迴圈中,此步進操作對應 p++(或 p = p + 1)。因此 Code 1 為 p++(或 p = p + 1)。

2. 推導 MIPS 組合語言指令(OP1, OP2, OP3)

  • 推導 OP1:

    • 目的為將 $t0(指標 p 的位址)遞增 4。
    • 暫存器加立即值常數 4,使用立即值加法指令:
      t0←t0+4t0 \leftarrow t0 + 4
      對應指令為 addi(或使用無號數不產生溢位的 addiu)。因此 OP1 為 addi(或 addiu)。
  • 推導 OP2:

    • 前面兩行組合語言指令:
      add $t1, $a1, $a1    # $t1 = size + size = 2 * size
      add $t1, $t1, $t1    # $t1 = 2 * size + 2 * size = 4 * size
      
      計算出位元組偏移量 4×size4 \times size。
    • 接續指令 OP2 $t2, $a0, $t1 需計算陣列邊界位址 &array[size]\&array[size]:
      &array[size]=base address ($a0)+4×size (offset in $t1)\&array[size] = \text{base address } (\$a0) + 4 \times size\ (\text{offset in } \$t1)
    • 兩暫存器相加,對應指令為 add(或 addu)。計算結果存入 $t2,即 $t2 = \&array[size]。因此 OP2 為 add(或 addu)。
  • 推導 OP3:

    • 迴圈判斷式為 p < &array[size],即比較 $t0(指標 p)是否小於 $t2(邊界位址)。
    • 若條件成立(p<&array[size]p < \&array[size]),迴圈繼續執行;接續的指令為:
      bne $t3, $zero, loop
      
      此行表示當 $t3 != 0(即條件為真時),跳回 loop 標籤。
🔒

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

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

免費註冊

第 4 題15 分

For a compiler design, the engineer works to decide between two code sequences for a specific computer, and the hardware of the specific computer performs the following CPI parameters:

Table 3: CPI for each instruction set

ABCD
CPI4213

For a high-level language program, the compiler engineer considers two code sequences which need the instruction counts as follows:

Table 4: Instruction counts for each instruction set

Code sequenceABCD
Code sequence 1100200300300
Code sequence 2200100300200

(a) (5%) Which code sequence executes the larger instructions? Please also show your calculations.
(b) (5%) Which code sequence is faster? Please also show your calculations.
(c) (5%) What is the CPI for each code sequence? Please also show your calculations.

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

這一題的完整詳解

核心觀念

本題評估計算機效能的核心指標與經典的 CPU 效能方程式(CPU Performance Equation):

  1. 總指令數(Instruction Count, ICIC):

    IC=∑iICiIC = \sum_{i} IC_i

    其中 ICiIC_i 為第 ii 類指令的執行次數。

  2. 總執行時脈週期數(CPU Clock Cycles):

    CPU Cycles=∑i(CPIi×ICi)\text{CPU Cycles} = \sum_{i} (CPI_i \times IC_i)

    其中 CPIiCPI_i 為第 ii 類指令所需的時脈週期數(Cycles Per Instruction)。

  3. 平均每條指令週期數(Average CPI):

    CPIavg=CPU CyclesIC=∑i(CPIi×ICiIC)CPI_{\text{avg}} = \frac{\text{CPU Cycles}}{IC} = \sum_{i} \left( CPI_i \times \frac{IC_i}{IC} \right)
  4. 執行時間(CPU Execution Time)與效能比較:

    CPU Time=CPU Cycles×Clock Cycle Time=CPU CyclesClock Rate\text{CPU Time} = \text{CPU Cycles} \times \text{Clock Cycle Time} = \frac{\text{CPU Cycles}}{\text{Clock Rate}}

    在同一台硬體電腦(Specific computer)上,時脈週期時間(Clock Cycle Time)相同,因此執行週期數越少者,執行時間越短、速度越快。


解題方法

(a) 計算兩組程式碼序列的總指令數(Instruction Count)

  • Code sequence 1:

    IC1=100+200+300+300=900 instructionsIC_1 = 100 + 200 + 300 + 300 = 900 \text{ instructions}
  • Code sequence 2:

    IC2=200+100+300+200=800 instructionsIC_2 = 200 + 100 + 300 + 200 = 800 \text{ instructions}

因為 900>800900 > 800,故 Code sequence 1 執行的指令數較多。


(b) 計算兩組程式碼序列所需的總時脈週期數並比較效能速度

在相同硬體條件下,時脈週期時間 TcT_c 恆定,執行時間正比於總時脈週期數(CPU Cycles\text{CPU Cycles})。

  • Code sequence 1 的總週期數:

    Cycles1=(4×100)+(2×200)+(1×300)+(3×300)\text{Cycles}_1 = (4 \times 100) + (2 \times 200) + (1 \times 300) + (3 \times 300) Cycles1=400+400+300+900=2000 cycles\text{Cycles}_1 = 400 + 400 + 300 + 900 = 2000 \text{ cycles}
  • Code sequence 2 的總週期數:

    Cycles2=(4×200)+(2×100)+(1×300)+(3×200)\text{Cycles}_2 = (4 \times 200) + (2 \times 100) + (1 \times 300) + (3 \times 200) Cycles2=800+200+300+600=1900 cycles\text{Cycles}_2 = 800 + 200 + 300 + 600 = 1900 \text{ cycles}

由於 Cycles2=1900<Cycles1=2000\text{Cycles}_2 = 1900 < \text{Cycles}_1 = 2000,Code sequence 2 所需的執行時間較少,因此 Code sequence 2 執行速度較快(Faster)。

若進一步計算加速比(Speedup):

Speedup=Cycles1Cycles2=20001900≈1.053(約快 5.26%)\text{Speedup} = \frac{\text{Cycles}_1}{\text{Cycles}_2} = \frac{2000}{1900} \approx 1.053 \text{(約快 5.26\%)}

(c) 計算各程式碼序列的平均 CPI

將各序列的總週期數除以其各自的總指令數:

  • Code sequence 1 的 CPI: CPI1=Cycles1IC1=2000900=209≈2.22CPI_1 = \frac{\text{Cycles}_1}{IC_1} = \frac{2000}{900} = \frac{20}{9} \approx 2.22
🔒

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

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

免費註冊

第 5 題15 分

Consider the following sequence of instructions in a MIPS pipelined processor:

1. lw  $t0, 0($t1)
2. add $t2, $t0, $t3
3. sub $t4, $t2, $t5
4. and $t6, $t4, $t7

(a) (10%) Identify all types of pipeline hazards in this instruction sequence.
(b) (5%) Suggest a method to resolve the hazards.

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

這一題的完整詳解

核心觀念

本題探討經典 MIPS 5 階段管線架構(IF、ID、EX、MEM、WB)中的管線危障(Pipeline Hazards)​與資料相依性(Data Dependency)。

  1. 管線危障分類:
    • 結構危障(Structural Hazard):硬體資源衝突(例如同時存取單一記憶體)。
    • 控制危障(Control Hazard):分支或跳躍指令造成下一條指令位址未定。
    • 資料危障(Data Hazard):後續指令需要先前記指令尚未產生的結果。
  2. 資料相依性類型:
    • RAW(Read After Write,真相依性):指令 jj 需讀取指令 ii 寫入的暫存器,在管線未處理時會發生 Data Hazard。
    • 在標準 MIPS 循序發射(In-order execution)管線中,WAR(Write After Read)與 WAW(Write After Write)不會形成危障。
  3. RAW 資料危障次分類:
    • Load-Use Data Hazard:lw 的資料在 MEM 階段結束後才能取得,若緊接著下一條 ALU 指令要使用該暫存器,即使有前饋(Forwarding),仍需暫停(Stall)1 個週期。
    • ALU-Use Data Hazard:ALU 指令的計算結果在 EX 階段結束即可取得,若後續指令需要該值,可透過前饋機制完全消除暫停。

解題方法

逐一分析相鄰指令間的資料暫存器相依性:

  1. 分析指令 1 到指令 2:

    • 指令 1:lw $t0, 0($t1) →\rightarrow 目的暫存器為 $t0(在 MEM 階段取得)。
    • 指令 2:add $t2, $t0, $t3 →\rightarrow 來源暫存器為 $t0(在 EX 階段需要輸入)。
    • 危害類型:Data Hazard(明確而言為 Load-Use Hazard)。
  2. 分析指令 2 到指令 3:

    • 指令 2:add $t2, $t0, $t3 →\rightarrow 目的暫存器為 $t2(在 EX 階段產生)。
    • 指令 3:sub $t4, $t2, $t5 →\rightarrow 來源暫存器為 $t2(在 EX 階段需要輸入)。
    • 危害類型:Data Hazard(RAW,ALU-Use Hazard)。
  3. 分析指令 3 到指令 4:

    • 指令 3:sub $t4, $t2, $t5 →\rightarrow 目的暫存器為 $t4(在 EX 階段產生)。
    • 指令 4:and $t6, $t4, $t7 →\rightarrow 來源暫存器為 $t4(在 EX 階段需要輸入)。
    • 危害類型:Data Hazard(RAW,ALU-Use Hazard)。
  4. 結構與控制危障檢驗:

    • 此程式片段無分支/跳躍指令,故無 Control Hazard。
    • 假設採用標準分離式 Instruction/Data Cache 與獨立暫存器讀寫埠,無 Structural Hazard。

各小題完整解析

(a) Identify all types of pipeline hazards in this instruction sequence

此指令序列中僅存在資料危障(Data Hazards / RAW Hazards),具體發生點與類別如下:

  1. Load-Use Data Hazard(指令 1 與指令 2 之間):
    • 指令 1(lw)欲寫入 $t0,而緊鄰的指令 2(add)立刻讀取 $t0 作為運算元。
    • 由於記憶體讀取之數值須至 MEM 階段結束後方能備妥,而 add 在 EX 階段即需要該運算元,形成典型的 Load-Use Data Hazard。
  2. ALU-Use Data Hazard(指令 2 與指令 3 之間):
    • 指令 2(add)將運算結果寫入 $t2,指令 3(sub)緊接著讀取 $t2。
    • 形成 RAW(Read-After-Write)Data Hazard。
  3. ALU-Use Data Hazard(指令 3 與指令 4 之間):
    • 指令 3(sub)將運算結果寫入 $t4,指令 4(and)緊接著讀取 $t4。
    • 同樣形成 RAW(Read-After-Write)Data Hazard。

(b) Suggest a method to resolve the hazards

針對上述危障,標準且最有效的硬體/軟體解決方案為:前饋(Forwarding / Bypassing)搭配管線暫停(Stall / Bubble)。

  1. 前饋技術(Forwarding / Bypassing):
    • 解決 ALU 之間的 RAW 危障(指令 2 →\rightarrow 3、指令 3 →\rightarrow 4):
      • 將 EX/MEM 管線暫存器或 MEM/WB 管線暫存器中已算出的結果,直接拉線繞送至下一指令的 EX 階段 ALU 輸入端。
      • 指令 2(add)在 EX 階段產生的 $t2,透過前饋網路直接送入指令 3(sub)的 EX 階段;指令 3(sub)產生的 `
🔒

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

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

免費註冊

第 6 題20 分

Consider a processor with the following cache hierarchy:

  • L1 Cache: Size: 32 KB, Block size: 64 bytes, 4-way associative, Hit rate: 90%90\%, Latency: 4 cycles
  • L2 Cache: Size: 256 KB, Block size: 128 bytes, 8-way associative, Hit rate: 95%95\%, Latency: 20 cycles
  • Main Memory: Latency: 100 cycles

The processor has a CPI (Cycles Per Instruction) of 1 for computation-only workloads. Memory access frequency is 40%40\% (i.e., 40%40\% of instructions involve memory).

(a) (10%) Calculate the effective CPI (accounting for memory accesses).
(b) (10%) Suggest one architectural change to improve the cache hierarchy performance and estimate the new effective CPI.

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

這一題的完整詳解

核心觀念

本題評量多層快取記憶體(Multi-level Cache Hierarchy)的效能分析與平均記憶體存取時間(AMAT, Average Memory Access Time)對處理器有效每指令週期數(Effective CPI)的影響。

關鍵定義與公式如下:

  1. 多層快取的平均記憶體存取時間(AMAT):
    若採用局部命中率(Local Hit Rate)定義,兩層快取的 AMAT 公式為:

    AMAT=LatencyL1+Miss RateL1×Miss PenaltyL1\text{AMAT} = \text{Latency}_{L1} + \text{Miss Rate}_{L1} \times \text{Miss Penalty}_{L1}

    其中,L1 的 Miss Penalty 即為存取 L2 及後續架構的平均時間:

    Miss PenaltyL1=LatencyL2+Miss RateL2×LatencyMemory\text{Miss Penalty}_{L1} = \text{Latency}_{L2} + \text{Miss Rate}_{L2} \times \text{Latency}_{\text{Memory}}
    • Miss RateL1=1−Hit RateL1\text{Miss Rate}_{L1} = 1 - \text{Hit Rate}_{L1}
    • Miss RateL2=1−Hit RateL2\text{Miss Rate}_{L2} = 1 - \text{Hit Rate}_{L2}(題目未特指時,L2 命中率即為 Local Hit Rate)
  2. 有效 CPI(Effective CPI)計算:
    處理器指令分為「純運算指令(Computation-only)」與「記憶體存取指令(Memory access)」。題目指出純運算指令的 CPIcompute=1\text{CPI}_{\text{compute}} = 1,記憶體存取頻率為 fmem=40%f_{\text{mem}} = 40\%。

    • 標準觀念(記憶體存取花費 AMAT 週期):
      純運算指令花費 1 週期,包含記憶體存取的指令除了指令本身的處理外,需額外花費 AMAT 週期完成記憶體讀寫: Effective CPI=CPIcompute+fmem×AMAT\text{Effective CPI} = \text{CPI}_{\text{compute}} + f_{\text{mem}} \times \text{AMAT}
    • 另類常見假設(將 L1 命中視為管線正常開銷,僅 Miss 產生 Stall):
      若假設 Base CPI = 1 已涵蓋 L1 命中之 4 週期延遲,僅快取未命中需補償停頓週期(Memory Stall Cycles): Stall per Access=Miss RateL1×Miss PenaltyL1\text{Stall per Access} = \text{Miss Rate}_{L1} \times \text{Miss Penalty}_{L1} Effective CPI=CPIbase+fmem×Stall per Access\text{Effective CPI} = \text{CPI}_{\text{base}} + f_{\text{mem}} \times \text{Stall per Access}

解題方法與關鍵步驟

子題 (a) 計算 Effective CPI

  1. 計算 L1 Miss Penalty:
    當 L1 Miss 時,存取 L2。L2 命中延遲為 20 週期;若 L2 亦未命中(未命中率為 1−0.95=0.051 - 0.95 = 0.05),則必須存取主記憶體(延遲 100 週期):

    Miss PenaltyL1=20+(1−0.95)×100=20+0.05×100=20+5=25 cycles\text{Miss Penalty}_{L1} = 20 + (1 - 0.95) \times 100 = 20 + 0.05 \times 100 = 20 + 5 = 25 \text{ cycles}
  2. 計算平均記憶體存取時間(AMAT):
    L1 的命中延遲為 4 週期,未命中率為 1−0.90=0.101 - 0.90 = 0.10:

    AMAT=4+0.10×Miss PenaltyL1=4+0.10×25=4+2.5=6.5 cycles\text{AMAT} = 4 + 0.10 \times \text{Miss Penalty}_{L1} = 4 + 0.10 \times 25 = 4 + 2.5 = 6.5 \text{ cycles}
  3. 計算 Effective CPI:

    • 依題目文意之標準推導(題目指明 CPI=1\text{CPI}=1 僅適用於 computation-only 負載,故記憶體指令需計入完整的存取延遲): Effective CPI=1+0.40×6.5=1+2.6=3.6\text{Effective CPI} = 1 + 0.40 \times 6.5 = 1 + 2.6 = 3.6
    • 補充備註(若評分標準認定 L1 命中已重疊於管線基底 CPI = 1 內,僅懲罰 L1 Miss Stalls): Effective CPI=1+0.40×(0.10×25)=1+0.40×2.5=1+1.0=2.0\text{Effective CPI} = 1 + 0.40 \times (0.10 \times 25) = 1 + 0.40 \times 2.5 = 1 + 1.0 = 2.0

子題 (b) 提出一項改進架構並估算新 Effective CPI

本題為開放式架構設計題,考驗對瓶頸分析與參數權衡的能力。

  1. 效能瓶頸分析:
    檢視 AMAT=4+0.10×25=6.5 cycles\text{AMAT} = 4 + 0.10 \times 25 = 6.5 \text{ cycles},可以發現 L1 Hit Latency 高達 4 週期(佔了 AMAT 的 61.5%61.5\%),且 L1 Miss Rate 高達 10%10\%。若能透過架構調整降低 L1 Miss Rate,將帶來最顯著且合乎常理的效能提升。

  2. 具體架構改善方案:
    將 L1 快取容量由 32 KB 提升至 64 KB(或提高關聯度至 8-way)。

🔒

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

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

免費註冊

第 7 題15 分

Consider a MIPS pipelined processor with a static branch prediction scheme that always predicts branches as not taken. The following instruction sequence is executed:

1. beq $t0, $t1, Label
2. add $t2, $t3, $t4
3. sub $t5, $t6, $t7
Label:
4. and $t8, $t9, $t10

Assume:

  • The branch is taken.
  • The branch resolution occurs in the ID stage.
  • Fetching and decoding the wrong instructions incurs a 2-cycle penalty.

(a) (5%) How many cycles are wasted due to misprediction?
(b) (10%) If the branch predictor is changed to dynamic (99% accuracy), calculate the expected average branch penalty for 100 branch instructions.

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

這一題的完整詳解

核心觀念

  1. 分支預測與分支處置懲罰(Branch Penalty):

    • 靜態分支預測採「恆不跳躍(Predict Not Taken)」,處理器會預設順序提取接下來的指令(第 2 行 add、第 3 行 sub)。
    • 若實際分支結果為跳躍(Taken),則發生預測錯誤(Misprediction),已進入管線的錯誤指令必須被清除(Flushed),進而產生額外的閒置週期,即為分支處置懲罰(Penalty cycles)。
    • 題目明確給定條件:「Fetching and decoding the wrong instructions incurs a 2-cycle penalty.」,代表每次分支預測失敗時浪費的週期數為 22 個週期(若依標準 MIPS 五級管線,在 ID 階段決定分支且需 2-cycle penalty,表示分支轉移延遲為 2 個時脈週期)。
  2. 動態分支預測與期望分支懲罰(Expected Branch Penalty):

    • 動態分支預測器根據歷史執行紀錄動態預測,設預測準確率為 PcorrectP_{\text{correct}},預測失誤率即為: Pmiss=1−PcorrectP_{\text{miss}} = 1 - P_{\text{correct}}
    • 預測正確時,不會浪費任何週期(Penalty = 00 cycles);預測失誤時,需承擔預測錯誤的懲罰(Penalty = 22 cycles)。
    • 單一分支指令的平均期望分支懲罰(Average Penalty per branch): Average Penalty=Pmiss×Penaltymispredict\text{Average Penalty} = P_{\text{miss}} \times \text{Penalty}_{\text{mispredict}}
    • 若計算 NN 個分支指令的總期望分支懲罰(Total Expected Branch Penalty for NN branches): Total Penalty=N×Average Penalty=N×Pmiss×Penaltymispredict\text{Total Penalty} = N \times \text{Average Penalty} = N \times P_{\text{miss}} \times \text{Penalty}_{\text{mispredict}}

解題方法

(a) 計算預測失誤所浪費的時脈週期數

  1. 情境分析:
    • 採用 Predict Not Taken,因此在 beq 執行時,管線循序擷取了第 2 行 add 與第 3 行 sub。
    • 實際執行時分支成立(Branch is taken),目標跳至 Label(第 4 行 and),發生分支預測錯誤。
  2. 懲罰週期判定:
    • 題目已知條件直接指出:「Fetching and decoding the wrong instructions incurs a 2-cycle penalty.」。
    • 因此,清除因預測錯誤而擷取/解碼的錯誤指令並轉向正確目標,共浪費 22 個時脈週期。

(b) 計算 100 個動態預測分支指令的期望平均分支懲罰

  1. 參數定義:
    • 預測準確率(Accuracy):Pcorrect=99%=0.99P_{\text{correct}} = 99\% = 0.99
    • 預測失誤率(Misprediction Rate): Pmiss=1−0.99=0.01=1%P_{\text{miss}} = 1 - 0.99 = 0.01 = 1\%
    • 預測失誤之處置懲罰:Penaltymispredict=2 cycles\text{Penalty}_{\text{mispredict}} = 2\text{ cycles}
    • 預測成功之處置懲罰:Penaltycorrect=0 cycles\text{Penalty}_{\text{correct}} = 0\text{ cycles}
🔒

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

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

免費註冊

其他考古題