Hi. I need help with writing a MIPS program that takes an inline array elements
ID: 3569886 • Letter: H
Question
Hi. I need help with writing a MIPS program that takes an inline array elements and outputs them in ascending and descending order. Any help will be greatly appreciated. Please show complete steps.
Here's my code:
.data
array: .space 100
arrayAscend: .space 100
arrayDescend: .space 100
askInput: .asciiz "Enter value of array element or 9999 to end "
output: .asciiz " You entered: "
comma: .asciiz ", "
.text
main:
li $t4, 9999 #$t4 = 9999
la $s0, array #$s0 = address of array[0]
move $a1, $s0 #saving array address in $a1 for later use
addi $s0, -4 # Moved $s0 one word up to accommodate increment
# later
inputLoop:
addi $s0, 4
#print out the prompt
la $a0, askInput
li $v0, 4
syscall
#read in the array element value entered by user
li $v0, 5
syscall
#store the value in array
move $t0, $v0
sw $t0, ($s0)
beq $t0, $t4, ProcessArray
j inputLoop
ProcessArray:
move $s0, $a1 # $s0 = array[0]
lw $s1, ($s0) # load the last element in $s1
addi $s0, 4
lw $s2, ($s0)
beq $s2, $t4, Exit
slt $t5, $s1, $s2 #$t5 = 1 if $s1 < $s2
beq $t5, $0, swap
j ProcessArray
#la $s0, array
Exit:
move $s0, $a1
#printout the array element array[0]
printLoop:
lw $t3, ($s0)
move $a0, $t3
li $v0, 1
syscall
la $a0, comma
li $v0, 4
syscall
addi $s0, 4
bne $t3, $t4, printLoop #Checking for 9999
li $v0, 10
syscall
swap:
move $a0, $s0
sw $s1, 0($a0)
addi $a0, -4
sw $s2, ($a0)
bne $s2, $t4, Exit
j ProcessArray
Explanation / Answer
.data array: .space 100 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 $t5Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.