Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- .align 2
- numbers: .word 1, 5, 4, 5635, 6, 346, 6563, 47, 23, 22
- .text
- la $t0, numbers # load address of numbers into $t0
- li $a1, 10 # load 10 into $a1 (n)
- jal BubbleSort
- dispNumbers:
- lw $a0, ($t0)
- puti $a0
- putc ', '
- addi $t0, $t0, 4 # increment $t0 by 4 (go to next number)
- sub $a1, $a1, 1 # decrement $a1 by 1 (n--)
- bnez $a1, dispNumbers
- endProgram:
- done
- BubbleSort:
- li $t1, 1 # k = 1
- # a1 = n (10)
- move $t2, $a1 # $t2 = n
- KLoop:
- li $t3, 0 # i = 0
- move $t4, $t2 # $t4 = n
- sub $t4, $t4, 1 # $t4--
- la $t0, numbers # load address of numbers into $t0
- ILoop:
- lw $t5, ($t0) # $t5 = a[i]
- lw $t6, 4($t0) # $t6 = a[i+1]
- sgt $t7, $t5, $t6 # if(a[i]>a[i+1]){t7 = 1}
- beqz $t7, Increment
- swap: # effectively swaps a[i] and a[i+1]
- sw $t6, ($t0) # a[i] = a[i+1]
- sw $t5, 4($t0) # a[i+1] = temp < -ish >
- Increment:
- addi $t3, $t3, 1 # i++
- addi $t0, $t0, 4 # $t0 += 4
- slt $t7, $t3, $t4 # if(i<n-1){t7 = 1}
- bnez $t7, ILoop
- addi $t1, $t1, 1 # k++
- slt $t7, $t1, $t2 # if(k<=n){t7 = 1}
- bnez $t7, KLoop # if(k<=n){KLoop}
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment