1

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 
Connor Black
  • 6,921
  • 12
  • 39
  • 70

2 Answers2

1

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:
...
Patrik
  • 2,695
  • 1
  • 21
  • 36
0

$a0 is not referenced anywhere in this code. You need to load values from $a0 using lb, modify them, and then store them back.

Variable Length Coder
  • 7,958
  • 2
  • 25
  • 29