Please help me fix this mips bubble sort of an array .data Array: .word 0, 4, 3,
ID: 3769954 • Letter: P
Question
Please help me fix this mips bubble sort of an array
.data
Array: .word 0, 4, 3, 2, 1, 7, 5, 9, 10, 6 # array of integers
.text
.globl main
main:
li $a2, 4 # $8 is the index, and variable x
la $a1, Array # $9 is the base address of the array
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 $t2,$t3,next #don't need to swap
sw $t3,($t4) #swap
sw $t2,($t1)
next: addi $t1,$t1,4
blt $t1,$t8,loops #inner loop done?
addi $t4,$t4,4 #yes-increment outter loop
move $t1,$t4
addi $t1,$t1,4
blt $t4,$t9,loops
Explanation / Answer
.data
Array: .word 0, 4, 3, 2, 1, 7, 5, 9, 10, 6 # array of integers
.text
.globl main
main:
la $a1, array # $a1 is the base address of the array
li $a2, 9 # $a2 = 9
li $t0, 0 # i = 0
loop:
la $a0, message1 # load message1 asking to enter int
li $v0, 4 # four is display string
syscall # syscall the displaying of a string
li $v0, 5 # five is read int
syscall #syscall of reading an int
beq $v0,$t1,sort # if int is equal to 99999 then jump to sort
addi $t0,4 # add four to i of the loop
sw $v0, ($a1) # store new int into array
addi $a1, $a1, 4 # move the array over by one element
j loop # jump back to beginning of loop
sort:
la $t4, array # t0 is number up to outer loop
la $t1, array # t1 is number comparing to inner loop
addi $t1, 4 # get next int of array and store in t1
la $t8, array
add $t8, $t0, $t8
la $t9, array
add $t9, $t0, $t9
addi $t9, -4
loops:
lw $t2, ($t4) # load first int to compare
lw $t3, ($t1) # load second int to compare
bgt $t2, $t3, next # if first int greater than second int then jump to next
sw $t3, ($t4) # swap
sw $t2, ($t1)
next:
addi $t1, 4
blt $t1, $t8, loops #inner loop done?
addi $t4, 4 #yes-increment outter loop
move $t1, $t4
addi $t1, 4
blt $t4, $t9, loops #outter loop done?
printArray:
la $a1,array
la $a0, message2
li $v0, 4
syscall
loop1:
blez $t0, done
li $v0, 1
lw $a0, 0($a1)
syscall
la $a0, next_line
li $v0, 4
syscall
addi $a1, $a1, 4
addi $t0, $t0, -4
j loop1
done:
j done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.