Convert C code to MIPS assembly language
Machine Structures
Description
For project four, your objective is to convert the given C++ code into MIPS assembly.
Please do not modify the C++ code itself. You are only allowed to make modifications to
the assembly file. Start writing your code below the main: label and above the exit: label.
For this project stay BETWEEN these labels.
When doing a C++ to MIPS conversion, it can be done in the following steps:
1 Assign variables to registers. When inspecting code, any constant values in expressions
may need to be assigned to temporary registers.
2 Initialize variables to registers. (actually put the values into the registers.)
3 Then you may begin converting the rest of the code.
Before you begin, please make sure you click the link on ilearn to create your GitHub repo.
After created please clone this repo with the git clone repo url command.
Base MIPS Code
p4code
#include <iostream>
using namespace std;
int main(void)
{
int sum = 0;
int size = 10;
intsumarr[] = {1,3,44,66,88,90,9,232,4325,2321};
for(int i = 0; i < size; i++){
sum = sum + sumarr[i];
}
intnum = 45689;
int rev = 0;
int d = -1;
while( num > 0){
d = num % 10;
rev = rev*10 + d;
num = num / 10;
}
intarr[] = {1,2,3,4,5,4,3,2,1};
int beg = 0;
int end = 8;
intisPalindrome = 1;
while(beg < end){
if (arr[beg] != arr[end]){
isPalindrome = -1;
break;
}
beg++;
end–;
}
cout<< “Sum: ” << sum <<endl;
cout<< “Reversed Number: ” << rev <<endl;
cout<< “Is Palindrome: ” <<isPalindrome<<endl;
return 0;
}
p4codeBase
.data
endl: .asciiz “\n” # used for cout<<endl;
sumlbl: .asciiz “Sum: ” # label for sum
revlbl: .asciiz “Reversed Number: ” # label for rev
pallbl: .asciiz “Is Palindrome: ” # label for isPalindrome
sumarr: .word 1
.word 3
.word 44
.word 66
.word 88
.word 90
.word 9
.word 232
.word 4325
.word 2321
arr: .word 1
.word 2
.word 3
.word 4
.word 5
.word 4
.word 3
.word 2
.word 1
.text
# sum –> $s0
# address of sumarr –> $s1
# rev –> $s2
# num –> $s3
# isPalindrome –> $s4
# address of arr –> $s5
# i –> $t0
# beg –> $s6
# end –> $s7
# d –> $t1
# 10 –> $t2
# 100 –> $t3
main:
exit:
la $a0, sumlbl # puts sumlbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing a string
syscall # make a syscall to system
move $a0, $s0 # puts sum into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system
la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall
la $a0, revlbl # puts revlbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing an string
syscall # make a syscall to system
move $a0, $s1 # puts rev into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system
la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall
la $a0, pallbl # puts pallbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing a string
syscall # make a syscall to system
move $a0, $s3 # puts isPalindrome into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system
la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall
addi $v0,$0, 10
syscall
Solution
p4code
.data
endl: .asciiz “\n” # used for cout<<endl;
sumlbl: .asciiz “Sum: ” # label for sum
revlbl: .asciiz “Reversed Number: ” # label for rev
pallbl: .asciiz “Is Palindrome: ” # label for isPalindrome
sumarr: .word 1
.word 3
.word 44
.word 66
.word 88
.word 90
.word 9
.word 232
.word 4325
.word 2321
arr: .word 1
.word 2
.word 3
.word 4
.word 5
.word 4
.word 3
.word 2
.word 1
.text
# sum –> $s0
# address of sumarr –> $s1
# rev –> $s2
# num –> $s3
# isPalindrome –> $s4
# address of arr –> $s5
# i –> $t0
# beg –> $s6
# end –> $s7
# d –> $t1
# 10 –> $t2
# 100 –> $t3
main:
addi $s0,$0,0 # int sum = 0;
addi $t2,$0,10 # int size = 10;
la $s1,sumarr # intsumarr[] = {1,3,44,66,88,90,9,232,4325,2321};
addi $t0,$0,0 # // i=0 in the for
for: # for(int i = 0; i < size; i++){
bge $t0,$t2,endfor # //comparison i<size in the for
lw $t4,($s1) # // load sumarr[i] in temporary t4
add $s0,$s0,$t4 # sum = sum + sumarr[i];
addi $s1,$s1,4 # //advance pointer to sumarr[i+1]
addi $t0,$t0,1 # //i++ of the for
j for # }
endfor:
li $s3,45689 # intnum = 45689;
addi $s2,$0,0 # int rev = 0;
addi $t1,$0,-1 # int d = -1;
whilenum: # while( num> 0){
ble $s3,$0,endwhile1 # // comparison for the while
div $s3,$t2 # // num % 10 calculation
mfhi $t1 # d = num % 10;
mult $s2,$t2 # // calculate rev*10
mflo $s2 # // rev = rev*10
add $s2,$s2,$t1 # rev = rev*10 + d;
div $s3,$t2 # // num / 10 calculation
mflo $s3 # num = num / 10;
j whilenum # }
endwhile1:
la $s5,arr # intarr[] = {1,2,3,4,5,4,3,2,1};
addi $s6,$0,0 # int beg = 0;
addi $s7,$0,8 # int end = 8;
addi $s4,$0,1 # intisPalindrome = 1;
whilebeg: # while(beg < end){
bge $s6,$s7,endwhile2 # // comparison for the while
sll $t4,$s6,2 # // for getting offset of arr[beg] first calculate beg*4
add $t4,$t4,$s5 # // add offset to arr address to get &arr[beg]
lw $t4,($t4) # // load $t4 with arr[beg]
sll $t5,$s7,2 # // for getting offset of arr[end] first calculate end*4
add $t5,$t5,$s5 # // add offset to arr address to get &arr[beg]
lw $t5,($t5) # // load $t5 with arr[end]
beq $t4,$t5,endif1 # if (arr[beg] != arr[end]){
addi $s4,$0,-1 # isPalindrome = -1;
j endwhile2 # break;
endif1: # }
addi $s6,$s6,1 # beg++;
addi $s7,$s7,-1 # end–;
j whilebeg # }
endwhile2:
addi $s1,$s2,0 # move rev to s1 as required by the cout below
addi $s3,$s4,0 # move isPalindrome to s3 as required by the cout below
exit:
la $a0, sumlbl # puts sumlbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing a string
syscall # make a syscall to system
move $a0, $s0 # puts sum into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system
la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall
la $a0, revlbl # puts revlbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing an string
syscall # make a syscall to system
move $a0, $s1 # puts rev into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system
la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall
la $a0, pallbl # puts pallbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing a string
syscall # make a syscall to system
move $a0, $s3 # puts isPalindrome into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system
la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall
addi $v0,$0, 10
syscall