0% found this document useful (0 votes)
35 views

Chapter 9 How to convert basic C code to MIPS assembly language

The document provides an overview of MIPS programming basics, including conditional statements such as if, if-else, and switch-case, along with loops like while and for. It emphasizes a strategy for writing MIPS code by first using high-level languages, then mapping them to pseudo code, and finally translating to MIPS instructions. Additionally, it includes examples and exercises for practicing MIPS assembly code related to branching and comparisons.

Uploaded by

hung.lq05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Chapter 9 How to convert basic C code to MIPS assembly language

The document provides an overview of MIPS programming basics, including conditional statements such as if, if-else, and switch-case, along with loops like while and for. It emphasizes a strategy for writing MIPS code by first using high-level languages, then mapping them to pseudo code, and finally translating to MIPS instructions. Additionally, it includes examples and exercises for practicing MIPS assembly code related to branching and comparisons.

Uploaded by

hung.lq05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

12/4/2023

MIPS Programming Basic


Dr. Truong Dinh Huy

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.

Simplified C (friendly pseudo code)


does not have while, compound if, complex expressions
does have simple if, goto, one-operator expressions

Branch with beq instruction


# high-level language pseudo code (C code)
if ($t0 != $t1)
$t0 = $t0 – 1;

# MIPS branch-friendly pseudo code


if ($t0 == $t1)
goto endif;
$t0 = $t0 – 1;
endif:

# Equivalent MIPS assembly code


beq $t0, $t1, endif
# then $t0 = $t0 - 1
addi $t0, $t0, -1
endif:

2
12/4/2023

Branch with bne instruction


# high-level language pseudo code
if ($t0 == $t1)
$t0 = $t0 – 1;
……

# MIPS branch-friendly pseudo code


if ($t0 != $t1)
goto endif;
$t0 = $t0 – 1;
endif:
……

# Equivalent MIPS assembly code


bne $t0, $t1, endif
# then $t0 = $t0 - 1
addi $t0, $t0, -1
endif:
……

Example
• Write the branch-friendly pseudo code

if ($t0 == $t1)
{
$t0 += 1;
$s0 += $t0;
}

3
12/4/2023

Solution

• First, branch-friendly pseudo


code MIPS assembly code

bne $t0, $t1, END_IF


if ($t0 != $t1)
# $t0 = $t0 + 1
goto END_IF;
addi $t0, $t0, 1
$t0 += 1;
$s0 += $t0; # $s0 = $s0 + $t0
END_IF: add $s0, $s0, $t0
END_IF:

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;
………

# MIPS branch-friendly pseudo code


if ($t0 <= $t1)
goto endif;
$t0 = $t0 – 1;
endif:
………

# Equivalent MIPS assembly code


ble $t0, $t1, endif
# then $t0 = $t0 - 1
addi $t0, $t0, -1
endif:
……

5
12/4/2023

bge instruction:
# high-level language pseudo code
if ($t0 < $t1)
$t0 = $t0 – 1;

# MIPS branch-friendly pseudo code


if ($t0 >= $t1)
goto endif;
$t0 = $t0 – 1;
endif:

# Equivalent MIPS assembly code


bge $t0, $t1, endif
# then $t0 = $t0 - 1
addi $t0, $t0, -1
endif:

blt instruction
# high-level language pseudo code
if ($t0 >= $t1)
$t0 = $t0 – 1;

# MIPS branch-friendly pseudo code


if ($t0 < $t1)
goto endif;
$t0 = $t0 – 1;
endif:

# Equivalent MIPS assembly code


blt $t0, $t1, endif
# then $t0 = $t0 - 1
addi $t0, $t0, -1
endif:

6
12/4/2023

bgt instruction
# high-level language pseudo code
if ($t0 <= $t1)
$t0 = $t0 – 1;

# MIPS branch-friendly pseudo code


if ($t0 > $t1)
goto endif;
$t0 = $t0 – 1;
endif:

