0% found this document useful (0 votes)
11 views1 page

Final Sort 2

Uploaded by

mkhamaise71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Final Sort 2

Uploaded by

mkhamaise71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

.

word 0x100 10
.word 0x101 9
.word 0x102 11
.word 0x103 8
.word 0x104 12
.word 0x105 1
.word 0x106 13
.word 0x107 14
.word 0x108 2
.word 0x109 7
.word 0x10A 3
.word 0x10B 6
.word 0x10C 15
.word 0x10D 4
.word 0x10E 16
.word 0x10F 5
add $sp, $zero, $imm, 0x100 # $sp = base address of the array (0x100)
add $s2, $zero, $imm, 16
outer_loop:
add $t2, $zero, $imm, 0 # Initialize outer loop index i = 0
add $s1, $zero, $imm, 0 # Reset swapped flag to 0

inner_loop:
add $s0, $sp, $t2 # $s0 = base address + i (address of the
current element)
lw $t0, $s0, $imm, 0 # Load element a[i] into $t0
lw $t1, $s0, $imm, 1 # Load element a[i+1] into $t1

bgt $imm, $t0, $t1, swap # If a[i] > a[i+1], jump to swap
beq $imm, $zero, $zero, no_swap # Otherwise, go to no_swap

swap:
sw $t1, $s0, $imm, 0 # Store a[i+1] at position a[i]
sw $t0, $s0, $imm, 1 # Store a[i] at position a[i+1]
add $s1, $imm, $zero, 1 # Set swapped flag to 1

no_swap:
add $t2, $imm, $t2, 1 # Increment inner loop index
blt $imm, $t2, $s2, inner_loop # Continue inner loop if i < 15

beq $imm, $s1, $zero, done # If no swaps were made, sorting is


complete
beq $imm, $zero, $zero, outer_loop # Otherwise, continue outer loop

done:
halt $zero, $zero, $zero, 0 # End program

You might also like