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

Homework Answers

Answer #1

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

addi $t0,$t0,1

addi $s0,$s0,4

j forLoop1

assign:

sw $t0,0($s1)

addi $s2,$s2,1

addi $s1,$s1,4

addi $s0,$s0,4

addi $t0,$t0,1

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
addi $t0,$t0,1

addi $s1,$s1,4

j forLoop2
exit:
li $v0,10
syscall

OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
I'm trying to write a nested loop in Mips Assembly that prints out, for example, if...
I'm trying to write a nested loop in Mips Assembly that prints out, for example, if user input x was 5: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 (spaces in between) So far my code is : li $v0, 5   #Ask for integer syscall move $t5, $v0 addi $t0, $zero, 0 For1:    slt $t1, $t0, $t5    beq $t1, $zero, Exit    add $s0, $s0, $t0    addi $t0, $t0, 1...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...