# Equivalent MIPS assembly code


bgt $t0, $t1, endif
# then $t0 = $t0 - 1
addi $t0, $t0, -1
endif:

Summary: Comparisons & Branch


C code Pseudo Code MIPS instruction
if ($t0 != $t1) if ($t0 == $t1) goto beq $t0, $t1, label
if ($t0 == $t1) if ($t0 != $t1) goto bne $t0, $t1, label
if ($t0 >= $t1) if ($t0 < $t1) goto blt $t0, $t1, label
if ($t0 <= $t1) if ($t0 > $t1) goto bgt $t0, $t1, label
if ($t0 > $t1) if ($t0 <= $t1) goto ble $t0, $t1, label
if ($t0 < $t1) if ($t0 >= $t1) goto bge $t0, $t1, label

7
12/4/2023

Exercises
• Write branch-friendly pseudo code and MIPS assembly for each:
if($t0 > $t1)
$t0 = $t0 + 1;

if($t0 <= $t1)


$t0 = $t0 + 1;

Comparisons with Zero


• Branch comparisons will frequently be made with zero
• if (X == 0)
• if (X != 0)
• if (X > 0)
• if (X < 0)
• if (X >= 0)
• if (X <= 0)

8
12/4/2023

Branch if Equal to Zero


# high-level language pseudo code
if ($t0 != 0)
$t0 = $t0 – 1;
………

# MIPS branch-friendly pseudo code


if ($t0 == 0) goto endif;
$t0 = $t0 – 1;
endif:
………

# Equivalent MIPS assembly code


beqz $t0, endif
# else $t0 = $t0 - 1
addi $t0, $t0, -1
endif:

Branch if Not Equal to Zero


# high-level language pseudo code
if ($t0 == 0)
$t0 = $t0 – 1;
………

# MIPS branch-friendly pseudo code


if ($t0 != 0) goto endif;
$t0 = $t0 – 1;
endif:
………

# Equivalent MIPS assembly code


bnez $t0, endif
# then $t0 = $t0 - 1
addi $t0, $t0, -1
endif:
………

9
12/4/2023

Comparisons with Zero


Pseudo Code MIPS instruction
if ($t0 == 0) goto beqz $t0, label
if ($t0 != 0) goto bnez $t0, label
if ($t0 > 0) goto bgtz $t0, label
if ($t0 < 0) goto bltz $t0, label
if ($t0 >= 0) goto bgez $t0, label
if (X <= 0) goto blez $t0, label

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

Conditionals —IF Else from C to Simplified C

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:

Conditionals —IFfrom Simplified C to MIPS


Simplified C MIPS
# assuming i in $t0,
# assuming n in $t1...

if (i >= 0) goto else1;


bge $t0, 0, else1
n = n - i;
sub $t1, $t1, $t0
goto end1;
goto end1
else1:
else1:
n = n + i;
add $t1, $t1, $t0
end1:
end1:

11
12/4/2023

Another example: If-else


C Code Simplified C
if (i < 0 && n >= 42) { if (i >= 0) goto else1;
if (n < 42) goto else1;
n = n - i; n = n - i;
goto end1;
} else { else1:
n = n + i; n = n + i;
} end1:

Simplified C MIPS
# assume i in $t0
# assume n in $t1

if (i >= 0) goto else1; bge $t0, 0, else1


if (n < 42) goto else1; blt $t1, 42, else1
n = n - i; sub $t1, $t1, $t0
goto end1; j end1
else1:
else1:
n = n + i;
add $t1, $t1, $t0
end1:
end1:

12
12/4/2023

Exercise – Part 1
• Write the MIPS branch-friendly pseudo code for this if-then-else statement.

# High-level language pseudo code


if ($t0 >= 0)
{
$s0 += 1;
$s1 += $s0;
}
else
{
$s0 -= 1;
$s2 += $s0;
}

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

• Considering switch statement as a chain of if-else statements


