MIPS SORTING AND DISPLAYING ARRAY I have a mips menu program that ask: 1.) Inser
ID: 670707 • Letter: M
Question
MIPS SORTING AND DISPLAYING ARRAY
I have a mips menu program that ask:
1.) Insert numbers into array (at most 20)
2.) Sort the array
3.) Print the sorted array
4.) Print the unsorted array
5.) Exit
I need help implementing choices 2 and 3, the others I believe I have already solved. Here is my code:
.data
.align 2
jumptable: .word top, case1, case2, case3, case4, case5
Array: .space 80
menu: .ascii " 1.) Insert numbers in array (at most 20) "
.ascii "2.) SORT THE ARRAY "
.ascii "3.) PRINT THE SORTED ARRAY "
.ascii "4.) PRINT THE UNSORTED ARRAY "
.asciiz "5.) Exit "
insert: .asciiz " Please insert 20 numbers into the array "
unsorted: .asciiz " YOUR OUTPUT ARRAY IS: "
blank: .asciiz " "
.globl main
.text
main:
top: li $v0, 4 #Prompt to display menu
la $a0, menu
syscall
li $v0, 5 #Reads menu choice
syscall
li $a2, 20 #SIZE
blez $v0, top #go back to menu if choice is less than or equal zero
li $t4, 5
bgt $v0, $t4, top #go back to menu if choice is greater than 5
la $a1, jumptable #read address for jumptable
sll $t0, $v0, 2 #word offset
add $t1, $a1, $t0 #pointer to jumptable
lw $t2, 0($t1) #load an address from jumptable
jr $t2 #jump to specified case
case1:
li $t3, 20 #i
la $t5, Array #address of array stored in t5
li $v0, 4 #Prompt to display insert string
la $a0, insert
syscall
while:
blez $a2, top #load into array until it decrements past 0
li $v0, 5 #Get integer
syscall
sw $v0, ($t5) #array[i] = v0
addi $t5, $t5, 4 #Shift by a word to make space for next input
addi $a2, $a2, -1 #decrement count
j while
case2:
#sort array
case3:
#print sorted array
case4:
li $v0, 4 #Prompt to display insert string
la, $a0, unsorted
syscall
la $a3, Array
print:
blez $t3,top
li $v0, 1
lw $a0, 0($a3)
syscall
li $v0, 4
la $a0, blank
syscall
addi $a3, $a3, 4
addi $t3, $t3, -1
j print
case5:
li $v0, 10
syscall
Explanation / Answer
for case 2 you can use the below mips code
main:
la $t0, Array
add $t0, $t0, 80
Loop1:
add $t1, $0, $0
la $a0, Array
Loop2:
lw $t2, 0($a0)
lw $t3, 4($a0)
slt $t5, $t2, $t3
beq $t5, $0, continue
add $t1, $0, 1
sw $t2, 4($a0)
sw $t3, 0($a0)
continue:
addi $a0, $a0, 4
bne $a0, $t0, Loop1
bne $t1, $0, Loop2
to print the array you can use below code
li $t0, 1
move $t1, $a1
addi $t2, $a1, 80
loop1:
sw $t0, ($t1)
addi $t1, $t1, 4
bne $t1, $t2, loop1
move $t1, $a1
loop2:
lw $t0, ($t1)
li $v0, 1
move $a0, $t0
syscall
addi $t1, $t1, 4
bne $t1, $t2, loop2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.