SORTING AN ARRAY IN MIPS ASSEMBLY Hi I am having trouble sorting my array in mip
ID: 671121 • Letter: S
Question
SORTING AN ARRAY IN MIPS ASSEMBLY
Hi I am having trouble sorting my array in mips and printing it when the choice to print sort array is chosen.
Here is my code:
.data
.align 2
jumptable: .word top, case1, case2, case3, case4, case5
Array: .space 80 #array for unsorted
Array1: .space 80 #array for sorted
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 UNSORTED ARRAY IS: "
sorted: .asciiz " YOUR OUTPUT SORTED ARRAY IS: "
blank: .asciiz " "
.globl main
.text
main:
top:
li $t3, 20 #SIZE max
li $v0, 4 #Prompt to display menu
la $a0, menu
syscall
li $v0, 5 #Reads menu choice
syscall
blez $v0, top #go back to menu if choice is less than or equal zero
bgt $v0, 5, 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 $t9, 0 #counter for loop display
la $t5, Array #address of array stored in t5
la $t6, Array1 #address of array1 stored in t6
li $v0, 4 #Prompt to display insert string
la $a0, insert
syscall
while:
li $v0, 5 #Get integer
syscall
beq $v0, -1, top #stop reading integers and go back to menu
sw $v0, ($t5) #array[i] = v0
sw $v0, ($t6) #array1[i] = v0
addi $t5, $t5, 4 #Shift by a word to make space for next input
addi $t6, $t6, 4 #Shift by a word to make space for next input
addi $t3, $t3, -1 #decrement space allowed
addi $t9, $t9, 1 #increment for loop display counter
bgtz $t3, while #if not max keep looping
case2:
li $t3, 1 #bool checker
li $t0, 20 #SIZE
li $t2, 0 #i = 0
la $a0, Array1 #base array in a0
check1:
lw $t5, 0($a0) #t5 = A[i]
lw $t6, 4($a0) #t6 = A[i+1]
slt $t3, $t5, $t6 #if t5<at6 then bool =1
beq $t3, 1, noswap #if bool = 1 noswap
sw $t6, 0($a0) #A[i] = A[i+1]
sw $t5, 4($a0) #A[i+1] = temp
addi $a0, $a0, 4 #add base address by 4 next pos
addi $t2, $t2, 1 #increment i
b check1 #go back to checker
noswap:
addi $a0, $a0, 4 #add base adress by 4 next pos
addi $t2, $t2, 1 #increment i
ble $t2, $t0, check1 #if t2 is less than or equal to t0 go back to check1
b top
case3:
li $v0, 4 #Prompt to display insert string
la, $a0, sorted
syscall
la $a3, Array1 #load base address of Array1 in $a3
print1:
li $v0, 1 #get intger from output and load it into Array1[i]
lw $a0, 0($a3)
syscall
li $v0, 4 #put space in between each integer output
la $a0, blank
syscall
addi $t9, $t9, -1 #decremant loop counter
addi $a3, $a3, 4 #add 4 to a3 to go to next pos in Array1
bgtz $t9, print1 #loop until loop while counter is greater than 0
b top #when counter is equal to 0 then go back to menu
case4:
li $v0, 4 #Prompt to display insert string
la, $a0, unsorted
syscall
la $a3, Array #load base address of Array into $a3
print:
li $v0, 1 #get integer from output and put it into Array[i]
lw $a0, 0($a3)
syscall
li $v0, 4 #put spaces in between each integer when printed
la $a0, blank
syscall
addi $a3, $a3, 4 #adds 4 to go to next pos in Array
addi $t9, $t9, -1 #decrement loop counter
bgtz $t9, print #loop print while t9 i greater than 0
b top #go back to menu when count is equal to 0
case5:
li $v0, 10 #Exit program
syscall
Explanation / Answer
.data
array: .space 100
input: .asciiz "Enter at least 4 integers: Enter the number 1000 to exit "
output: .asciiz "The array in ascending order: "
commas: .asciiz ","
.text
.globl main
main:
la $a1, array #loads a pointer to array into $a1
li $a2,9 #loads 9 into $a2
li $t0,0
li $t1,1000
loops:
la $a0, input #loads input text into $a
li $v0, 4 #loads 4 into $v0 (prints string)
syscall
li $v0, 5 #loads 5 into $v0 (read interger)
syscall
beq $v0,$t1,swap
addi $t0,$t0,4 #add 4 to $t0, save to $t0
sw $v0, ($a1) #stores input into array
addi $a1, $a1,4 #add 4 to $a1, save to $a1
j loops
swap:
la $t4, array #loads array to $t4
la $t1, array #loads array to $t1
addi $t1,$t1,4 #add 4 to $t1, save to $t1
la $t8,array #loads array to $t8
add $t8,$t0,$t8 #add $t8 to $t0, save to $t8
la $t9,array
add $t9,$t0,$t9 #add $t9 to $t0, save to $t9
addi $t9,$t9,-4 #subtracts 4 from $t9, save to $t9
loop:
lw $t2,($t4) #load input into $t2
lw $t3,($t1) #load input into $t3
blt $t2,$t3,loop1 #if $t2 < $t3, go to loops
sw $t3,($t4) #store $t3 in $t4
sw $t2,($t1) #store $t2 in $t1
loop1:
addi $t1,$t1,4 #add 4 to $t1, save to $t1
blt $t1,$t8,loop #if $t1<$t8, go to loop
addi $t4,$t4,4 #add 4 to $t4, save to $t4
move $t1,$t4
addi $t1,$t1,4 #add 4 to $t1, save to $t1
blt $t4,$t9,loop #if $t4<$t9, to go loop
print:
la $a1,array #loads array to $a1
la $a0, output #loads output to $a0
li $v0, 4 #loads 4 into #v0
syscall
loop2:
blez $t0, done #if $t0<=0, go to done
li $v0, 1 #loads 1 into $v0
lw $a0, 0($a1) #load an inout into $a0
syscall
la $a0, commas #loads commas into $a0
li $v0, 4 #loads 4 into $v0
syscall
addi $a1, $a1, 4 #add 4 to $a1, save to $a1
addi $t0, $t0, -4 #subtracts 4 from #t0, save to $t0
j loop2
done:
j done
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.