Chap3 MIPS Part2
Chap3 MIPS Part2
MIPS – 32 BITS
Data transfer
• RAM access only allow load/ store instructions
• Load word has destination first, store has destination last
2
Data transfer operations
Syntax (I-format): op rt,(constant/address)rs
op: operation code
rs: base address
rt: destination/ source register number
Constant: -215 215 – 1
Address: offset added to base address in rs (offset is always a multiple of 4)
3
Data transfer operations
lw rt, offset(rs) # load a word at the address
$s0 + 12 from the memory
sw rt, offset(rs) # store a word to the memory at
the location $s0 + 12
4
Example
• Suppose that A is an array of 100 words with the starting address (base
address) contained in register $s0.
• The value of the variable g are stored in registers $s1 and $s2 respectively
C code: g = A[2];
MIPS code:
lw $t0, 8($s0) #load A[2] to the register $t0
add $s1, $t0,$zero #save the value of A[2] to g
5
Example
C code: A[12] = h + A[8];
MIPS code:
lw $t0, 32($s0) #load A[8] to the register $t0
add $t0, $s2,$t0 #temporary register $t0=h +
A[8]
sw $t0, 48 ($s0) #store h + A[8] back to A[12]
6
Discussions
• Load a byte xzzz zzzz from memory to a register in CPU. (Useful for ASCII)
lb $t0, g
What is the value of this register (x is the MSB of the byte) ?
Sing-extended: xxxx xxxx xxxx xxxx xxxx xxxx xzzz zzzz
• If you want the remaining bits from the right to have a zero value
(unsigned number)
Load byte unsigned: lbu $t0, g
7
Discussions
Load/ store a half of word (useful for Unicode)
Load halft: lh $t0, g #load ½ word – the 2 right most bytes
Store half: sh $t0, g #store ½ word – the 2 right most
bytes
8
Branch operations:
• Two kinds of branch instructions:
o Conditional
o Unconditional
• MIPS branch destination address = PC + (4 * offset)
o Target address = PC + offset × 4
o PC already plus 4 bytes by this time
9
Branch operations: Conditional branch
Syntax (I-format): op rs , rt , target address
rs: the first source register number
rt: the second source register number
Target Address: the address of the next instruction
beq rs, rt, label #if (opr1 == opr2) goto label
bne rs, rt, label #if (opr1 != opr2) goto label
10
Example
C Code: MIPS code:
if(a==b) #a $t0, b $t1, i $t2
i = 1; beq $t0, $t1, Label
else addi $t2, $zero, 0
i = 0; Label
addi $t2, $zero, 1
11
Branch operations: Unconditional branch
Syntax (J-format): op Target Address
rs: the first source register number
rt: the second source register number
Target Address: the address of the next instruction
j label #goto label
12
Example
C Code: MIPS code:
Do{ #a $t0
Task 1; Loop:
}while(a!=0) Task 1
Task 2 bne $t0, $zero, Loop
j Task 2
13
Discussion
• Bigger/ smaller comparison ?
MIPS support slt instruction to make the solutions for not equal comparison
Syntax: slt rd, rs, rt
Explain: if (rs < rt)
rd = 1
else
rd = 0
14
Example
a < b
slt $t0, $s0, $s1 # if (a < b) then $t0 = 1
bne $t0, $0, Label # if (a < b) then goto Label
Another Task # else then do something
Label
a > b
slt $t0, $s1, $s0 # if (b < a) then $t0 = 1
bne $t0, $0, Label # if (b < a) then goto Label
Another Task # else then do something
Label
Another Task # else then do something
15
Example
a b
slt $t0, $s0, $s1 # if (a < b) then $t0 = 1
beq $t0, $0, Label # if (a b) then goto Label
Another Task # else then do something
Label
a b
slt $t0, $s1, $s0 # if (b < a) then $t0 = 1
beq $t0, $0, Label # if (b a) then goto Label
Another Task # else then do something
Label
16
Discussion
Compare with constant?
MIPS support slti instruction to make the solutions for not equal comparison
Syntax: slti rd, rs, constant
Explain: if (rs < constant)
rd = 1
else
rd = 0
17
Example: Switch...case in C
C code: C code: Convert to if...else
switch (k) { if (k == 0)
case 0: f = i + j; f = i + j;
break; else if (k == 1)
case 1: f = g + h; f = g + h;
break;
else if (k == 2)
case 2: f = g - h;
break; f = g – h;
} f g h i j k
$s0 $s1 $s2 $s3 $s4 $s5
18
Example: Switch..case in MIPS
bne $s5, $0, L1 # if (k != 0) then goto L1
Add $s0, $s3, $s4 # else (k == 0) then f = i + j
J Exit # end of case Exit (break)
L1:
addi $t0, $s5, -1 # $t0 = k – 1
bne $t0, $0, L2 # if (k != 1) then goto L2
add $s0, $s1, $s2 # else (k == 1) then f = g+ h
J Exit # end of case Exit (break)
L2:
addi $t0, $s5, -2 # $t0 = k – 2
bne $t0, $0, Exit # if (k != 2) then goto Exit
sub $s0, $s1, $s2 # else (k == 2) then f = g - h
Exit:
19
System calls
• Through the system call (syscall) instruction to request a service from
a small set of the operating system services
• System call code $v0
• Arguments $a0 - $a3 ($f12 for floating-point)
• Return value $v0 ($f0 for floating-point)
20
System
calls
21
Example
addi $v0, $0, 4# $v0 = 0 + 4 = 4
22
23