# 計算機組織_劉一宇 ## Ch2. Instrution Set Architecture ## MIPS 組合語言 ## 前言 ::: info 組合語言 v.s. 處理器指令集 組合語言:大集合 指令集:小集合 需要 Assembler (組譯器) 把組合語言轉換成處理器可以執行的指令 ::: - 為什麼會這樣? - 有一些組語會轉成 2~3 個指令 - 如:le, ge... => xor, cmp, jump... - 為什麼要這樣? - 增加可讀性 ### Design priciple - priciple 1: simplicity favors regularity - 指令越簡單,對於硬體設計越好 - priciple 2: Smaller is faster - priciple 3: Make the common case fast - priciple 4: Good design demands good ompromises - 好的設計是需要折衷的 ## Outline - Operands - Register - Memory, data transfer - Immediate (Constants) - Signed and Unsigned numbers - 老師要我們自己去回憶 - Represnting instruction - 要怎麼呈現、要怎麼擺放 - Field - Operations - Logical - Decision making and branches - Supporting procedures in hardware - procedures call (function call) - 要怎麼做 recursion??? - 程式整合 (使用習慣 convension) - Addressing - Synchronization - 多處理器間同步議題 - 作業系統課程 - Compile -> Assembly -> Assembler - System software - 不同指令集比較 - Arm and x86 ## 開始 ### Instruction categories - Load/Store - Computation ### Instruction Formats - r format - i format - j format ### Oprends #### Add ```MIPS add $s0 $s1 $s2 # s0 = s1 + s2 ``` - $s0: destination - $s1: source 1 - $s2: source 2 ### Rigester - 通用暫存器 - $r0 - $r31 - ![[Pasted image 20230307112922.png|500]] - PC 指到下一個指令 - HI - LO > 為什麼暫存器 32 個bits? > design priciple 2: Smaller is faster - 16~23: $s0 ~ $s7 - 很像 C variable,因為有 scope 的概念 ### Memory 一個 word 32 bits - lw : load word - sw : save word #### 讀取記憶體位置 ```MIPS lw $t0, 8($s0) ``` $s0 是一個連續記憶體的位址起點(因為記憶體位址為 32bits,放進暫存器剛剛好) 前面的 8 是 offset,必須是常數(不可以是變數) lw 則是把 $s0 + 8 的位址放進 $t0 ``` $t0 = MEM[$s0 + 12] ``` #### 儲存記憶體位址 ```MIPS sw $t0, 12($s0) ``` ``` MEM[$s0 + 12] = $t0 ``` ## Signed ### 2's complement sign extension 把 3 bits 擴充成 4 bits 只要將空的位置填補上 3 bits 的最高位元 ## Representing Instructions 如何把組合語言轉成 01 的機器碼呢? 把指令切成不同欄位,放入資訊 - R-format: For Register - I-format: For immediate - J-format: For jump > RISC-V 的 Instruction type 切成了六種,切得更細,但變得更簡單 ### R-format - opcode 6bits:用於指定指令類型、對於所有 R 型指令該值均為 0,用 funct 來精確指定指令類型 - Rs (Source Register) 5bits:指定第一個來源操作數的暫存器編號 - Rt (Target Register) 5bits:指定目的操作數的暫存器編號 - Rd (Destination Register) 5bits:指定目的地的暫存器編號 - shatmt 5bits: 拿來做 shift 表示位移的位數 - funct 6bits > 為什麼要設定 opcode 都是 0,卻用 function 去定義? > 為什麼不要把 opcode 跟 function 合在一起? ### I-format I 型指令基本上與 R 型相同, opcode 仍然用於指定指令的操作類型 但這裡沒有 funct,所以不同的 I 型指令其 opcode 是不同的 - Rs (Source Register) 5bits:指定第一個來源操作數的暫存器編號 - Rt (Target Register) 5bits:指定來源操作數的暫存器編號或目的操作數的暫存器編號 - immediate 16bits:可以表示 2^16 的不同數值 (signed) > 如果我們存取的常數比 16bits 還大,應該怎麼辦? > Mars 聰明的會把指令拆成兩個 : ) ### Stored-Program Concept 現在的電腦系統有一個大 concept 不管是指令或是資料都存在一起,因此要怎麼做處理要決定於程式設計師(資料型態等) 但同樣會有一個問題是像 buffer overflow 這種沒有做 bonudry check 造成的問題 ## Operations ### Bitwise #### Shift 比較特別的是 sra,因為 shift 本身是除法,但如果我們把一個負數往右移 (111110000) 會變成正數(開頭填0),因此有了一個 sra,會將負數填入 1,正數填 0 ### Decision making and branches 電腦和計算機最大的差別,就是我們可以有不同的決策
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up