Ch2:Instructions: Language of the Computer
本筆記只有抄到 2-12,因為後面的出題機率過低就不寫了
之後應該會補上
本章主要在讓讀者理解電腦真正能「聽懂」的是什麼語言——也就是機器指令(machine instructions),本章將從高階語言一路深入到底層的指令集結構。
2-1 Instruction
- Instruction set(指令集):
- 不同電腦有不同的指令集(但大部分相同)
- 早期電腦使用較簡單的指令集
MIPS 指令集
- 是 RISC 結構的代表
- 被廣泛運用在嵌入式系統
- 是許多現代指令集的經典
2-2 Operations of the Computer Hardware
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
2-3 Operands of the Computer Hardware
Regigster Operands
- 用於算數操作
- MIPS 共有 32 * 32-bit 的 register file
- 用於高頻率存取資料
- 編號從 0 ~ 31
- 32-bit 的資料稱為一個 word
- Assembler name
- Temporary Value:
$t0
到 $t9
- Static variables:
$s0
到 $s7
$zero
:MIPS register 中的預留常數,不可被 overwrite
Memory Operands
- Main memory 用來儲存複合資料
- Array, Structure, Dynamic Data
- 每一個 memory address 對應到一個 bytes
- MIPS 是 Big Endian 的
- Big Endian:MSB first
- Little Endian:LSB first
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 範例:
- h in
$s2
, A in $s3
- C code
注意:Immediate Operands 不可以進行減法
- 在指令中的常數資料
- 不可被用於減法,可以透過加負數來進行減法
2-4 Unsigned and Signed
Unsigned
2's-Complement
- 有號數
- 轉換方式:
- 範例:-14(8-bit)
- 先把無號數轉成 2 進位:
- 0 -> 1, 1 -> 0:
- 加一:
- 範圍: ~
- Sign Extension:
- 當我們要用更大容量的資料型態儲存我們的數字時,就必須做 sign extension
- 正數補 0,負數補 1
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Sign-magnitude
- 有號數,把 sign 和 magnitude 分開表示
- 轉換方式:
- 範例:-14(8-bit)
- Sign = 1(Negative)
- Magnitude =
- Sign + Magnitude:
- 範圍: ~
2-5 Representing Instructions in the Computer
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
op
: operation code (opcode)
rs
: first source register number
rt
: second source register number
rd
: destination source register number
shamt
: shift amount
funct
: function code (extend opcode)
- 範例:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
rt
: destination or source register number
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
2-6 Logical Operation
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
2-7 Conditional Operations
條件判斷
- 條件判斷:當條件符合時跳到指定的 instruction
- 若不符合:繼續往下執行
- beq:if
- if (rs == rt) branch to instruction labeled L1
- bne:if not
- if (rs != rt) branch to instruction labeled L1
- slt:
- sle:
- j:jump
Loop statement
- C code:
- MIPS:
- i in
$s3
, k in $s5
, address of save in $s6
Basic block
- 定義:Basic block 是一連串代碼,包含以下兩個特性:
- 沒有內部分支(embedded branches),除了結尾(例如跳躍或條件分支)。
- 沒有分支目標(branch targets),除了在區塊開頭。
- Compiler 會辨別程式裡的 Basic block 進行優化

Unsigned and Sign Comparison
- Signed:slt, slti
- Unsigned:sltu,sltui

