# bubbleSort.asm # SETUP .data #set the size and content of array Size: .word 500 Array: .space 2000 NL: .asciiz "\n" .text # Initialize List main: lw $s7, Size # get size of list move $t6, $zero move $t7, $zero # init random number generator with a seed of 1 so all tests are the same li $v0, 40 li $a0, 1 li $a1, 1 syscall INIT_START: beq $t7, $s7, INIT_DONE li $v0, 42 li $a0, 1 li $a1, 100000 SYSCALL addi $a0, $a0, 100 sw $a0, Array($t6) addi $t6, $t6, 4 addi $t7, $t7, 1 b INIT_START INIT_DONE: move $s1, $zero # set counter for # of elems printed move $s2, $zero # set offset from Array # for i = 1 to SZ-1 # j = i # tmp = A[i] # while j > 0 and A[j-1] > A[j] # A[j] = A[j-1] # j-- # A[j] = tmp # i t0 # j t1 # tmp t2 # A[j-1] t3 addi $t0, $zero, 1 # i = 0 SORT_MAIN_LOOP: bge $t0, $s7, SORT_DONE mul $t1, $t0, 4 # j = i (actually ) lw $t2, Array($t1) # tmp = A[j] INNER_LOOP_START: beq $t1, 0, INNER_LOOP_END addi $t5, $t1, -4 # t5 is j-1 lw $t3, Array($t5) blt $t3, $t2, INNER_LOOP_END # exit condition A[j-1] <= tmp sw $t3, Array($t1) # A[j] = A[j-1] move $t1, $t5 b INNER_LOOP_START INNER_LOOP_END: sw $t2, Array($t1) addi $t0, $t0, 1 # i++ b SORT_MAIN_LOOP SORT_DONE: #move $s1, $zero # set counter for # of elems printed #move $s2, $zero # set offset from Array # #print_loop: # bge $s1, $s7, print_loop_end # stop after last elem is printed # lw $a0, Array($s2) # print next value from the list # li $v0, 1 # syscall # la $a0, NL # print a newline # li $v0, 4 # syscall # addi $s1, $s1, 1 # increment the loop counter # addi $s2, $s2, 4 # step to the next array elem # # j print_loop # repeat the loop #print_loop_end: li $v0, 10 syscall