Lecture 2: MIPS Instruction Set
Lecture 2: MIPS Instruction Set
• Today’s topic:
MIPS instructions
1
Recap
2
Instruction Set
3
Instruction Set
4
A Basic MIPS Instruction
C code: a=b+c;
C code a = b + c + d + e;
translates into the following assembly code:
add a, b, c add a, b, c
add a, a, d or add f, d, e
add a, a, e add a, a, f
C code f = (g + h) – (i + j);
7
Subtract Example
C code f = (g + h) – (i + j);
translates into the following assembly code:
8
Operands
10
Memory Operands
int a, b, c, d[10]
…
Memory
Base address
12
Immediate Operands
destination register
source address
lw $t0, 8($t3)
any register
a constant that is added to the register in brackets
14
Example
Convert to assembly:
15
Example
Convert to assembly:
• Decimal 3510
• Binary 001000112
17
Instruction Formats
19
Control Instructions
• Unconditional branch:
j L1
jr $s0
Convert to assembly:
if (i == j)
f = g+h;
else
f = g-h;
20
Control Instructions
• Unconditional branch:
j L1
jr $s0
Convert to assembly:
if (i == j) bne $s3, $s4, Else
f = g+h; add $s0, $s1, $s2
else j Exit
f = g-h; Else: sub $s0, $s1, $s2
21
Exit:
Example
Convert to assembly:
while (save[i] == k)
i += 1;
22
Example
Convert to assembly:
Loop: sll $t1, $s3, 2
while (save[i] == k) add $t1, $t1, $s6
i += 1; lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
i and k are in $s3 and $s5 and j Loop
base of array save[] is in $s6 Exit:
23
Title
• Bullet
24