Question

I'm starting with mips32 and I'm getting stuck when trying to get a letter from a string to print it. The code should get the string, print it character by character and when it finds an i print iiing.

.data
msg: .asciiz "testing"
i: .asciiz "iiing"

.text

.globl main

main:           
    la $t0, msg
    la $t1, i
    li $t2, 0
loop:
     bneq $t0, 105, end #$t0=i?
     lb $a0, ($t0)
     li $v0, 4
     syscall
     addi $t0, $t0, 1
     b loop

end:
     move $a0, $t1
     li $v0, 4
     syscall

Where is the problem?

Was it helpful?

Solution

You have a few problems.

  1. You are comparing $t0, which is the address of the current character, not the character itself. Move that test below the lb line and test against $a0.

  2. 105 in ASCII is E, not i. Try 151 (or to be more normal, 0x69).

  3. You want to compare with beq, not bneq.

  4. Inside the loop, you should use syscall 11, which prints a single character, rather than the current syscall 4 you're using, which prints a string.

  5. Your program doesn't make an exit syscall (10) at the end.

You can look at this link for a list of syscalls.

Here's a complete working program for reference:

    .data

msg: .asciiz "testing"
i:   .asciiz "iiing"

    .text
    .globl main

main:           
    la $t0, msg
    la $t1, i
    li $t2, 0

loop:
    lb $a0, ($t0)
    beq $a0, 0x69, end
    li $v0, 11
    syscall
    addi $t0, $t0, 1
    b loop

end:
    move $a0, $t1
    li $v0, 4
    syscall
    li $v0, 10
    syscall
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top