I have made the inner loop which is doing comparison and swapping but i am having a difficulty in implementing outer loop which will run according to the number of elements.
.data
Arr: .word 5, 4, 3, 2, 1
.text
.globl main
main:
la $a0, Arr # Pass the base address of the array Arr to input argument register $a0
addi $a1, $zero, 5 # initialze the value of sorting loop ($al=5)
li $t1, 0 # initialize the value of outer loop (t1=0)
sort:
lw $s1, 0($a0) # Load first element in s1
lw $s2, 4($a0) # Load second element in s2
bgt $s1, $s2, swap # if (s1 > s2) go to swap
sorting_loop:
addiu $a0, $a0, 4 # move pointer to next element
subiu $a1, $a1, 1 # decrement the value of inner loop ($a1)
bgtz $a1, sort # loop till value of inner loop is not equal to zero
j end
swap:
sw $s1, 4($a0) # put value of [i+1] in s1
sw $s2, 0($a0) # put value of [i] in s2
j sorting_loop
end: # End the program
li $v0, 10
syscall
Your inner pass looked pretty good, but [I think] it was going one past the end with the
4($a0)
(e.g. for an array of 5, the loop count must be 4). I've fixed that.I've added the outer loop. Please excuse the gratuitous style cleanup, but it's how I could understand your logic before adding to it. I renamed some of your labels so they would make more sense to me.
When I added the outer loop, I converted your inner one into a function called by the outer loop [seemed cleaner that way].
I added two optimizations: decrementing the pass count and the "early escape" flag [if a given pass makes no swaps].
I've also added a number of annotations. The code may not be perfect, but I think it will help get you closer.