问题描述:

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 2

values: .word 12, 15, 13, 2, 25, 3, 5

valueCount: .word 7

initialPrint: .asciiz "\Unsorted Array: \n"

donePrint: .asciiz "\n Sorted Array: \n"

comma: .asciiz ", "

.text

.globl main

main: # Beginning stuff

la $a0, initialPrint # load initialPrint string into a0

li $v0, 4 # set code (4) to print string

syscall # print initialPrint

la $s6, values

la $s7, valueCount

lw $s7, 0($s7) # extract valueCount word from s7

move $a0, $s6 # move values into $a0 for function call

move $a1, $s7 # move valueCount into $a1 for function call

jal printList # print initial array

li $s1, 0 # index = 1

selectionSort: # Middle stuff

bge $s1, $s7, endPrint

move $a0, $s6 # move values into $a0 for function call

move $a1, $s7 # move valueCount into $a1 for function call

move $a2, $s1 # move index into $a2 for function call

jal findSmallest # Let's get this show on the road

move $a0, $v0 # move smallestIndex into a0

move $a1, $s1 # move currentndex into $a1

move $a2, $s6 # move values into $a2

jal swap # swap

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

addi $s6, $s6, 4 # next position in array

addi $s1, $s1, 1 # index++

j selectionSort

# Ending stuff

endPrint: la $a0, donePrint # load donePrint string into a0

li $v0, 4 # set code (4) to print string

syscall # print donePrint

move $a0, $s6 # move values into $a0 for function call

move $a1, $s7 # move valueCount into $a1 for function call

jal printList # print initial array

li $v0, 10 # load end call

syscall # end

##########################################################################################

############# findSmallest Function ##################################################

##########################################################################################

## takes in array, current index, and valueCount

## examines all elements from n-index through n

## outputs index of lowest value

findSmallest: ## Begin Function

addi $sp, $sp, -24 # preserve state before function call

sw $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 Body

move $s0, $a0 # move values into s0

move $s1, $a1 # move valueCount into s1

move $s2, $a2 # move index into s2

lw $s4, 0($s0) # load first word

addi $s0, $s0, 4 # move to next word

addi $s2, $s2, 1

floop: bge $s2, $s1, endLoop # if index >= valueCount then done

lw $t1, 0($s0) # load currentValue

ble $s4, $t1, skip # if smallestValue < currentValue then skip

move $s4, $t1 # set smallestValue = currentValue

move $s3, $s2 # set smallestIndex = currentIndex

skip:

addi $s0, $s0, 4 # iterate to next word in values

addi $s2, $s2, 1 # index++

j floop # repeat loop

endLoop: move $v0, $s3

# End Function Stuff

lw $ra, 0($sp) # restore state before function call

lw $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, 24

jr $ra

##########################################################################################

############# swap Function ##########################################################

##########################################################################################

swap: # Begin Function

addi $sp, $sp, -32 # preserve state before function call

sw $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 function

move $s0, $a0 # move smallestIndex into function

move $s1, $a1 # move targetIndex into function

move $s6, $a2

sll $s0, $s0, 2

add $t1, $s0, $s6

lw $t2, 0($t1) # get the value from the array cell

sll $s1, $s1, 2

add $t3, $s1, $s6

lw $t4, 0($t3) # get the value from the array cell

sw $t2, 0($t3)

sw $t4, 0($t1)

move $v0, $s6

# End Function # restore state before function call

lw $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, 32

jr $ra

##########################################################################################

############# printList Function #####################################################

##########################################################################################

# a0 - values array (address)

# a1 - valueCount (word)

# s6 - values

# s7 - valueCount (word)

printList:

## Setup

## Begin Function

addi $sp, $sp, -16 # allocate stack space for 3 values

sw $ra, 0($sp) # store return adder

sw $s6, 4($sp)

sw $s7, 8($sp)

sw $s0, 12($sp)

move $s6, $a0

move $s7, $a1

li $s0, 1 # index = 1

print:

lw $a0, 0($s6)

li $v0, 1 # load output call

syscall # output value

la $a0, comma # load comma string into a0

li $v0, 4 # set code (4) to print string

syscall # print initialPrint

addi $s6, $s6, 4

addi $s0, $s0, 1

ble $s0, $s7, print

# End Function Stuff

lw $ra, 0($sp) # store return adder

lw $s6, 4($sp)

lw $s7, 8($sp)

lw $s0, 12($sp)

addi $sp, $sp, 16

jr $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.

相关阅读:
Top