Documente Academic
Documente Profesional
Documente Cultură
Content
Introduction
Programming structures
MIPS operands
MIPS instruction set
Branching
Procedure call
Practice
MIPS simulator
Writing program for MIPS
Introduction
add a, b, c
Operation performed
add b and c,
then store result into a
add a, b, c
operation
CO&ISA, NLT 2013
operands
3
Introduction
add t0, g, h
add t1, i, j
sub f, t0, t1
# t0 = g + h
# t1 = i + j
# f = t0 - t1
Equivalent C code
f = (g + h) (i + j)
Operands
Object of operation
MIPS operands
Registers
Memory locations
Constant/Immediate
Byte =Byte
8 bits
Halfword= 2 bytes
Halfword
Word =Word
4 bytes
Doubleword
= 8 bytes
Doubleword
Register File
32 bits
src1 addr
src2 addr
dst addr
write data
32 src1
data
5
5
32
32
locations
32 src2
data
write control
Register
Number
$zero
0
$at
1
$v0 - $v1
2-3
$a0 - $a3
4-7
$t0 - $t7
8-15
$s0 - $s7
16-23
$t8 - $t9
24-25
$gp
28
$sp
29
$fp
30
$ra
31
CO&ISA, NLT 2013
Usage
Preserve
on call?
constant 0 (hardware)
n.a.
reserved for assembler
n.a.
returned values
no
arguments
yes
temporaries
no
saved values
yes
temporaries
no
global pointer
yes
stack pointer
yes
frame pointer
yes
return addr (hardware)
yes
10
MIPS: load/store
machine.
Typical operation
11
Memory operand
Large size
Outsize CPU Slower than register
12
0x0f
0x0e
0x0d
Word 3
Byte addressable
0x0c
0x0b
0x0a
0x09
Word 2
0x08
0x07
0x06
0x05
Word 1
0x04
0x03
0x02
0x01
Word 0
Alignment
In decimal:
Word address = 4 * word
number
In binary:
Word address
= word number + 00
0x00
13
Byte Addresses
Big Endian:
Little Endian:
0
lsb
(least significant byte)
msb
(most significan byte)
0
1
2
big endian order
14
Example
value
X+3
68
X+2
1B
X+1
5D
FA
15
Immediate operand
16
Instruction set
3 instruction formats:
Register (R)
Immediate (I)
Branch (J)
17
5 instruction types
Conditional branch
Unconditional branch
18
19
rd, rs, rt
#rd rs + rt
sub
rd, rs, rt
#rd rs rt
#rd rs + const
rs
5-bits
rt
5-bits
rd
5-bits
20
Example
Currently $s1 = 6
addi
$s2, $s1, 3
addi
$s1, $s1, -2
sub
21
and
rd, rs, rt
#rd rs & rt
or
rd, rs, rt
#rd rs | rt
ori
#rd rs | const
nor
rd, rs, rt
and
or
22
Logical shift and arithmetic shift: move all the bits left or
right
sll
srl
sra
23
sw $t0, 8($s3)
24
. 0001
+ . 1001
. 1010
= 0x1200
Memory
0xf f f f f f f f
1000 (24)
$t0
0100 (94)
24
1100 (ac) $s3
40ac
0x120040ac
0x12004094
0x0000000c
0x00000008
0x00000004
0x00000000
word address (hex)
25
Ex:
Exit :
if (i==j)
h = i + j;
26
Example
start:
Exit:
addi
s0, zero, 1
addi
s1, zero, 2
addi
s3, zero, 0
beq
add
s3, s2, s1
add
s2, s3, s1
.end start
27
then
else
228
Unconditional branch
label
#go to label
30
Example
Solution
slti
beq
addi
j
else: addi
endif:...
$t0,$s1,5
$t0,$zero,else
$t1,$zero,3
endif
$t1,$zero,10
#
#
#
#
#
31
rs
rt
op
rs
rt
op
rd
sa
funct
immediate
jump target
R format
I format
J format
32
R-format instruction
op
rs
rt
rd
shamt
funct
op
6-bits
rs
5-bits
rt
5-bits
rd
5-bits
shamt 5-bits
funct
6-bits
33
add
sub
op
source2
0x22
17
18
34
Example
lw
$t0,0($s1)
addi
add
$t1,$t1,1
# increment index i by 1
35
#$t0 = 1 if $s2<15
18
0x0F
36
0x0d
Stored program in
memory
1 instruction = 1 word
Instruction cycle
Instruction 3
0x0c
0x0b
0x0a
0x09
Instruction 2
0x08
0x07
0x06
0x05
Instruction 1
Fetch
Decode
Execution
0x04
0x03
0x02
0x01
Instruction 0
0x00
37
CPU
3000
Inst. i+2
3008
Inst. i+1
3004
Inst. i
3000
PC
Before fetching instruction i
CO&ISA, NLT 2013
memory
CPU
3004
Inst. i+2
3008
Inst. i+1
3004
Inst. i
3000
PC
After fetching instruction i
38
Example
The simple while loop: while (A[i]==k) i=i+1;
$t1,$s1,$s1
# t1 = 4*i
add
$t1,$t1,$t1
add
$t1,$t1,$s2
# t1 = A + 4*I,
address of A[i]
lw
$t0,0($t1)
bne
$t0,$s3,endwhl
addi $s1,$s1,1
endwhl:
CO&ISA, NLT 2013
loop
#
39
Example
The simple switch
switch(test) {
case 0:
a=a+1; break;
case 1:
a=a-1; break;
case 2:
b=2*b; break;
default:
}
Solution
beq
beq
beq
b
case_0:
addi
b
case_1:
sub
b
case_2:
add
b
default:
continue:
s1,t0,case_0
s1,t1,case_1
s1,t2,case_2
default
s2,s2,1
continue
#a=a+1
s2,s2,t1
continue
#a=a-1
s3,s3,s3
continue
#b=2*b
40
Example
loop:
add
s1,s1,1
#i=i+step
add
t1,s1,s1
#t1=2*s1
add
t1,t1,t1
#t1=4*s1
add
t1,t1,s2
lw
t0,0(t1)
add
s5,s5,t0
#sum = sum+A[i]
bne
s1,s3,loop
41
Exercise
$t0,$zero,else
addi $t1,$zero,3
j
endif
42
ProcedureAddress
26 bit address
$ra
#return
31
0x08
43
2.
3.
4.
5.
6.
44
jal
PC
proc
Prepare
to call
Prepare
to continue
proc
Save, etc.
Restore
jr
$ra
45
PC
jal
abc
Prepare
to call
Prepare
to continue
abc
Procedure
abc
Save
xyz
jal
Procedure
xyz
xyz
Restore
jr
$ra
jr
$ra
46
MIPS Organization
Processor
Memory
Register File
src1 addr
src2 addr
5
dst addr
write data
11100
src1
data
32
32
registers
($zero - $ra)
read/write
addr
src2
32 data
32
32
32 bits
branch offset
32
Fetch
PC = PC+4
Exec
32 Add
PC
32 Add
4
read data
32
32
32
write data
32
Decode
230
words
32
32 ALU
32
32
4
0
5
1
6
2
32 bits
7
3
01100
01000
00100
00000
word address
(binary)
byte address
(big Endian)
47
Summary
Can it be implemented?
Can it be programmed?
Which CPU is suitable?
Metric of performance
49