Skip to content

Instantly share code, notes, and snippets.

@harveyslash
Created March 20, 2018 17:33
Show Gist options
  • Save harveyslash/d5eb47480065580f081e1d77e0706047 to your computer and use it in GitHub Desktop.
Save harveyslash/d5eb47480065580f081e1d77e0706047 to your computer and use it in GitHub Desktop.
sort:
addi $sp,$sp,-40 # allocate space for the return address
sw $ra,4($sp)
sw $s0,8($sp)
sw $s1,12($sp)
sw $s2,16($sp)
sw $s3,20($sp)
sw $s4,24($sp)
sw $s5,28($sp)
sw $s6,32($sp)
sw $s7,36($sp)
li $s0, 2 # store 2 as constant
move $s1, $a1 # store param a1
move $s2, $a2 # store param a2
slt $t5,$a2,$a1
beq $a2,$a1,sort_done
beq $t5,$zero,sort_non_trivial
j sort_done
sort_non_trivial:
# sort left first:
div $a2, $s0
mflo $a2
jal sort
# sort right
sub $t1, $a2 ,$a1
div $t1, $s0
mflo $t1
addi $t1, $t1,1
add $t1, $t1,$a1
move $a1 ,$t1 # a1 now = a2
jal sort
move $a1, $s1
move $a2, $s2
sub $t1, $a2 ,$a1
div $t1, $s0
mflo $t1
move $a3, $t1
jal merge
sort_done:
lw $s7,36($sp)
lw $s6,32($sp)
lw $s5,28($sp)
lw $s4,24($sp)
lw $s3,20($sp)
lw $s2,16($sp)
lw $s1,12($sp)
lw $s0,8($sp)
lw $ra,4($sp)
addi $sp,$sp,40 # allocate space for the return address
jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment