Search code examples
assemblymipsmips32

MIPS .asciiz first and last char are always equal


Here is my simple programm, it should get the string from .data convert each char into capital and finally print the string.

.data
    s:  .asciiz     "wassup"

.text
    la  $t0, s          # get adress of text
    add $t1, $zero, $t0     # first adress

text_loop:
    lb  $t2 , 0($t1)        # first char
    beqz    $t2, end_text       # quit loop if string ends
    sub $t2, $t2, 32
    sb  $t2, s
    add $t1, $t1, 1     # next adress
    j   text_loop       # continue loop

end_text:
    
    li  $v0, 4
    la  $a0, s
    syscall

    li  $v0, 10     
    syscall     

Output: enter image description here

Only the first char is changed and it is always transformed into the last char.

Im 2 weeks into learning assembly so any help is much appreciated.


Solution

  • Looking at your code, you don't store to the same place you load.

    The line:

        sb  $t2, s
    

    is the problem

    You always store the modified character to the beginning of the string. This line should mirror the load:

        lb  $t2 , 0($t1)
    

    and become:

        sb  $t2, 0($t1)