Convert the given C++ code into MIPS assembly. arr: .word 1 .word 2 .word 3 .wor
ID: 3739233 • Letter: C
Question
Convert the given C++ code into MIPS assembly.
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
main:
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??;
}
Explanation / Answer
.data
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
main:
la $s5, arr
li $s6, 0 # beg
li $s7, 8 # end
li $s4, 1 # isPalindrome
WHILE: bge $s6, $s7, END # while(beg < end)
sll $t0, $s6, 2
add $t0, $t0, $s5
lw $t0, 0($t0) # $t0 = arr[beg]
sll $t1, $s7, 2
add $t1, $t1, $s5
lw $t1, 0($t1) # $t1 = arr[end]
IF: beq $t0, $t1, ELSE # if (arr[beg] != arr[end])
li $s4, -1 # isPalindrome = ?1;
b END # break
ELSE:
j WHILE
END: