# 計組 L2
## MIPS ISA:4-Design Principles
- Principle 1: Simplicity favors regularity.
```MIPS
add a, b, c # a gets b + c
↑算術指令一律需要a,b,c三個運算元,keeping hardware simple.
```
- Principle 2: Smaller is faster.
```
Register只放最常用variable,其餘放入memory
```
- Principle 3: Make common case fast.
```
避免使用move:用 add $t2, $s1, $zero 替代
- Small constants are common
- Immediate operand avoids a load instruction
```
- Principle 4: Good design demands a compromise(妥協,折衷)
```
MIPS Representation
- Different formats complicate decoding, but allow 32-bit instructions uniformly
- Keep formats as similar as possible
- 例如,在指令中提供較大的記憶體位置和常數與保持所有指令長度的一致間,所取得的折衷方案(使用三種指令格式而非一種)
```
---
## Register
- 用於儲存運算元,提供快速資料存取
- MIPS有:
-32個32-bit一般目的Register
-32個32-bit浮點數運算暫存器 ($f0-f31)
-數個特殊目的暫存器
**※補充: 32-bit data called a “word”**
- Program Counter(PC): 32-bit特殊目的暫存器
-用於儲存 "下一個要被執行指令"的所在記憶體位置
- Hi , Lo : 兩個32-bit特殊目的暫存器
-用於儲存32-bit數 x 32-bit數乘積,Hi是高位元32-bit,Lo則是低位元32-bit
#### *Spilling Registers程序:
- Compiler將最常用variable放在register,將不常用variable放入memory(利用load&store指令來回傳送)
#### 問題:為什麼不設計多一點register?
- 因為CPU存取register需經解碼程序來指定所要的register,register變大解碼時間也變大,clock cycle time也跟著上升造成CPU Performance降低。
- **Design Principle 2 : Smaller is faster**
---
## Memory
- Memory is byte addressed: 一個位址(address)指到的資料大小為一個位元組(byte)
-**實際多數基本資料存取單位: 1word (=4bytes = 32bits)**
-記憶體位址: 0,1,2...-2^32-1
-字組(word)位址: 0,4,8...-2^32-4
- **Alignment:在MIPS裡所有的Word都要從byte address為4的倍數放起**
-這樣CPU只花一次存取時間,就可從memory讀一個指令or一個字組資料,否則要花2次或以上
- MIPS is **Big Endian** :
-在位元組最左邊or最大位元組(MSB,Most Significant Byte)被放置於最低記憶體位址
-LSB, Little Endian則相反
- **"0x" **表示後續的數為**16進位數**
---
## Data Transfer指令

- lw or sw , offset(base register)
---
## Immediate運算指令
- **No subtract immediate instruction**(沒有subi !!!)
-use negative constant (ex. addi, $s2, $s1, -1)
- **Design Principle 3: Make the common case fast**
-Small constants are common
-Immediate operand avoids a load instruction
- MIPS **register 0 ($zero)** is the **constant 0**
-可以用來將數值在2 registers間移動 (ex. addi $t2, $s1, zero)
:::warning
### 關於數:2's complement Signed INT:

- 最高位 有 (-)負號
- 可用來表示 (-2^n) - 2^n-1 - 1
### Sign Negation: 取補數後, +1

### Sign Extention: 把Sign Bit 向左複製n-bit (Unsigned的話直接填0)

- MIPS裡需要sign extention
-addi: extend immediate value
-lb, lh: extend loaded byte/halfword
-beq, bne: extend the displacement
- **lui(load upper immediate)** 指令能將16位元常數放到32-bit register的左半邊16位元,同時將右半邊16位元清空為0。
-**ori**指令則待lui完,載入15-0 bits
-ex. lui $t0, 1010101010101010
|左16bit|右16bit|
|-|-|
| 1010101010101010 | 0000000000000000 |
:::
---
## 1. MIPS指令表示方法
- Machine code: Instructions are encoded in binary
- **MIPS指令皆為 32-bit words** (1 word = 4bytes = 32bits)
- 暫存器編號:
-$t0 –$t7 are reg’s 8 –15
-$s0 –$s7 are reg’s 16 –23
-$t8 –$t9 are reg’s 24 –25
- 暫存器種類&編號:
|name|for what?|NO.|
|-|-|-|
| $v0 , $v1 | result values | 2-3 |
| $a0 - $a3 | arguments | 4-7 |
| **$t0 - $t7** | temporaries | 8-15 |
| **$s0 – $s7** | saved | 16-23 |
| **$sp**|stack pointer|29|
| **$ra**|return address|31|
### R-format
:::info

