Skip to content

Instantly share code, notes, and snippets.

@dark-swordsman
Last active March 12, 2018 02:31
Show Gist options
  • Select an option

  • Save dark-swordsman/605200586df7cc42e6b1e7a3efe26d33 to your computer and use it in GitHub Desktop.

Select an option

Save dark-swordsman/605200586df7cc42e6b1e7a3efe26d33 to your computer and use it in GitHub Desktop.
# REGISTER DIRECTORY
# $t0 array length persist
# $t1 stack pointer address persist
# $t2 current index
# $t3 current address
# $t4 base number temp
# $t5 next number temp
# $t6 temporary number temp
# $t7 bubble sort -1 index limit
# $t8 swap true or false
# prompt the user for array length
.data
enterLengthString: .asciiz "\nPlease enter the amount of numbers you are going to input into the program.\n"
.text
# print string
li $v0, 4
la $a0, enterLengthString
syscall
# get input
li $v0, 5
syscall
# store array length in $t0
add $t0, $v0, $zero
# initalize index with 0
li $t2, 0
# define start of array with stack pointer
la $t1, ($sp)
# defire current address
la $t3, ($t1)
.data
enterNumberString: .asciiz "Please enter the numbers\n"
.text
# print string
li $v0, 4
la $a0, enterNumberString
syscall
enterLoop:
# get input
li $v0, 5
syscall
sw $v0, ($t3)
addi $t3, $t3, 4
addi $t2, $t2, 1
blt $t2, $t0, enterLoop
# print original array
.data
originalArrayMessage: .asciiz "\nHere is your original array: \n"
.text
li $v0, 4
la $a0, originalArrayMessage
syscall
# reset address index and number index
la $t3, ($t1)
li $t2, 0
.data
arraySpace: .asciiz ", "
.text
printLoop:
li $v0, 1
lw $a0, ($t3)
syscall
li $v0, 4
la $a0, arraySpace
syscall
addi $t3, $t3, 4
addi $t2, $t2, 1
blt $t2, $t0, printLoop
# bubble sort
.data
bubbleSortAlert: .asciiz "\n\nBubble Sorting...\n"
.text
li $v0, 4
la $a0, bubbleSortAlert
syscall
restartBubbleSort:
# reset address and index
la $t3, ($t1)
li $t2, 0
subi $t7, $t0, 1
li $t8, 0
bubbleSortLoop:
lw $t4, ($t3)
lw $t5, 4($t3)
bgt $t4, $t5, swap
j skip
swap:
add $t6, $t5, $zero
sw $t4, 4($t3)
sw $t6, ($t3)
li $t8, 1
skip:
addi $t3, $t3, 4
addi $t2, $t2, 1
blt $t2, $t7, bubbleSortLoop
beq $t8, 1, restartBubbleSort
# print result
.data
sortedArrayMessage: .asciiz "\nHere is your sorted array:\n"
.text
li $v0, 4
la $a0, sortedArrayMessage
syscall
# reset address and index
la $t3, ($t1)
li $t2, 0
printSortedLoop:
li $v0, 1
lw $a0, ($t3)
syscall
li $v0, 4
la $a0, arraySpace
syscall
addi $t3, $t3, 4
addi $t2, $t2, 1
blt $t2, $t0, printSortedLoop
# exit program
li $v0, 10
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment