LordMirai

bronk code

Mar 16th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. .data
  2.  
  3. .align 2
  4. numbers: .word 1, 5, 4, 5635, 6, 346, 6563, 47, 23, 22
  5.  
  6. .text
  7. la $t0, numbers # load address of numbers into $t0
  8. li $a1, 10 # load 10 into $a1 (n)
  9.  
  10. jal BubbleSort
  11.  
  12. dispNumbers:
  13. lw $a0, ($t0)
  14.  
  15. puti $a0
  16. putc ', '
  17.  
  18. addi $t0, $t0, 4 # increment $t0 by 4 (go to next number)
  19. sub $a1, $a1, 1 # decrement $a1 by 1 (n--)
  20.  
  21. bnez $a1, dispNumbers
  22.  
  23. endProgram:
  24. done
  25.  
  26.  
  27. BubbleSort:
  28. li $t1, 1 # k = 1
  29. # a1 = n (10)
  30. move $t2, $a1 # $t2 = n
  31.  
  32. KLoop:
  33. li $t3, 0 # i = 0
  34. move $t4, $t2 # $t4 = n
  35. sub $t4, $t4, 1 # $t4--
  36.  
  37. la $t0, numbers # load address of numbers into $t0
  38.  
  39. ILoop:
  40. lw $t5, ($t0) # $t5 = a[i]
  41. lw $t6, 4($t0) # $t6 = a[i+1]
  42.  
  43. sgt $t7, $t5, $t6 # if(a[i]>a[i+1]){t7 = 1}
  44.  
  45. beqz $t7, Increment
  46.  
  47. swap: # effectively swaps a[i] and a[i+1]
  48. sw $t6, ($t0) # a[i] = a[i+1]
  49. sw $t5, 4($t0) # a[i+1] = temp < -ish >
  50.  
  51. Increment:
  52. addi $t3, $t3, 1 # i++
  53. addi $t0, $t0, 4 # $t0 += 4
  54.  
  55. slt $t7, $t3, $t4 # if(i<n-1){t7 = 1}
  56.  
  57. bnez $t7, ILoop
  58.  
  59. addi $t1, $t1, 1 # k++
  60. slt $t7, $t1, $t2 # if(k<=n){t7 = 1}
  61. bnez $t7, KLoop # if(k<=n){KLoop}
  62.  
  63. jr $ra
Advertisement
Add Comment
Please, Sign In to add comment