sorting - MIPS Selection Sort Not Working

I am trying to write a selection sort algorithm in MIPS but am having problems. I have tested my swap and findSmallest functions individually and they appear to be working but when I run the code I get some addresses (I'm assuming) in the output.

`` .data.align 2values: .word 12, 15, 13, 2, 25, 3, 5valueCount: .word 7initialPrint: .asciiz "\Unsorted Array: \n"donePrint: .asciiz "\n Sorted Array: \n"comma: .asciiz ", ".text.globl mainmain: # Beginning stuffla \$a0, initialPrint # load initialPrint string into a0li \$v0, 4 # set code (4) to print stringsyscall # print initialPrintla \$s6, valuesla \$s7, valueCountlw \$s7, 0(\$s7) # extract valueCount word from s7move \$a0, \$s6 # move values into \$a0 for function callmove \$a1, \$s7 # move valueCount into \$a1 for function calljal printList # print initial arrayli \$s1, 0 # index = 1selectionSort: # Middle stuffbge \$s1, \$s7, endPrintmove \$a0, \$s6 # move values into \$a0 for function callmove \$a1, \$s7 # move valueCount into \$a1 for function callmove \$a2, \$s1 # move index into \$a2 for function calljal findSmallest # Let's get this show on the roadmove \$a0, \$v0 # move smallestIndex into a0move \$a1, \$s1 # move currentndex into \$a1move \$a2, \$s6 # move values into \$a2jal swap # swapmove \$s6, \$v0 # move swapped array into \$s6addi \$s6, \$s6, 4 # next position in arrayaddi \$s1, \$s1, 1 # index++j selectionSort# Ending stuffendPrint: la \$a0, donePrint # load donePrint string into a0li \$v0, 4 # set code (4) to print stringsyscall # print donePrintmove \$a0, \$s6 # move values into \$a0 for function callmove \$a1, \$s7 # move valueCount into \$a1 for function calljal printList # print initial arrayli \$v0, 10 # load end callsyscall # end####################################################################################################### findSmallest Function ############################################################################################################################################## takes in array, current index, and valueCount## examines all elements from n-index through n## outputs index of lowest valuefindSmallest: ## Begin Functionaddi \$sp, \$sp, -24 # preserve state before function callsw \$ra, 0(\$sp)sw \$s0, 4(\$sp)sw \$s1, 8(\$sp)sw \$s2, 12(\$sp)sw \$s3, 16(\$sp)sw \$s4, 20(\$sp)sw \$t1, 24(\$sp)## s0 = values## s1 = valueCount## s2 = index## s3 = smallestIndex## s4 = smallestValue## t1 = currentValue## Function Bodymove \$s0, \$a0 # move values into s0move \$s1, \$a1 # move valueCount into s1move \$s2, \$a2 # move index into s2lw \$s4, 0(\$s0) # load first wordaddi \$s0, \$s0, 4 # move to next wordaddi \$s2, \$s2, 1floop: bge \$s2, \$s1, endLoop # if index >= valueCount then donelw \$t1, 0(\$s0) # load currentValueble \$s4, \$t1, skip # if smallestValue < currentValue then skipmove \$s4, \$t1 # set smallestValue = currentValuemove \$s3, \$s2 # set smallestIndex = currentIndexskip:addi \$s0, \$s0, 4 # iterate to next word in valuesaddi \$s2, \$s2, 1 # index++j floop # repeat loopendLoop: move \$v0, \$s3# End Function Stufflw \$ra, 0(\$sp) # restore state before function calllw \$s0, 4(\$sp)lw \$s1, 8(\$sp)lw \$s2, 12(\$sp)lw \$s3, 16(\$sp)lw \$s4, 20(\$sp)lw \$t1, 24(\$sp)addi \$sp, \$sp, 24jr \$ra####################################################################################################### swap Function ####################################################################################################################################################swap: # Begin Functionaddi \$sp, \$sp, -32 # preserve state before function callsw \$ra, 0(\$sp)sw \$s0, 4(\$sp)sw \$s1, 8(\$sp)sw \$t1, 12(\$sp)sw \$t2, 16(\$sp)sw \$t3, 20(\$sp)sw \$t4, 24(\$sp)sw \$s6, 28(\$sp)# main functionmove \$s0, \$a0 # move smallestIndex into functionmove \$s1, \$a1 # move targetIndex into functionmove \$s6, \$a2sll \$s0, \$s0, 2add \$t1, \$s0, \$s6lw \$t2, 0(\$t1) # get the value from the array cellsll \$s1, \$s1, 2add \$t3, \$s1, \$s6lw \$t4, 0(\$t3) # get the value from the array cellsw \$t2, 0(\$t3)sw \$t4, 0(\$t1)move \$v0, \$s6# End Function # restore state before function calllw \$ra, 0(\$sp)lw \$s0, 4(\$sp)lw \$s1, 8(\$sp)lw \$t1, 12(\$sp)lw \$t2, 16(\$sp)lw \$t3, 20(\$sp)lw \$t4, 24(\$sp)lw \$s6, 28(\$sp)addi \$sp, \$sp, 32jr \$ra####################################################################################################### printList Function ################################################################################################################################################ a0 - values array (address)# a1 - valueCount (word)# s6 - values# s7 - valueCount (word)printList:## Setup## Begin Functionaddi \$sp, \$sp, -16 # allocate stack space for 3 valuessw \$ra, 0(\$sp) # store return addersw \$s6, 4(\$sp)sw \$s7, 8(\$sp)sw \$s0, 12(\$sp)move \$s6, \$a0move \$s7, \$a1li \$s0, 1 # index = 1print:lw \$a0, 0(\$s6)li \$v0, 1 # load output callsyscall # output valuela \$a0, comma # load comma string into a0li \$v0, 4 # set code (4) to print stringsyscall # print initialPrintaddi \$s6, \$s6, 4addi \$s0, \$s0, 1ble \$s0, \$s7, print# End Function Stufflw \$ra, 0(\$sp) # store return adderlw \$s6, 4(\$sp)lw \$s7, 8(\$sp)lw \$s0, 12(\$sp)addi \$sp, \$sp, 16jr \$ra``

My output:

``Unsorted Array:12, 15, 13, 2, 25, 3, 5, soSorted Array:25, 1684370546, 13, 3, 540703073, 1869835861, 1953656659,``

It has to be something with the way the array is being stored because there are memory addresses in the output.

I found a number of issues in your code:

``````move    \$a0, \$v0        # move smallestIndex into a0
move    \$a1, \$s1        # move currentndex into \$a1
move    \$a2, \$s6        # move values into \$a2

jal swap            # swap
``````

Here you're passing a couple of indices that are relative to the beginning of the array, but `\$s6` is incremented at the end of each iteration, so it will only point to the beginning of the array during the very first iteration. Instead of `move \$a2, \$s6` you should use `la \$a2, values`.

``````move    \$s6, \$v0        # move swapped array into \$s6
``````

This should be removed altogether.

``````move    \$a0, \$s6        # move values into \$a0 for function call
move    \$a1, \$s7        # move valueCount into \$a1 for function call
jal printList
``````

Here you should place the base address of the array in `\$a0`, so it should be `la \$a0, values` instead of `move \$a0, \$s6`.

And in `findSmallest` you don't seem to initialize `\$s3` for the case where the first element is the smallest one.