Question

# The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i...

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 Assignment3.F19.s. You won’t have to write the data allocation sections, some of the initializations, and the output loop at the end.

So the C++ code given is:

#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;

}

}

The MIPS is partially given but there are parts to fill in:

.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 "\n"

# j \$s0

# i \$s1

# &x[0] \$s2

# &ascend[0] \$s3

.text

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++;

# }

# }

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

MIPS CODE:

.data
x: .word 0 29 13 9 0 3 7 7
ascend: .word 0 0 0 0 0 0 0 0
endl: .asciiz "\n"
.text
.globl main
main:
la \$s0,x
la \$s1,ascend

li \$t0,0

li \$s2,0

forLoop1:

beq \$t0,8,printAscend

lw \$t1,0(\$s0)

lw \$t2,4(\$s0)

blt \$t1,\$t2,assign

j forLoop1

assign:

sw \$t0,0(\$s1)

j forLoop1

printAscend:

li \$t0,0

la \$s1,ascend

forLoop2:

beq \$t0,\$s2,exit

lw \$a0,0(\$s1)

li \$v0,1

syscall

la \$a0,endl

li \$v0,4

syscall

j forLoop2
exit:
li \$v0,10
syscall

OUTPUT: