Search code examples
assemblyarchitecturemipscomputer-sciencemips32

Why does register $a1 always prints a 0 in Mips architecture


I was testing some mips assembly before working on an assignment and I was trying to check if characters inside a string were inside the alphabet or not. I figured a hacky way to make it work but I would prefer if someone could explain to me why my code prints out a 0 everytime.

.data
str1: .asciiz "allo"
str2: .asciiz "a1b2"
true: .word 1
false: .word 0

.text

main:

    la $a0, str1
    move $t0, $a0

    loop:
        lb $a0, 0($t0)           # pointer on array
        beqz $a0, end            # Checks for end of array
        blt $a0, 97, non_alpha   # Is character in the alphabet
        bgt $a0, 122, non_alpha
        addi $t0, $t0, 1         # Increment pointer
        j loop      

    end:
        li $v0, 1
        lw $a1, true      # !!! This line is the issue, why $a1 print a 0
        syscall           # When I change it to $a0 it prints out 1 as it 
                          # should

        # end program
        li $v0, 10
        syscall

non_alpha: "Did not touch this yet, ignore this"

Solution

  • The service you are syscalling ($v0 = 1 = print_integer) only prints out the value in $a0. Any changes to $a1 will not affect the outcome as it has nothing to do with the service at all.

    For more information on what each service does and what argument it uses, refer to this: https://courses.missouristate.edu/KenVollmar/mars/Help/SyscallHelp.html