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
1a. Using the MIPS program below (with bugs intact), determine the instruction format for each instruction...
1a. Using the MIPS program below (with bugs intact), determine the instruction format for each instruction and write the decimal values of each instruction field. addi $v0, $zero, 0 loop: lw $v1, 0($a0) sw $v1, 0($a1) sll $a0, $a0, 2 add $a1, $a1, $a0 beq $v1, $zero, loop 1b. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds...
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...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following conditional statement. If ( n is even) { n = n / 2; } else { n = 3 * n + 1; } In this case, n is to be input by the user (assume they input a non-negative value), the conditional is performed, and the resulting n is to be output. Again, use the system calls for input, output, and exiting the...
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 =...
For the MIPS assembly instructions below, what is the corresponding C statement? Assume that the variables...
For the MIPS assembly instructions below, what is the corresponding C statement? Assume that the variables f, g, h, and I are assigned to registers $s0 - $s3, respectively. Assume that the base address of arrays X, Y, and Z are in registers $s4 - $s6, respectively. Be conservative and use a single temp register as possible. It is a C arithmetic command with two source operands and a destination. Each part is 3 points. Add $t0, $s0, $s1 addi...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
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 =...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
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...