0 Replies - 1220 Views - Last Post: 01 December 2010 - 10:08 PM

#1 Shabu22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-February 10

MIPS - help with finding smallest character in array

Posted 01 December 2010 - 10:08 PM


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.

The main part of the program is already completed, and my code is being tested with it.

Here is my code:



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


        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

        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


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


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!

Thank you!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1