Hi Chegg Community Experts, I need to translate this C++ code in MIPS assembly c
ID: 3801517 • Letter: H
Question
Hi Chegg Community Experts, I need to translate this C++ code in MIPS assembly code. The core of the exercises is the proper use of loops. 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 ex-pressions may need to be assigned to temporary registers.
2 Initialize variables to registers. (actually put the values into the registers.)
3 Then move onto the rest of the code.
Expected Output:
Sum: 5050
Reversed Number: 98654
is Palindrome: 1
C++ code:
Explanation / Answer
here iam giving the code in 3 part as sum, reversed number and palindrome
SUM:
li $t0, 0 #t0 is sum=0
li $t, 100 # t1 is constant 100
li $t2,0 #t2 is constant 0
loop: # for loop exection
beq $t2, $t1, end
loop body
addi $t0, $t0,$t1
j loop
end:
Reversed Number:
li $a1, 45689 # $a1 contains input number
xor $a2, $a2, $a2 # $a2 will hold reverse integer
li $t1, 10
beqz $a1, end
loop:
divu $a1, $t1 # Divide number by 10
mflo $a1 # $a1 = quotient
mfhi $t2 # $t2 = reminder
mul $a2, $a2, $t1 # reverse=reverse*10
add $a2, $a2, $t2 # + reminder
bgtz $a1, loop
end:
j end # Here $a2 contains the reversed integer
To check Palindrome
move $t0,0 #set $t0 to 0 for your counter.
pos1:
beq $t0,8 , pos2 #terminate the loop if $t0 equals 8
addi $t0, $t0, 1
j pos1 #start the next iteration of the loop
pos2:
loop:
move $t0, 0($a0) # load the first array value into t0
move $t1,0($a8) #load last array value
addi $t0, $t0, 1
subi $t1,$t1,1
beq $t1, $t0, exit
j loop