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...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
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...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
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...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....