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

we have c++ code and we need to convert to mips assembly language please help ou

ID: 3620280 • Letter: W

Question

we have c++ code and we need to convert to mips assembly language


please help

our teacher already did some parts we are running our code but givving error


here is instructions


The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i is saved in the
array ascend[], in order. Compile and run the program; it should print 0 4 5.
In this exercise, you’ll try to translate the C++ program to MIPS. Some of the more
tedious parts are already given in Prob3.F10.s. You won’t have to write the data
allocation sections, some of the initializations, and the output loop at the end.
Here’s what you have to fill in:
main:li $s0, 0 # int j = 0;
la $s2, x
la $s3, ascend
# for (int i=0; i<7; i++) {
for: # if (x[i] < x[i+1]) {
# ascend[j] = i;
# j++;
# }
# }
As we did in Lab 2.3, we first identify the for loop, and translate it:
li $s1, 0 # for (int i=0; i<7; i++) {
for: # if (x[i] < x[i+1]) {
# ascend[j] = i;
# j++;
# }
addi $s1, $s1, 1
blt $s1, 7, for # }
Next, we deal with the if statement. We have to compare two integers: x[i] and x[i+1].
They have to be in registers before we can compare them with a bge.
First we have to calculate the address of x[i]. According to Chapter 3 slides, this is &x[0]
+ i*4. Then we load it into a temporary register:
li $s1, 0 # for (int i=0; i<7; i++) {
for: mul $t0, $s1, 4 # if (x[i] < x[i+1]) {
add $t0, $t0, $s2
lw $t1, ($t0) # $t1 = x[i]
# ascend[j] = i;
# j++;
# }
addi $s1, $s1, 1
blt $s1, 7, for # }
Then we need to calculate the address of x[i+1]. However, this is just &x[i] + 4! So we
can use a constant offset of 4 from $t0 as the address, and load x[i+1] into another
temporary register, say $t2.
Then we can test x[i] < x[i+1] using a conditional branch. The true part of the if
statement is
# ascend[j] = i;
Again, we’ll need to calculate &ascend[j], then store i into this address, and increment j.
Fill out all the missing code in Prob3.F10.s; make sure your final version runs and
produces the same results as the C++ program.


------------------------------------------------

here are the c++ code

#include <iostream>
using namespace std;

int x[] = {0, 29, 13, 9, 0, 3, 7, 7};
int ascend[8] = {0};
int main() {
int j = 0;
for (int i=0; i<7; i++) {
if (x[i] < x[i+1]) {
ascend[j] = i;
j++;
}
}

for (int i=0; i<j; i++) {
cout << ascend[i] << endl;
}

}



------------------------------------------

here where we need to convert

.data
x: .word 0 #int x[] = {0, 29, 13, 9, 0, 3, 7, 7};
.word 29
.word 13
.word 9
.word 0
.word 3
.word 7
.word 7

ascend: .word 0:8 #int ascend[8] = {0};
endl: .asciiz " "

# j $s0
# i $s1
# &x[0] $s2
# &ascend[0] $s3

.text
main: li $s0, 0 # int j = 0;
la $s2, x
la $s3, ascend


li $s1, 0 # for (int i=0; i<7; i++) {
for: mul $t0, $s1, 4 # if (x[i] < x[i+1]) {
add $t0, $t0, $s2
lw $t1, ($t0) # $t1 = x[i]

ble


# ascend[j] = i;
# j++;
# }

# }

li $s1, 0 # for (int i=0; i<j; i++) {
for1: mul $t0, $s1, 4
add $t0, $t0, $s3
lw $a0, ($t0)
li $v0, 1
syscall # cout << ascend[i] << endl;
la $a0, endl
li $v0, 4
syscall # }

addi $s1, $s1, 1
blt $s1, $s0, for1

li $v0, 10
syscall


////////////////////////////////////




here is the code we wrote and giving error

.data
x: .word 0 #int x[] = {0, 29, 13, 9, 0, 3, 7, 7};
.word 29
.word 13
.word 9
.word 0
.word 3
.word 7
.word 7

ascend: .word 0:8 #int ascend[8] = {0};
endl: .asciiz " "

# j $s0
# i $s1
# &x[0] $s2
# &ascend[0] $s3

.text
main: li $s0, 0 # int j = 0;
la $s2, x
la $s3, ascend


li $s1, 0 # for (int i=0; i<7; i++) {
for: mul $t0, $s1, 4 # if (x[i] < x[i+1]) {
add $t0, $t0, $s2
lw $t1, ($t0) # $t1 = x[i]
add $t0, 4
add $t0, $t0, $s2
lw $t2, ($t0)
bge $t1, $t2, ascend


mul $t0, $s1, 4
add $t0, $t0, $s3
lw $a0, ($t0)
add $a0, $s1, $0 # ascend[j] = i;
addi $s0, $s0, 1 # j++;
li $v0, 4 # }

blt $s1, 7, for # }

li $s1, 0 # for (int i=0; i<j; i++) {
for1: mul $t0, $s1, 4
add $t0, $t0, $s3
lw $a0, ($t0)
li $v0, 1
syscall # cout << ascend[i] << endl;
la $a0, endl
li $v0, 4
syscall # }

addi $s1, $s1, 1
blt $s1, $s0, for1

li $v0, 10
syscall


Explanation / Answer

please rate - thanks may not be what your looking for since I would have done it totally different, but it works