Jtibs 0 Newbie Poster

Hello,

I am trying to write a MIPS program that uses a function call to return the index of the smallest character within an array.

I have my code set up in a way I thought would be correct, but when I try to test it seems to not even be close to working.

The function takes two parameters from main: $a0 = the address of the phrase
$a1 = the address of the last non nul character

Here is my code:

.text

findSmallPos:

addiu $sp, $sp, -24     # allocate stack space -- default of 24 here
sw    $fp, 0($sp)       # save caller's frame pointer
sw    $ra, 4($sp)       # save return address
addiu $fp, $sp, 20      # setup main's frame pointer

addi $t0, $t0, $zero    # index = 0
lb $t3, 0($a1)          # $t3 = address of last non nul char


findSmallPosLoopBegin:

        lb $t1, 0($a0)           # $t1 = current char

        beq $t1, $zero, findSmallPosLoopEnd     # if the current char is null, loop is over     

        lb $t4, 1($a0)          # $t4 = the next char
        slt $t5, $t1, $t4       # is the current char less than the next char?
        bne $t5, $zero, below   # if the current char is less than the next char branch 

        addi $a0, $a0, 1        # $a0++
        addi $t0, $t0, 1        # index ++

        j findSmallPosLoopBegin

below:
        sb $t1, 1($a0)         # swaps the place of the smaller char with the larger char       
        sb $t4, 0($a0)
        addi $a0, $a0, 1        # $a0++
        addi $t0, $t0, 1        # index++

        j findSmallPosLoopBegin

findSmallPosLoopEnd:

add $v0, $t0, 0         # $v0 = index of smallest character


findSmallPosDone:
lw    $ra, 4($sp)     # get return address from stack
lw    $fp, 0($sp)     # restore the caller's frame pointer
addiu $sp, $sp, 24    # restore the caller's stack pointer
jr    $ra             # return to caller's code

Any help would be greatly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.