Skip to content

Instantly share code, notes, and snippets.

@quinnzipse
Last active October 6, 2020 01:43
Show Gist options
  • Save quinnzipse/f1f9790ca6c1f7ce668418ed3fcc78c2 to your computer and use it in GitHub Desktop.
Save quinnzipse/f1f9790ca6c1f7ce668418ed3fcc78c2 to your computer and use it in GitHub Desktop.
CS270 homework 3 insertion sort of a user inputted number array
#Quinn Zipse H3.asm CS270
.data
prompt1: .asciiz "Enter up to 20 integers (-1 to quit): "
nums: .word 0 : 64
prompt2: .asciiz " "
.text
la $a0, prompt1
la $a1, nums
li $a2, 20
jal readArray
la $a0, nums
add $a1, $v0, $zero
add $s0, $v0, $zero
jal insertionSort
la $a0, nums
add $a1, $s0, $zero
jal printArray
li $v0, 10
syscall
readArray:
li $v0, 4
syscall # print the prompt passed in at $a0
li $t0, 0
j test
loop:
li $v0, 5
syscall # get an int input from the user
sw $v0, 0($a1)
addi $v0, $v0, 1
beqz $v0, done # break the loop if -1 is inserted
addi $a1, $a1, 4
addi $t0, $t0, 1
test:
blt $t0, $a2, loop # loop if we haven't inputted 20 numbers
done:
move $v0, $t0 #move the number of values entered
jr $ra
insertionSort:
li $t0, 0
move $t2, $a0 # Stores the init address for bounds reasons
loop1:
move $t1, $a0 # address for looping backwards
lw $t3, 0($a0) # t3 green
j innerTest
innerLoop:
lw $t4, 0($t1)
# if ( $t3 > 0($t1) )
bgt $t3, $t4, test1 # break out of innerLoop
# else swap $t3, 0($t1)
sw $t4, 4($t1)
sw $t3, 0($t1)
innerTest:
addi $t1 ,$t1, -4 # set the index to n-1
bge $t1, $t2, innerLoop # check to see if index n-1 is valid
test1:
addi $a0, $a0, 4 #increment address
addi $t0, $t0, 1 #increment counter
blt $t0, $a1, loop1
jr $ra
printArray:
li $t0, 0 #setup counter
move $t1, $a0 #move address
j test2
loop2: #loop through printing a number and then a space
lw $a0, 0($t1)
li $v0, 1
syscall
la $a0, prompt2
li $v0, 4
syscall
addi $t1, $t1, 4
addi $t0, $t0, 1
test2:
blt $t0, $a1, loop2 #keep looping if you have more numbers
jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment