Try   HackMD

2017q1 Homework4 (microarch)

contributed by <rayleigh0407>

預期目標

  • 複習 MIPS 內部設計
  • 回顧計算機組織結構
  • 不只是小考,繼續「誠實面對自己」

作業內容


考慮以下 MIPS 實作,其 data path 如下:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

上圖紅色的 1, 2, 3 對 MIPS 做了對應的修改,請考慮以下狀況,分別解釋對應的程式碼是否能運作:

1. 做了 1: Stuck at 0 left of cut 的修改,sw $s1, 0($s2) 能否運作?這樣的修改會使得哪些程式無法運作,請列出至少兩項組合語言列表

先看看這條信號的功能
這是為了檢測是否將資料寫入 register 的信號 (不論是從 Mem 來或是計算結果)。因此若將這條線保持輸出為 0, 資料就無法由計算或是記憶體存入 register , 因此以下組合語言在存入資料至 Register 時會失效 (計算功能仍有效):

  • R-type instructions : add, sub, sll;
  • I type instructions : lw (move memory data into register), addi, andi, slti.

2. 做了 2 的修改後,以下程式能否運作?解釋並提出可運作的版本

​​​​```
​​​​add $s1, $s1, $s1
​​​​add $s1, $t0, $t1
​​​​```

切掉這條線, 代表上一個指令計算出來的值, 無法以 Data forwarding 的方式取值 (但他只切了一條, 代表指令中 $Rs 或 $Rt 其中一個仍然可以 Forwarding ), 但是在題目的指令中, 並不會發生 Data Hazard, 因此此程式仍然能運作。

3. 做了 3 的修改後,以下程式能否運作?

​​​​```
​​​​addi $s2, $zero, 2
​​​​addi $s1, $zero, 2
​​​​beq $s2, $s1, exit
​​​​```

切掉這條, 代表某些 conditional branch 失效了, 像是 beq, bne。


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →



如上圖所示, beq 及 bne 在比較兩個暫存器時, 若符合要求, 會將 PC 之值轉為 PC + 4 + imm, 砍掉這條即代表這兩個指令永遠只會跳到下一個指令, 無法跳躍到其他地方。

參考上圖可能以為 PC 的來源只有兩個, 其實還有 jal, j, jr 等可以決定 PC 的值.


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

conditional branch 不能用, 那就自己做一個

    ...
Branch:
    subu $t0, $s2, $s1    
    sltiu $t1, $zero, $t0
    xor $t1, $t1, $t1
    sll $t2, $t1, 2
    add $ra, $ra, $t2
    jr $ra
    
    addi $s2, $zero, 2
    addi $s1, $zero, 2
    jal Branch
    j exit
    ...

subu: 兩數相減 ( unsigned )
sltiu: 比較兩數 若 $t0 > 0 則 $t1 = 1 ( unsigned )

若相等, 則 $t1 = 0, 右移兩位後與 $ra 相加, $ra 值不變, return 後就會執行j exit
若不相等, 則 $t1 = 1, 右移兩位後與 $ra 相加, $ra = $ra + 4, return 後會跳過 j exit 指令

參考資料

MIPS Instruction Reference
大二計算機組織講義