memory operands
- memory is byte addressed
- identifies an 8-bit byte
- words are aligned in memory
- address must be multiple of 4
- MIPS is Big Endian
- most-significant byte at least address
- eg:
- a word: A0 B1 C2 23
- store in memory increasing:
- A0 B1 C2 23
data transfer operations
- C code:
g = h + A[8]
MIPS code:
lw $t0, 32($s3) add $s1,$s2, $t0
C code:
A[12] = h + A[8]
MIPS code:
lw $t0, 32($s3) add $t0,$s2, $t0 sw $t0, 48($s3)
- define address:
- g -> $s1
- h -> $s2
- A -> $s3
- define address:
register vs memory
- register are faster to access than memory
- 所以要安排好,常用的盡量長時間放在register
immediate operands
- addi
- 可直接用const value
- 用法:
addi $s3, $s3 , 4
the constant zero
- MIPS register 0 ($zero) is the constant 0
- 不能寫入
- 已寫死是0
- useful command
- 當作move
add $t2, $s1, $zero
- 當作move
representing instruction
- machine code
- instruction are encoded in binary
MIPS R-format instruction
- instruction fields
- op:
- 6bits
- operation code (opcode)
- rs:
- 5bits
- first source register number
- rt:
- 5bits
- rd:
- 5bits
- shamt:
- 5bits
- funct:
- 6bits
- op:
eg:
add $t0, $s1, $s2
- 數學運算 的opcode => 0
- rs => $s1
- rt => $s2
- rd => $t0
- shamt => 0
- funct => 32 (add)
- 數學運算 的opcode
MIPS I-format instruction
- instruction fields
- op:
- 6bits
- rs:
- 5bits
- rt:
- 5bits
- constant or address:
- 16bits
- op:
- 會有兩個format的原因
- principle 3
eg:
lw $t0 ,1200($t1)
- op => 35
- rs => $t1
- rt => $t0
- address
MIPS logical operator
- shift operator
- shift left (sll)
- shift left, fill 0 bits
- 意義:×2
- shift right (srl)
- shift right, fill 0 bits
- 意義:/2
- 有另一種指令sla,sra
- 會根據+-號補上1||0
- shift left (sll)
and operator
operator:
and $t0 $t1 $t2
- t1,t2 做and, 存入t0
useful to mask bits in a word
- select some bits, clear other to 0
or operator
- operator:
or $t0 $t1 $t2
- 跟and的方式一樣
- useful to include bits in a word
- select some bits to 1, leave other unchanged
- operator:
- not operator
- MIPS does not have not operator
- operator:
nor $t0 $t1 $zero
- 利用nor operator 和 $zero 來做not
- useful to invert bit in a word
flow control operation
- beq
- 例子
beq $s0 $s1 L1
- 意思:
if (s0==s1) jump L1
- 例子
- bne
- 例子:
bne $s0 $s1 L2
- 意思:
if(s0!=s1) jump L2
- 例子:
- j
- 就是jump
compiling loop statements
- c/c++:
while(save[i]==k)i+=1;
- MIPS code:
Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: ...
more conditional operation
- set result to 1 if a condition is true; otherwise,set to 0
- operator:
slt rd, rs, rt slti rt, rs, const sltu sltui
- meaning
if(rs<rt)rd = 1;else rd= 0 if(rs<const) rt = 1, else rt = 0 Unsigned operators
- 不做blt的原因:
- 硬體比較難做,而且成品可能會拖垮其他的指令的速度
- blt 實做
procedure call instruction
- jal
- 直接跳
- 會記下下一行的address在$ra
- jr
- 跳回$ra
Byte/Halfword operations
- could use bitwise operations
- code:
lb rt, offset(rs) lh rt, offset(rs) lbu lhu
- load 一個byte
- load 兩個byte
- unsigned operator
jump addressing
- j format
- op code
- 6bits
- address
- word
- 26 bits
- 所儲存的address是原始address/4
- byte => word
- op code
branching far away
- beq
- 可以跳+-128k的位置
- j
- 可跳256MB
assembler pseudo instruction
- 可以提供假的指令
- 如 move
fallacies
- powerful instruction => higher performance
- 不一定
- 可能跑比較久
- 可能會拖慢其他的instruction
- use assembly code for high performance
- 除非寫assembly 寫得比較好
- 現在的compiler都很好
- backward compatible => instruction set doesn't change
- 會增加新的instruction
chapter 3 arithmetic for computers
problem of ripple carry adder
- 下一個bit要等上一個bit運算完才能執行
IEEE floating point format
- s
- sign bit
- 0: non-negative
- 1: negative
- exponent
- single: 8bits
- double: 11 bits
- fraction