2-8 Supporting Procedures in Computer Hardware
Register 使用
-
$a0
~ $a3
: 參數暫存器(arguments)
- 對應 register 4 ~ 7。
- 用來傳遞函式的參數。
-
$v0
, $v1
: 回傳值暫存器(result values)
- 對應 register 2 和 3。
- 用來存放函式的回傳值。
-
$t0
~ $t9
: 暫時暫存器(temporary values)
- 用來儲存臨時計算結果。
- 可以被呼叫者覆寫(callee 可以自由使用,不用保留)。
-
$s0
~ $s7
: 保存暫存器(saved variables)
- 用來儲存需要保留的資料。
- 必須由被呼叫者保存與恢復(callee 必須在進入與離開時處理)。
-
$gp
: global pointer
- Register 28。
- 指向靜態資料的全域指標。
-
$sp
: stack pointer
- Register 29。
- 指向目前 stack 的頂端,管理函式呼叫時的堆疊空間。
-
$fp
: frame pointer
- Register 30。
- 協助存取目前函式的參數與區域變數。
-
$ra
: return address
- Register 31。
- 儲存函式呼叫的返回位址(通常由
jal
指令自動寫入)。
Procedure call
- jal:jump and link
- 語法:
- 功能:
- 把下一條指令的 address 存到
$ra
裡,儲存跳回來的地方
- 跳到
ProcedureLabel
所代表的副程式位置執行。
- 執行完副程式後會自己跳回來
- jr:Jump register
- 語法:
- 功能:
- 把
$ra
的內容複製到 PC(program counter)
- 放在副程式裡面,這樣就會跳回主程式
- 比較:
指令 |
全名 |
功能描述 |
是否儲存 return address |
用途範例 |
j |
jump |
無條件跳轉到指定的標籤(Address) |
否 |
跳過一段程式碼 |
jal |
jump and link |
跳轉到副程式並儲存返回 Address 至 $ra |
是 |
呼叫副程式(function call) |
jr |
jump register |
跳轉到暫存器指定的 Address(通常是 $ra ) |
否 |
副程式返回、switch-case |
Local Data on the Stack
-
Local data 會被 callee 分配的
-
Procedure frame:
- 用來存放副程式執行所需的資料
- MIPS 會使用
$fp
, $sp
,來標記 procedure frame 的位置
- 程式可以存取 procedure frame 內部的資料

-
以下資料需要用 stack 存:
$s0
~ $s7
$ra
- 第五個以上的 argument(
$a0
~ $a3
不夠存)
Leaf procedure
- 定義:不呼叫任何其他函式的函式
- Example:
- C code:
- MIPS code:
- Arguments g, …, j in
$a0
, …, $a3
- f in
$s0
(hence, need to save $s0
on stack)
- result in
$v0
sw
:把 register 的資料 store 到 memory 中
lw
:把 memory 的資料 load 到 register 中
Non Leaf Procedure
- 定義:有呼叫其他函式的函式
- Example:
- C code:
- MIPS:
- Argument n in
$a0
- Result in
$v0
- 呼叫在其他函式後,register 中的
$a0
, $ra
會被改變,需要用 lw 來還原
Memory layout
- Text:code
- Static data:Global variables
- Dynamic data:儲存於 heap,屬於動態分配的記憶體
- Stack:自動存取

2-9 Communicating with People
- ASCII:128 characters
- Unicode:256 characters

Byte/Halfword Operations
- 主要處理較小單位的應用
- byte = 8 bit
- halfword = 16 bit
- lb:讀取 8 bits 擴充有號數到 32 bits
- lh:讀取 16 bits 擴充有號數到 32 bits
- lbu:讀取 8 bits 擴充無號數到 32 bits
- lhu:讀取 16 bits 擴充無號數到 32 bits
32-bit Constant
- 大部分的常數都只有 16 bit
- 我們可以透過下方方式來組合出一個 32 bit 常數
- lui:把 16 bit 存到高 16 位
- ori:利用 or 運算儲存低 16 位

Branch Addressing
Branch Addressing 用於目標 address 在 PC 附近的情況(移動範圍:16 bit)
- 大部分的目標都在鄰近的 branch 上
- 可以用前進、後退的方式查找

- PC-relative addressing:
- 目標 address = PC(現在的 address) + offset 4
Jump Addressing
Jump Addressing 用於目標 address 離 PC 很遠的情況(移動範圍:26 bit)
- Jump(j, jal)指令可以允許程式移動到 Text segment 中的任何地方
- J format:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 目標 address = PC31…28 : (address 4)
Branch Far Away
我們可以使用 j 指令來取代 branch 太遠的情形
MIPS 存址的五大模式

2-12 Translating and Starting a Program
