Chapter 9 How to convert basic C code to MIPS assembly language
Chapter 9 How to convert basic C code to MIPS assembly language
Outline
• If
• If – else
• Nested If
• Switch case
• Do while
• While
• For
• Nested Loop
1
12/4/2023
Basic strategy
Writing correct assembler directly is hard.
Recommended strategy:
1. Solve a problem by using high-level languages like C, Java.
2. Map high-level program down to friendly pseudo code (or “simplified C”).
3. Translate this pseudo code to MIPS instructions.
2
12/4/2023
Example
• Write the branch-friendly pseudo code
if ($t0 == $t1)
{
$t0 += 1;
$s0 += $t0;
}
3
12/4/2023
Solution
Example
• Write the equivalent MIPS assembly code
if ($t0 == 25)
{
$t0 = $t0 + 10;
}
4
12/4/2023
Example
• First, the branch-friendly
• MIPS assembly code
pseudo code
li $t1, 25
if ($t0 != 25)
bne $t0, $t1, END_IF
goto END_IF
addi $t0, $t0, 10
$t0 = $t0 + 10;
END_IF:
END_IF:
……# remainder of the program
……..
ble instruction:
# high-level language pseudo code
if ($t0 > $t1)
$t0 = $t0 – 1;
………
5
12/4/2023
bge instruction:
# high-level language pseudo code
if ($t0 < $t1)
$t0 = $t0 – 1;
blt instruction
# high-level language pseudo code
if ($t0 >= $t1)
$t0 = $t0 – 1;
6
12/4/2023
bgt instruction
# high-level language pseudo code
if ($t0 <= $t1)
$t0 = $t0 – 1;
7
12/4/2023
Exercises
• Write branch-friendly pseudo code and MIPS assembly for each:
if($t0 > $t1)
$t0 = $t0 + 1;
8
12/4/2023
9
12/4/2023
Exercises
• Write branch-friendly pseudo code and MIPS assembly for each:
if($t0 >= 0)
$t0 = $t0 + 1;
if($t0 < 0)
$t0 = $t0 + 1;
10
12/4/2023
Standard C Simplified C
if (i < 0) { if (i >= 0) goto else1;
n = n - i; n = n - i;
goto end1;
} else { else1:
n = n + i; n = n + i;
} end1:
11
12/4/2023
Simplified C MIPS
# assume i in $t0
# assume n in $t1
12
12/4/2023
Exercise – Part 1
• Write the MIPS branch-friendly pseudo code for this if-then-else statement.
Exercise – Part 2
• Write the equivalent MIPS assembly code
if ($t0 < 0) goto ELSE;
$s0 += 1;
$s1 += $s0;
goto END_ELSE;
ELSE:
$s0 -= 1;
$s2 += $s0;
END_ELSE
13
12/4/2023
Exercise
• Write MIPS assembly code for:
if ($t0 == 0)
$t0 = $t0 + 3;
else {
if($t0 == 1)
$t0 = $t0 + 2;
else
$t0 = $t0 + 1;
}
Exercise: Solution
bnez $t0, else1
addi $t0, $t0, 3
j endif
else1:
li $t2, 1 # must load constant 1 in a register
bne $t0, $t2, else2
addi $t0, $t0, 2
j endif
else2:
$t0 = $t0 + 1;
endif:
14
12/4/2023
Exercise
• Write MIPS code for this C/C++/Java…
if ($t0 > 0)
{
if ($t0 < $t1)
$t0 += 1;
else
$t0 += 2;
}
Switch statement
• #high-level language
int c= 2;
int x = 0;
switch(c)
{
case 0: x= x+1; break;
case 1: x=x+2; break;
Case 2: x=x+3; break;
}
Translate this code to MIPS assembly code ?
15
12/4/2023
Method 1
16
12/4/2023
.data
JUMP_TABLE: .word case0, case1, case2
...
case0: add $s0, $s0, 1
j END_SWITCH
case1: add $s0, $s0, 2
j END_SWITCH
case2: add $s0, $s0, 3
j END_SWITCH
int C = 2; // C = 0, 1, or 2
$t1 = JumpTable[C*4];
jr $t1
17
12/4/2023
Full code
.data
JUMP_TABLE: .word case0, case1, case2
.text
main:
# Let $t0 hold the value of c variable (0, 1, or 2)
bltz $t0, END_SWITCH
li $t1, 2
bgt $t0, $t1, END_SWITCH
# Multiply switch value 0, 1, or 2 by 4
sll $t0, $t0, 2
la $a0, JUMP_TABLE
add $a0, $a0, $t0
lw $t2, 0($a0)
jr $t2
case0: add $s0, $s0, 1 # $s0 hold the value of x variable
j END_SWITCH
case1: add $s0, $s0, 2
j END_SWITCH
case2: add $s0, $s0, 3
j END_SWITCH
END_SWITCH:
18
12/4/2023
Exercise
• Write the MIPS assembly code for:
19
12/4/2023
Exercise: Solution
LOOP:
# Load register $v0 with code 5 for read integer
li $v0, 5
While Loop
# High-level language pseudo code
int i = 0;
while (i < 10)
{
i = i + 1;
}
20
12/4/2023
While Loop
# MIPS branch-friendly pseudo code
int i = 0;
LOOP:
if (i >= 10) goto END_WHILE;
i = i + 1;
goto LOOP;
Exercise
• Write the branch-friendly pseudo code
int i = 10;
int sum = 0;
while (i >= 0)
{
sum = sum + i;
i = i – 1;
}
21
12/4/2023
Exercise
• Write the equivalent MIPS assembly code
Exercise
• Write the MIPS assembly code for this program
22
12/4/2023
Exercise Solution
For Loop
C Simplified C
23
12/4/2023
Nested Loop
int x = 0; # $t0 = x
while(x < 3) # $s0 = 3
{
int y = 0; # $t1 = y
while(y < 5) # $s1 = 5
{
y++;
}
Write the equivalent MIPS code
x++;
}
24
12/4/2023
$t0 = $t0+1;
go to Loop1;
Exit_while1:
……
Exercise
Write the MIPS assembly code to do insertion sort (shown by the following C
code segment).
int main() {
int i, j, v;
int A[10] = {6, 3, 7, 2, 0, 9, 1, 8, 4, 5};
for (i = 1; i < 10; ++i) {
v = A[i];
for (j = i - 1; j >= 0 && A[j] >= v; --j) {
A[j+1] = A[j];
}
A[j+ 1] = v;
}
return 0;
}
25