# Bubble sort
# Ilustrate hand-assembly, nested procedure call

.data

#------------------------------------------------
Count:      .word 9
Array:      .word 1, 3, 4, 2, -1, 5, 8, 12, -2  # spaces req by QtSpim


.text

#------------------------------------------------
main:       la       $a0,Array         
            lw       $a1,Count            
            jal      sort
            
#------------------------------------------------
            ori      $2,$0,10          # system call: exit program
            syscall

#------------------------------------------------
sort:       sub      $sp,$sp,20
            sw       $ra,16($sp)
            sw       $s3,12($sp)
            sw       $s2,8($sp)
            sw       $s1,4($sp)
            sw       $s0,0($sp)
            
            move     $s2,$a0
            move     $s3,$a1
            
            move     $s0,$zero
for1tst:    bge      $s0,$s3,exit1

            sub      $s1,$s0,1
for2tst:    blt      $s1,$zero,exit2
            sll      $t1,$s1,2
            add      $t2,$s2,$t1
            lw       $t3,0($t2)
            lw       $t4,4($t2)
            ble      $t3,$t4,exit2  
            
            move     $a0,$s2
            move     $a1,$s1
            jal      swap
            
            sub      $s1,$s1,1
            j        for2tst
            
exit2:      add      $s0,$s0,1
            j        for1tst
            
exit1:      lw       $s0,0($sp)
            lw       $s1,4($sp)
            lw       $s2,8($sp)
            lw       $s3,12($sp)
            lw       $ra,16($sp)
            add      $sp,$sp,20
            
            jr       $ra
                 


swap:       sll      $t1,$a1,2
            add      $t1,$a0,$t1
            lw       $t0,0($t1)
            lw       $t2,4($t1)
            sw       $t2,0($t1)
            sw       $t0,4($t1)
            jr       $ra


