2015年9月17日 星期四

Chapter2 Instructions: Language of the Computer

Chapter2 Instructions: Language of the Computer



There are four design princinple in MIPS


1. Simplicity favors regularity.
2. Smaller is faster.
3. Good design demands good compromises.
4. Make the common case fast.

simplicity favors regularity


e.g.
C code:
A = B + C + D;
E = F - A;
MIPS code:
add $t0, $s1, $s2    # $t0 = $s1 + $s2, put result “temporarily” in $t0
add $s0, $t0, $s3    # $s0 = $t0 + $s3, now we can use the “temporary” result from $t0
sub $s4, $s5, $s0   # $s4 = $s5 - $s0


--->加減乘除必須遵照R-format  
--->Operands must be registers — only 32 registers provided  (MIPS arithmethic)


smaller is faster


Clock cycle faster vs. More registers  


The amount of time it takes to get a value from a register into the ALU, or from the ALU into a register, is proportional to the exponent of 2. That is, the time for 32 registers is twice the time of 16 registers and 1/2 the time of 64 registers.
總的來說,發現32個register是比較適當的


----->
那如果暫存器不夠用呢?


ans:
Must move values between memory and registers


Load and Store instruction

C or Java code:
A[8] = h + A[8];
MIPS code:
la $t3, A                       # load address of start of array A
lw $t0, 32($t3)             # load contents of A[8]
add $t0, $s2, $t0
sw $t0, 32($t3)


從這兩個準則可以發現
MIPS的兩大特性
• loading words but addressing bytes
• arithmetic performed on registers only


Machine languague


R-format


   6                       5               5               5                5                  6    bit


• op — basic operation of the instruction, the opcode
• rs — first register source operand
• rt — second register source operand
• rd — register destination operand, it gets the result
• shamt — shift amount (e.g sll $d, $t, h  note: t*2^h )
• funct — Function. Selects the specific variant of the opcode.


I-format




------->
Good design demands good compromises.


Where’s the compromise?
Keep all instructions at 32 bits Offset limited to 16-bits, ± 32K


Immediate Operands


1.Constant data specified in an instruction
 addi $s3, $s3, 4
2.
No subtract immediate instruction
Just use a negative constant
 addi $s2, $s1, -1


Small constants are common
Immediate operand avoids a load instruction


--->  Make the common case fast


j-format


e.g.
J -- Jump
JAL -- Jump and link

介紹完四大原則,這邊介紹一些常用到的指令與標示符號和格式


Unsigned Binary integers


Range: 0 to +2^n  – 1


2s-Complement Signed Integers


Range: –2^(n – 1)  to +2^(n – 1)  – 1


二元數與二的補數轉換


Logical Operations


shift operation
R-type
Shift left logical
Shift left and fill with 0 bits
sll by i bits multiplies by 2^i
Shift right logical
Shift right and fill with 0 bits
srl by i bits divides by 2 i  (unsigned only)


AND OR NOT operation
這三種指令基本上大同小異


nor $t0, $t1 $zero
Condition operation
Branch to a labeled instruction if a  condition is true. Otherwise, continue sequentially


beq rs, rt, L1
if (rs == rt) branch to instruction labeled L1;
bne rs, rt, L1
if (rs != rt) branch to instruction labeled L1;
j L1
unconditional jump to instruction labeled L1


Basic Block
A basic block is a sequence of instructions
with
No embedded branches (except at end)
No branch targets (except at beginning)
note: compiler 可以對一個block進行優化

Branch Instruction Design


why MIPS don’t use bge or blt ?


--->Hardware for <, ≥, … slower than =, ≠
--->beq and bne are the common case This is a good design compromise




Procedure Calling


Steps required
1. Place parameters in registers
2. Transfer control to procedure
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call


有兩種procedure


leaf-procedure  
function不會在call subroutine所以callee不需要push return adress到stack裡
e.g


Non-leaf procedure


步驟:
For nested call, caller needs to save on the
stack:
Its return address
Any arguments and temporaries needed after
the call
Restore from the stack after the call


e.g.
新增說明文字





-->return adress是情況,要自己push進stack裡



-->callee若需要要自己push進stack裡





Byte/Halfword Operations


lb rt, offset(rs)     lh rt, offset(rs)
Sign extend to 32 bits in rt   e.g.
00 1010         --->0000 0000 0000 1010
11 1111 0001 --->1111 1111 1111 0001
lbu rt, offset(rs)    lhu rt, offset(rs)
Zero extend to 32 bits in rt


sb rt, offset(rs)     sh rt, offset(rs)
Store just rightmost byte/halfword (解題目時,要記得MIPS是big-endian~~)
暫存器最右邊的寫入MSB




載入上半部,且右半部清為零


載入下半部


Branch addressing




forward or backward


PC-relative addressing
Target address = PC + offset*4
PC already incremented by 4 by this time


e.g.






參考資料:


交大吳凱強 計算機組織上課講義


對於MIPS的四大原則有很好的例子


lb sb 之題目




額外補充之big-endian與little-endian


int到底有幾個btyes呢?


某op學校講義(英文版  還未看完)


wiki sign extension

沒有留言:

張貼留言