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

I\'m writing a Mips program that takes user input as an array of elements and ou

ID: 3570568 • Letter: I

Question

I'm writing a Mips program that takes user input as an array of elements and outputs the data in ascending and descending orders. At present, the program compiles and prints in descending order. Need to know how to have data compiles and prints in reverse order. In other words print in ascending order as well as print in descending order. Any help will greatly appreciated. Answer question completely and correctly. Show all steps. Thank you.

(*Recently, I have been receiving the same incorrect program code responses. Please refrain from sending the same incorrect program code.Only serious individuals will receive full credit.)

Here's my code:


.data
array: .space 100
askInput: .asciiz "Enter value of array element or 9999 to end."
output: .asciiz "You entered: "
comma: .asciiz ", "
#
.text
.globl main
#
main:
la $a1, array
li $a2, 9
li $t0, 0
li $t1,9999
#
loop:
# cout <<"askInput"<< endl;
la $a0, askInput
li $v0, 4
syscall
#
li $v0, 5
syscall
#
beq $v0,$t1,sort
addi $t0,$t0,4
sw $v0, ($a1)
addi $a1, $a1, 4 # move the array over by 1 element
j loop
#
sort:
la $t4, array #t0 is number up to outter loop
la $t1, array #t1 is number comparing to inner loop
addi $t1,$t1,4
la $t8,array
add $t8,$t0,$t8
la $t9,array
add $t9,$t0,$t9
addi $t9,$t9,-4
#
loops:
lw $t2,($t4) #get number 1 outter loop
lw $t3,($t1) #get number 2 inner loop
blt $t3,$t2,next #don't need to swap
sw $t3,($t4) #swap
sw $t2,($t1)
#
next:
addi $t1,$t1,4
blt $t1,$t8,loops #inner loop exit?
addi $t4,$t4,4 #yes-increment outter loop
move $t1,$t4
addi $t1,$t1,4
blt $t4,$t9,loops #outter loop exit?
#
printArray:
# cout <<"output"<< endl;
la $a1,array
la $a0, output
li $v0, 4
syscall
#
loop1:
blez $t0, exit
li $v0, 1
lw $a0, 0($a1)
syscall
#
la $a0, comma
li $v0, 4
syscall
#
addi $a1, $a1, 4
addi $t0, $t0, -4
j loop1
#
exit:
j exit

Explanation / Answer

.data array: .space 40 prompt: .asciiz "Enter a number: " spacee: .asciiz " " output: .asciiz "The numbers are: " .text main: li $t1,10 #load 10 into $t1 la $a1,array #load a pointer to array into $a1 loop: addi $t1,$t1,-1 #subtract 1 from $t1, save to $t1 li $v0,4 #load 4 into $v0 (print string) la $a0,prompt #load prompt text into $a syscall #display prompt li $v0,5 #load 5 into $v0 (read integer) syscall #prompt for input sw $v0,0($a1) #store input int to array addi $a1,$a1,4 #add 4 to $a1, save to $a1 bnez $t1,loop #if $t1 isn't zero,goto loop li $t1,9 #if $t1 is zero, load 9 into $t1 li $t2,9 #and load 9 into $t2 la $a1,array #load array pointer into $a1 loop1: beqz $t2,here #if $t2 is zero, goto here addi $t2,$t2,-1 #subtract 1 from $t2, save to $t2 lw $t5,0($a1) #load an input int into $t5 lw $t6,4($a1) #load the next one into $t6 addi $a1,$a1,4 #add 4 to $a1, save to $a1 ble $t5,$t6,loop1 #if $t5