Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert C++ code into MIPS assembly Please do not modify the C++ code itself. Yo

ID: 3739767 • Letter: C

Question

Convert 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.

Expected Output:

Sum: 7179

Reversed Number: 98654

is Palindrome: 1

____________________________________________________________________________________________

Base MIPS Code:

.data
endl: .asciiz " " # 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, $s2 # 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, $s4 # 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

_______________________________________________________________________________________________________________________

C++ Equivalent

#include <iostream>

using namespace std;

int main(void)

{

int sum = 0;

int size = 10;

int sumarr[] = {1,3,44,66,88,90,9,232,4325,2321};

for(int i = 0; i < size; i++){

sum = sum + sumarr[i];

}

int num = 45689;

int rev = 0;

int d = -1;

while( num > 0){

d = num % 10;

rev = rev*10 + d;

num = num / 10;

}

int arr[] = {1,2,3,4,5,4,3,2,1};

int beg = 0;

int end = 8;

int isPalindrome = 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;

}

Explanation / Answer

$LC2:

.ascii "Sum: "

$LC3:

.ascii "Reversed Number: "

$LC4:

.ascii "Is Palindrome: "

$LC0:

.word 1

.word 3

.word 44

.word 66

.word 88

.word 90

.word 9

.word 232

.word 4325

.word 2321

$LC1:

.word 1

.word 2

.word 3

.word 4

.word 5

.word 4

.word 3

.word 2

.word 1

main:

addiu $sp,$sp,-144

sw $31,140($sp)

sw $fp,136($sp)

move $fp,$sp

sw $0,24($fp)

li $2,10 # 0xa

sw $2,52($fp)

lui $2,%hi($LC0)

addiu $3,$fp,60

addiu $2,$2,%lo($LC0)

li $4,40 # 0x28

move $6,$4

move $5,$2

move $4,$3

jal memcpy

nop

sw $0,28($fp)

$L3:

lw $3,28($fp)

lw $2,52($fp)

nop

slt $2,$3,$2

beq $2,$0,$L2

nop

lw $2,28($fp)

nop

sll $2,$2,2

addiu $3,$fp,24

addu $2,$3,$2

lw $2,36($2)

lw $3,24($fp)

nop

addu $2,$3,$2

sw $2,24($fp)

lw $2,28($fp)

nop

addiu $2,$2,1

sw $2,28($fp)

b $L3

nop

$L2:

li $2,45689 # 0xb279

sw $2,32($fp)

sw $0,36($fp)

li $2,-1 # 0xffffffffffffffff

sw $2,56($fp)

$L5:

lw $2,32($fp)

nop

blez $2,$L4

nop

lw $3,32($fp)

li $2,10 # 0xa

bne $2,$0,1f

div $0,$3,$2

break 7

1:

mfhi $2

sw $2,56($fp)

lw $2,36($fp)

nop

sll $2,$2,1

sll $3,$2,2

addu $3,$2,$3

lw $2,56($fp)

nop

addu $2,$3,$2

sw $2,36($fp)

lw $3,32($fp)

li $2,10 # 0xa

bne $2,$0,1f

div $0,$3,$2

break 7

1:

mfhi $2

mflo $2

sw $2,32($fp)

b $L5

nop

$L4:

lui $2,%hi($LC1)

addiu $3,$fp,100

addiu $2,$2,%lo($LC1)

li $4,36 # 0x24

move $6,$4

move $5,$2

move $4,$3

jal memcpy

nop

sw $0,40($fp)

li $2,8 # 0x8

sw $2,44($fp)

li $2,1 # 0x1

sw $2,48($fp)

$L8:

lw $3,40($fp)

lw $2,44($fp)

nop

slt $2,$3,$2

beq $2,$0,$L6

nop

lw $2,40($fp)

nop

sll $2,$2,2

addiu $3,$fp,24

addu $2,$3,$2

lw $3,76($2)

lw $2,44($fp)

nop

sll $2,$2,2

addiu $4,$fp,24

addu $2,$4,$2

lw $2,76($2)

nop

beq $3,$2,$L7

nop

li $2,-1 # 0xffffffffffffffff

sw $2,48($fp)

b $L6

nop

$L7:

lw $2,40($fp)

nop

addiu $2,$2,1

sw $2,40($fp)

lw $2,44($fp)

nop

addiu $2,$2,-1

sw $2,44($fp)

b $L8

nop

$L6:

lui $2,%hi($LC2)

addiu $5,$2,%lo($LC2)

lui $2,%hi(std::cout)

addiu $4,$2,%lo(std::cout)

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

lw $5,24($fp)

move $4,$2

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

nop

move $3,$2

lui $2,%hi(std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&))

addiu $5,$2,%lo(std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&))

move $4,$3

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))

nop

lui $2,%hi($LC3)

addiu $5,$2,%lo($LC3)

lui $2,%hi(std::cout)

addiu $4,$2,%lo(std::cout)

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

lw $5,36($fp)

move $4,$2

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

nop

move $3,$2

lui $2,%hi(std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&))

addiu $5,$2,%lo(std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&))

move $4,$3

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))

nop

lui $2,%hi($LC4)

addiu $5,$2,%lo($LC4)

lui $2,%hi(std::cout)

addiu $4,$2,%lo(std::cout)

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

lw $5,48($fp)

move $4,$2

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

nop

move $3,$2

lui $2,%hi(std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&))

addiu $5,$2,%lo(std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&))

move $4,$3

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))

nop

move $2,$0

move $sp,$fp

lw $31,140($sp)

lw $fp,136($sp)

addiu $sp,$sp,144

j $31

nop

__static_initialization_and_destruction_0(int, int):

addiu $sp,$sp,-32

sw $31,28($sp)

sw $fp,24($sp)

move $fp,$sp

sw $4,32($fp)

sw $5,36($fp)

lw $3,32($fp)

li $2,1 # 0x1

bne $3,$2,$L12

nop

lw $3,36($fp)

li $2,65535 # 0xffff

bne $3,$2,$L12

nop

lui $2,%hi(std::__ioinit)

addiu $4,$2,%lo(std::__ioinit)

jal std::ios_base::Init::Init()

nop

lui $2,%hi(__dso_handle)

addiu $6,$2,%lo(__dso_handle)

lui $2,%hi(std::__ioinit)

addiu $5,$2,%lo(std::__ioinit)

lui $2,%hi(std::ios_base::Init::~Init())

addiu $4,$2,%lo(std::ios_base::Init::~Init())

jal __cxa_atexit

nop

$L12:

nop

move $sp,$fp

lw $31,28($sp)

lw $fp,24($sp)

addiu $sp,$sp,32

j $31

nop

_GLOBAL__sub_I_main:

addiu $sp,$sp,-32

sw $31,28($sp)

sw $fp,24($sp)

move $fp,$sp

li $5,65535 # 0xffff

li $4,1 # 0x1

jal __static_initialization_and_destruction_0(int, int)

nop

move $sp,$fp

lw $31,28($sp)

lw $fp,24($sp)

addiu $sp,$sp,32

j $31

nop