Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2014
COMPUTER ARCHITECTURE
CSE Fall 2014
Faculty of Computer Science and Engineering
Department of Computer Engineering
BK
TP.HCM
Vo Tan Phuong
http://www.cse.hcmut.edu.vn/~vtphuong
dce
2014
Chapter 3
MIPS Instruction Set Architecture
Fall 2014, CS
dce
2014
Presentation Outline
Instruction Set Architecture
Fall 2014, CS
dce
2014
Examples
(Versions)
Introduced in
Intel
1978
MIPS
1986
PowerPC
(601, 604, )
1993
Fall 2014, CS
dce
2014
Accumulator architecture
Accumulator
latch
ALU
registers
address
Memory
latch
Fall 2014, CS
dce
2014
Stack architecture
latch
latch
stack
ALU
latch
Memory
stack pt
push c
c
b
add
pop a
b+c
Fall 2014, CS
dce
2014
Stack
Architecture
Accumulator
Architecture
RegisterMemory
MemoryMemory
Register
(load-store)
Push A
Load A
Load r1,A
Add C,B,A
Load r1,A
Push B
Add
Add
Add
Store C
Pop
r1,B
Store C,r1
Load r2,B
Add
r3,r1,r2
Store C,r3
Fall 2014, CS
dce
2014
Stack
zero operand: all operands implicit (on TOS)
Register-Memory
two operands, one in memory
Memory-Memory
three operands, may be all in memory
Fall 2014, CS
dce
2014
Instructions
Instructions are the language of the machine
We will study the MIPS instruction set architecture
Known as Reduced Instruction Set Computer (RISC)
Elegant and relatively simple design
Similar to RISC architectures developed in mid-1980s and 90s
Very popular, used in many products
Fall 2014, CS
dce
2014
Next . . .
Fall 2014, CS
10
dce
2014
Memory
$0
$1
$2
32 General
Purpose
Registers
Arithmetic &
Logic Unit
$31
ALU
Execution &
Integer Unit
(Main proc)
Integer
mul/div
Hi
FPU
FP
Arith
F0
F1
F2
Floating
Point Unit
(Coproc 1)
F31
Floating-Point
Arithmetic Unit
Lo
TMU
Integer
Multiplier/Divider
32 Floating-Point
Registers
Trap &
BadVaddr
Status Memory Unit
Cause
(Coproc 0)
EPC
Fall 2014, CS
11
dce
2014
$16 = $s0
$1
= $at
$17 = $s1
$2
= $v0
$18 = $s2
$3
= $v1
$19 = $s3
$4
= $a0
$20 = $s4
$5
= $a1
$21 = $s5
$6
= $a2
$22 = $s6
$7
= $a3
$23 = $s7
$8
= $t0
$24 = $t8
$9
= $t1
$25 = $t9
$10 = $t2
$26 = $k0
$11 = $t3
$27 = $k1
$12 = $t4
$28 = $gp
$13 = $t5
$29 = $sp
$14 = $t6
$30 = $fp
$15 = $t7
$31 = $ra
Fall 2014, CS
12
dce
2014
Register
$0
$1
$2 $3
$4 $7
$8 $15
$16 $23
$24 $25
$26 $27
$28
$29
$30
$31
Usage
Always 0
(forced by hardware)
Reserved for assembler use
Result values of a function
Arguments of a function
Temporary Values
Saved registers
(preserved across call)
More temporaries
Reserved for OS kernel
Global pointer
(points to global data)
Stack pointer
Frame pointer
Return address
13
dce
2014
Instruction Formats
Rs5
Rt5
Rd5
sa5
funct6
Immediate (I-Type)
16-bit immediate constant is part in the instruction
Op6
Rs5
Rt5
immediate16
Jump (J-Type)
Used by jump instructions
Op6
immediate26
Fall 2014, CS
14
dce
2014
Instruction Categories
Integer Arithmetic
Arithmetic, logical, and shift instructions
Data Transfer
Load and store instructions that access memory
Data movement and conversions
Miscellaneous
Instructions that transfer control to/from exception handlers
Memory management instructions
Computer Architecture Chapter 3.1
Fall 2014, CS
15
dce
2014
Stack Segment
Stack Grows
Downwards
Memory
Addresses
in Hex
Dynamic Area
Data Segment
0x10000000
Static Area
Text Segment
0x04000000
Reserved
0
Computer Architecture Chapter 3.1
Fall 2014, CS
16
dce
2014
Next . . .
Fall 2014, CS
17
dce
2014
R-Type Format
Op6
Rs5
Rt5
Rd5
sa5
funct6
Fall 2014, CS
18
dce
2014
Instruction
add
addu
sub
subu
Meaning
$s1 = $s2 + $s3
$s1 = $s2 + $s3
$s1 = $s2 $s3
$s1 = $s2 $s3
R-Type Format
op = 0
op = 0
op = 0
op = 0
rs = $s2
rs = $s2
rs = $s2
rs = $s2
rt = $s3
rt = $s3
rt = $s3
rt = $s3
rd = $s1
rd = $s1
rd = $s1
rd = $s1
sa = 0
sa = 0
sa = 0
sa = 0
f = 0x20
f = 0x21
f = 0x22
f = 0x23
Fall 2014, CS
19
dce
2014
Addition/Subtraction Example
# $t0 = g + h
# $t1 = i + j
# f = (g+h)(i+j)
op
sa
func
Fall 2014, CS
20
dce
2014
x y
0
0
1
1
0
0
1
1
0
1
0
1
0
0
0
1
0
1
0
1
x or y
0
1
1
1
x y x xor y
x y x nor y
0
0
1
1
0
0
1
1
0
1
0
1
0
1
1
0
0
1
0
1
1
0
0
0
Fall 2014, CS
21
dce
2014
Instruction
and
or
xor
nor
Meaning
$s1 = $s2 & $s3
$s1 = $s2 | $s3
$s1 = $s2 ^ $s3
$s1 = ~($s2|$s3)
R-Type Format
op = 0
op = 0
op = 0
op = 0
rs = $s2
rs = $s2
rs = $s2
rs = $s2
rt = $s3
rt = $s3
rt = $s3
rt = $s3
rd = $s1
rd = $s1
rd = $s1
rd = $s1
sa = 0
sa = 0
sa = 0
sa = 0
f = 0x24
f = 0x25
f = 0x26
f = 0x27
Examples:
Assume $s1 = 0xabcd1234 and $s2 = 0xffff0000
and $s0,$s1,$s2
# $s0 = 0xabcd0000
or
$s0,$s1,$s2
# $s0 = 0xffff1234
xor $s0,$s1,$s2
# $s0 = 0x54321234
nor $s0,$s1,$s2
# $s0 = 0x0000edcb
Fall 2014, CS
22
dce
2014
Shift Operations
sll
32-bit register
shift-out MSB
srl
shift-in 0
sra
shift-in sign-bit
Computer Architecture Chapter 3.1
...
shift-in 0
...
shift-out LSB
...
shift-out LSB
Fall 2014, CS
23
dce
2014
Shift Instructions
Instruction
sll
srl
sra
sllv
srlv
srav
$s1,$s2,10
$s1,$s2,10
$s1, $s2, 10
$s1,$s2,$s3
$s1,$s2,$s3
$s1,$s2,$s3
Meaning
$s1 = $s2 << 10
$s1 = $s2>>>10
$s1 = $s2 >> 10
$s1 = $s2 << $s3
$s1 = $s2>>>$s3
$s1 = $s2 >> $s3
R-Type Format
op = 0
op = 0
op = 0
op = 0
op = 0
op = 0
rs = 0 rt = $s2
rs = 0 rt = $s2
rs = 0 rt = $s2
rs = $s3 rt = $s2
rs = $s3 rt = $s2
rs = $s3 rt = $s2
rd = $s1
rd = $s1
rd = $s1
rd = $s1
rd = $s1
rd = $s1
sa = 10
sa = 10
sa = 10
sa = 0
sa = 0
sa = 0
f=0
f=2
f=3
f=4
f=6
f=7
$s1,$s2,8
$s1 = $s2<<8
$s1 = 0xcd123400
sra
$s1,$s2,4
$s1 = $s2>>4
$s1 = 0xfabcd123
$s1 = $s2>>>$s3
$s1 = 0x0000abcd
srlv $s1,$s2,$s3
Fall 2014, CS
24
dce
2014
Binary Multiplication
$t0, $s1, 2
; $t0 = $s1 * 4
sll
$t1, $s1, 5
; $t1 = $s1 * 32
; $s2 = $s1 * 36
Fall 2014, CS
25
dce
2014
Your Turn . . .
$t0,
$t1,
$s2,
$t0,
$s2,
$s1,
$s1,
$t0,
$s1,
$s2,
1
3
$t1
4
$t0
;
;
;
;
;
$t0
$t1
$s2
$t0
$s2
=
=
=
=
=
$s1
$s1
$s1
$s1
$s1
*
*
*
*
*
2
8
10
16
26
; $s2 = $s1 * 32
; $s2 = $s1 * 31
Fall 2014, CS
26
dce
2014
Next . . .
Fall 2014, CS
27
dce
2014
I-Type Format
Rs5
Rt5
immediate16
# $s1 = $s2 + 5
# $s1 = $s2 | 5
Fall 2014, CS
28
dce
2014
Instruction
addi
addiu
andi
ori
xori
lui
$s1, $s2, 10
$s1, $s2, 10
$s1, $s2, 10
$s1, $s2, 10
$s1, $s2, 10
$s1, 10
Meaning
$s1 = $s2 + 10
$s1 = $s2 + 10
$s1 = $s2 & 10
$s1 = $s2 | 10
$s1 = $s2 ^ 10
$s1 = 10 << 16
I-Type Format
op = 0x8
op = 0x9
op = 0xc
op = 0xd
op = 0xe
op = 0xf
rs = $s2
rs = $s2
rs = $s2
rs = $s2
rs = $s2
0
rt = $s1
rt = $s1
rt = $s1
rt = $s1
rt = $s1
rt = $s1
imm16 = 10
imm16 = 10
imm16 = 10
imm16 = 10
imm16 = 10
imm16 = 10
Fall 2014, CS
29
dce
2014
translated as
addiu $s0,$s1,5
C = B1;
translated as
addiu $s2,$s1,-1
imm = -1 = 1111111111111111
A = B&0xf; translated as
andi
$s0,$s1,0xf
C = B|0xf; translated as
ori
$s2,$s1,0xf
C = 5;
translated as
ori
$s2,$zero,5
A = B;
translated as
ori
$s0,$s1,0
Fall 2014, CS
30
dce
2014
32-bit Constants
Rs5
Rt5
immediate16
load upper
16 bits
clear lower
16 bits
lui $s1,0xAC51
$s1=$17 0xAC51
0x0000
ori $s1,$s1,0x65D9
$s1=$17 0xAC51
0x65D9
Fall 2014, CS
31
dce
2014
Next . . .
Fall 2014, CS
32
dce
2014
J-Type Format
Op6
immediate26
PC4
immediate26
00
least-significant
2 bits are 00
Fall 2014, CS
33
dce
2014
bltz Rs,label
branch to label if (Rs
bgtz Rs,label
branch to label if (Rs
blez Rs,label
branch to label if (Rs
bgez Rs,label
branch to label if (Rs
No need for beqz and bnez instructions. Why?
< 0)
> 0)
<= 0)
>= 0)
Fall 2014, CS
34
dce
2014
Fall 2014, CS
35
dce
2014
How to implement:
Solution:
blt
slt
bne
ble
slt
beq
$s0,$s1,label
$at,$s0,$s1
$at,$zero,label
$s2,$s3,label
$at,$s3,$s2
$at,$zero,label
Fall 2014, CS
36
dce
2014
Pseudo-Instructions
li
$s1, 0xabcd1234
sgt
blt
Fall 2014, CS
37
dce
2014
Instruction
j
beq
bne
blez
bgtz
bltz
bgez
label
rs, rt, label
rs, rt, label
rs, label
rs, label
rs, label
rs, label
Instruction
slt
sltu
slti
sltiu
Meaning
jump to label
branch if (rs == rt)
branch if (rs != rt)
branch if (rs<=0)
branch if (rs > 0)
branch if (rs < 0)
branch if (rs>=0)
Format
op6 = 2
op6 = 4
op6 = 5
op6 = 6
op6 = 7
op6 = 1
op6 = 1
imm26
rs5
rs5
rs5
rs5
rs5
rs5
Meaning
rd=(rs<rt?1:0)
rd, rs, rt
rd=(rs<rt?1:0)
rd, rs, rt
rt, rs, imm16 rt=(rs<imm?1:0)
rt, rs, imm16 rt=(rs<imm?1:0)
rt5
rt5
0
0
0
1
imm16
imm16
imm16
imm16
imm16
imm16
Format
op6 = 0
op6 = 0
0xa
0xb
rs5
rs5
rs5
rs5
rt5
rt5
rt5
rt5
rd5
rd5
0
0x2a
0
0x2b
imm16
imm16
Fall 2014, CS
38
dce
2014
Next . . .
Fall 2014, CS
39
dce
2014
Translating an IF Statement
Consider the following IF statement:
if (a == b) c = d + e;
else c = d e;
Assume that a, b, c, d, e are in $s0, , $s4 respectively
else:
exit:
bne
addu
j
subu
. . .
Fall 2014, CS
40
dce
2014
Fall 2014, CS
41
dce
2014
...
# skip if false
# skip if false
# both are true
Fall 2014, CS
42
dce
2014
Fall 2014, CS
43
dce
2014
Your Turn . . .
Fall 2014, CS
44
dce
2014
Next . . .
Fall 2014, CS
45
dce
2014
load
Memory
Registers
store
Store Instruction:
Transfers data from a register to memory
Fall 2014, CS
46
dce
2014
# Rt MEMORY[Rs+imm16]
# Rt MEMORY[Rs+imm16]
Rs5
Rt5
immediate16
Memory Word
Base address
Fall 2014, CS
47
dce
2014
Load Instruction
Load/Store Instruction Format (I format):
lw $t0, 24($s3)
35
19
2410
Memory
2410 + $s3 =
. . . 0001 1000
+ . . . 1001 0100
. . . 1010 1100 =
0x120040ac
0xf f f f f f f f
0x120040ac
$t0
0x12004094
$s3
data
Computer Architecture Chapter 3.1
0x0000000c
0x00000008
0x00000004
0x00000000
word address (hex)
Fall 2014, CS
48
dce
2014
Byte Addresses
Since 8-bit bytes are so useful, most architectures
address individual bytes in memory
Alignment restriction - the memory address of a word must be on
natural word boundaries (a multiple of 4 in MIPS-32)
Big Endian:
leftmost byte is word address
IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA
Little Endian:
rightmost byte is word address
Intel 80x86, DEC Vax, DEC Alpha (Windows NT)
3
msb
0
big endian byte 0
lsb
1
Fall 2014, CS
49
dce
2014
$s1, 8($s0)
# $s1 = A[2]
addiu
$s2, $s1, 5
# $s2 = A[2] + 5
sw
$s2, 4($s0)
# A[1] = $s2
Registers
...
...
$s0 = $16 address of A
A[2] + 5
...
lw
sw
A[3]
A[2]
A[1]
A[0]
A+12
A+8
A+4
A
...
Fall 2014, CS
50
dce
2014
sign extend
s s
zero extend
bu
sign extend
s s
zero extend
hu
Fall 2014, CS
51
dce
2014
Instruction
lb
lh
lw
lbu
lhu
sb
sh
sw
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
Meaning
I-Type Format
rt = MEM[rs+imm16]
rt = MEM[rs+imm16]
rt = MEM[rs+imm16]
rt = MEM[rs+imm16]
rt = MEM[rs+imm16]
MEM[rs+imm16] = rt
MEM[rs+imm16] = rt
MEM[rs+imm16] = rt
0x20
0x21
0x23
0x24
0x25
0x28
0x29
0x2b
rs5
rs5
rs5
rs5
rs5
rs5
rs5
rs5
rt5
rt5
rt5
rt5
rt5
rt5
rt5
rt5
imm16
imm16
imm16
imm16
imm16
imm16
imm16
imm16
If Immediate16 = 0 then
Fall 2014, CS
52
dce
2014
Next . . .
Fall 2014, CS
53
dce
2014
$s1,
$t0,
$t1,
$t1,
$s1,
$t0,
$t0,
loop
$s1, $s1
$s0
0($t0)
$s2, exit
$s1, 1
$s1, 2
$s0, $t0
#
#
#
#
#
#
#
Memory
...
A[i]
A+4i
...
A[2]
A+8
A[1]
A[0]
A+4
A
...
i = 0
$t0 = address A
$t1 = A[i]
exit if (A[i]== k)
i = i+1
$t0 = 4*i
$t0 = address A[i]
Fall 2014, CS
54
dce
2014
Fall 2014, CS
55
dce
2014
Copying a String
$t0,
$t1,
$t2,
$t2,
$t0,
$t1,
$t2,
$s0
$s1
0($t0)
0($t1)
$t0, 1
$t1, 1
$zero, L1
#
#
#
#
#
#
#
56
dce
2014
sum = 0;
for (i=0; i<n; i++) sum = sum + A[i];
$t0,
$t1,
$s2,
$t2,
$s2,
$t0,
$t1,
$t1,
$s0
$t1, $t1
$s2, $s2
0($t0)
$s2, $t2
$t0, 4
$t1, 1
$s1, L1
#
#
#
#
#
#
#
#
57
dce
2014
Next . . .
Fall 2014, CS
58
dce
2014
Addressing Modes
Where are the operands?
How memory addresses are computed?
Immediate Addressing
Op6
Rs5
Rt5
immediate16
Operand is a constant
Register Addressing
Op6
Rs5
Rt5
Rd5
sa5
Operand is in a register
funct6
Register
Rs5
Rt5
immediate16
Byte
Halfword
Word
Fall 2014, CS
59
dce
2014
PC-Relative Addressing
Op6
Rs5
Rt5
immediate16
+1
PC30
00
PC30 + immediate16 + 1
immediate26
:
PC4
00
Pseudo-direct Addressing
Op6
PC26
00
PC4
immediate26
00
Fall 2014, CS
60
dce
2014
PC4
immediate26
00
PC30 + immediate16 + 1
00
Fall 2014, CS
61
dce
2014
Fall 2014, CS
62
dce
2014
Fall 2014, CS
63