Search code examples
assemblyconvertersmipstoupper

Implementing toUpper function in MIPS


So I'm trying to implement a kind of toUpper function in MIPS. The function has access to two variables: $a0, which is the starting address of a string of characters, and $a1 which is the length of the string. I'm trying to loop through the string and convert it to uppercase. Does anyone know where I'm going wrong?

I think I'm having trouble actually loading the first character from the starting address into a variable.

to_upper:

    li $t0, 0 #initialize counter  
    li $t1, 0

Loop: 
    addi $t0, $t0, 1

    sgt $t5, $t0, $a1   
    beqi $t5, 1, Done   

    slti $t5, $t1, 97
    beqi $t5, 1, Loop

    sgti $t5,$t1, 122
    beqi $t5, 1, Loop

    subi $t1, $t1, 32

j Loop

    Done:
    jr $ra 

Solution

  • It is better to place

    addi $t0, $t0, 1
    

    at the end of the loop.

    So, like this:

    ...
    Loop:
        addi $t5, $a0, $t0
        lb $t6, 0($t5)
        ...set $t6 to upper...
        sb $t6, 0($t5)
        addi $t0, $t0, 1
        blt $t0, $a1, Loop
    Done:
    ...