- op : operation code
- rs : register 1
- rt : register 2 (R-format)
- rd : register destination
- shamt : shift amount (00000 for now)
- funct : funtion code (extend opcode)
- 例子: 
:::
### I-format
:::info

- rt: register destination (I-format)
- Constant: –215to +215–1
- Address: offset added to base address in rs
- **Design Principle 4:Good design demands good compromises**
-Different formats complicate decoding, but allow 32-bit instructions uniformly
-Keep formats as similar as possible
:::
### J-Format
:::info

:::success
- 直接定址(Direct Addressing)
-j, jal的目標位址為**address x 4**
-jr為R-format

- Jump範圍:

:::
### Logical邏輯指令
:::info
- shamt: 要移幾位
- **MIPS只有"NOR"
-特例:NOT用法 ex. nor $t0, $t1, $zero**
- 
:::
### Flow Control流程指令
:::info
- 指令:

- **IF敘述**:
C: 
MIPS: 
**要先寫bne(Else)部分**
:::
### Conditional狀況(比較大小)指令
:::info

:::
### Procedure Call程序呼叫指令
:::info
- **Jal**: Jump and link
-將下一個指令的位址(即PC+4)儲存到$ra中
- **Jr**: 做Procedure裡程序返回使用
-會跳到$ra所儲存的位址執行
- EX: 呼叫者(caller)將argument放在$a0 - $a3中,使用jal B跳到被呼叫者(callee)程序B, callee執行運算並將結果放在$v0 - $v1, 然後使用jr $ra將控制權還給caller
:::success
- Leaf Procedure Example:


- Non-Leaf Procedure Example (Procedures that call other procedures):
<font color="blue">※ In nested Calls, caller要存以下2點在stack</font>
<font color="blue">※ 1.return address(存在$ra)</font>
<font color="blue">※ 2.在call完之後所需要的所有argument跟temporaries</font>
-
-
:::
### Branch 分支指令
:::info
- <font color="red">分支到哪?</font>

- Branch範圍:-2^15 - 2^15-1(words) or -2^17 - 2^17-4(bytes)
-因為PC包含目前指令的位址,所以要 -1 word或 -4 bytes
-為什麼是15不是16?>>>因為最左邊的bit(第16位)是sign bit
:::
### 例子:Machine Code 綜合
:::warning


:::
### Review:MIPS指令三種format比較
:::info

- R-format: add,sub,sll,srl,jr...
- I-format: lw, sw, addi(rs,rt倒過來), **beq, bne(rs,rt不用倒過來)**...
- J-format: j, jal
:::
---
## 2.MIPS定址模式
#### 定址(addressing): 指令取得運算元或計算目的位址
- 1.立即定址(Immediate Addressing) / **add, ori...**
-運算元包含在 指令本身的16位元constant

- 2.暫存器定址(Register Addressing) / **addi, subi, and, or...**
-運算元在register裡

- 3.基底or位移定址(Base or displacement Addressing) / **lw, sw, lb, sb**
-運算元在memory裡, memory位址計算方式 = base register內容加上指令中16位元constant

- 4.PC相對定址(PC-relative addressing) / **beq, bne**
-Target address為PC+指令中常數

- 5.虛擬直接定址(Pseudodirect addressing) / **j, jal**
-Target address為指令的26bit&PC高位元 結合

---
## 3.綜合應用
### SWAP
:::info


:::
### SORT
:::info



:::
---
## 補充:跑一隻程式時

---
### 2011考古參考答案
1. 5 times
2. G = 0.5
3. G = 17/23
4. G = 1/3
5. Make common case fast, 因為使用immediate可以減少一次記憶體讀取
7. d
8. d
9. d
10. d
11. d
12.
##### tags: `計組` `CO` `L2`