If (c==0){
x= x+1;
}
else{
if (c==1){x=x+2;}
else{
if (c==2){x=x+3;}
}
}

Method 2: jump table


JUMP_TABLE = { case0, case1, case2 }
# Default skips over switch if key value is out of range
if (C < 0) goto END_SWITCH;
if (C > 2) goto END_SWITCH;
goto JUMP_TABLE[C];
case0: $s0 = $s0 + 1; goto END_SWITCH;
case1: $s0 = $s0 + 2; goto END_SWITCH;
case2: $s0 = $s0 + 3; goto END_SWITCH;
END_SWITCH:

16
12/4/2023

MIPS jump table


 Initialize an array of 32-bit words so each element holds the
address of the first instruction of each switch case Aren’t these numbers ???

.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

Using the Jump Table

 The case variable value 0, 1, or 2 is used to index an


element of the Jump Table

 Must multiply case variable value by 4 since the Jump


Table is an array of words

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:

Example Do-While Loop


# C++ do-while loop # Branch-friendly
int i = 0; # pseudo code
do int i = 0;
LOOP:
{
i = i + 1;
i = i + 1; if(i < 10) goto LOOP;
}
while(i < 10);

18
12/4/2023

Example Do-While Loop


# Equivalent MIPS assembly code
li $t0, 0
li $t1, 10
LOOP:
# Increment $t0 by 1
addi $t0, $t0, 1

# if $t0 < 10 goto LOOP


blt $t0, $t1, LOOP

Exercise
• Write the MIPS assembly code for:

# User must enter an integer > 0


int x;
do
{
cin >> x;
}
while(x < 0);

19
12/4/2023

Exercise: Solution
LOOP:
# Load register $v0 with code 5 for read integer
li $v0, 5

# Read integer returns value in $v0 register


syscall

bltz $v0, LOOP

While Loop
# High-level language pseudo code
int i = 0;
while (i < 10)
{
i = i + 1;
}

# MIPS branch-friendly pseudo code


int i = 0;
LOOP:
if (i >= 10) goto END_WHILE;
i = i + 1;
goto LOOP;
END_WHILE:

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;

# Equivalent MIPS assembly code


li $t0, 0
li $t1, 10
LOOP: bge $t0, $t1, END_WHILE
addi $t0, $t0, 1
b LOOP
END_WHILE:

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

i = 10; # Equivalent MIPS assembly code


sum = 0; li $t0, 10
WHILE: li $s0, 0
WHILE: bltz $t0, END_WHILE
if (i < 0) goto END_WHILE;
add $s0, $s0, $t0
sum = sum + i;
addi $t0, $t0, -1
i = i – 1; b WHILE
goto WHILE; END_WHILE:
END_WHILE:

Exercise
• Write the MIPS assembly code for this program

char *str = “Hello World!”;


char *s = str;
while (*s != ‘\0’) { printf(“%c”, *s); s = s + 1; }

22
12/4/2023

Exercise Solution

For Loop
C Simplified C

for (int i = 1; i <= 10; i++) { int i;


printf(”%d\n”, i); i = 1;
} loop:
……… if (i > 10) goto end;
i++;
printf(”%d”, i);
printf(”\n”);
goto loop;
end:
……

23
12/4/2023

li $t0, 1 # int i; // i in register $t0


# i = 1;
loop: # loop:
bgt $t0, 10, end # if (i > 10) goto end;
move $a0, $t0 # printf(”%d” i);
li $v0, 1
syscall
li $a0, '\n' # printf(”%c”, '\n');
li $v0, 11
syscall
addi $t0, $t0, 1 # i++;
b loop # goto loop;
end:
………

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

Nested Loop (2)


$t0 = 0; # $t0 = x
Loop1:
if($t0 >=3) goto Exit_while1;
$t1 = 0; # $t1 = y
Loop2:
if ($t1>=5) goto Exit_while2;
$t1 = $t1 + 1;
goto Loop2;
Exit_while2:

$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

You might also like