spearman008
spearman008

Reputation: 87

Count number of lowercase letters

So I have created this program to count the number of lowercase letters in a string. The problem I am having is that when I reach the end of the string and the nl character is reached, the line beq $t0, $t1, end is not being executed; it just continues indefinitely. I'm not sure what I'm doing incorrectly.

.data
msg1: .word 0:24
.text
.globl main
main:
    addu $s0, $0, $ra
    li $v0, 8
    la $a0, msg1
    la $a1, 100
    syscall
loop:
    lb $t0, 4($a0)
    li $t1, 0x0a
    beq $t0, $t1, end
continue:
    li $t1, 'a'
    blt $t0, $t1, count
    li $t1, 'z'
    bgt $t0, $t1, count     
count:
    addi $t4, $t4, 1
    j loop
end:
    li $v0, 1
    addu $a0, $t2, $0       
    syscall 
    jr $ra

Upvotes: 0

Views: 1550

Answers (1)

markgz
markgz

Reputation: 6266

You compare 4($a0) with 0x0a on each iteration of the loop, but you never change $a0 in the loop, so you are not advancing through your string and never look at the \n at the end of the string.

There also are a few other bugs in your code.

Use this at the start of your loop:

loop:
    lb $t0, 0($a0)
    addiu $a0, $a0, 1
    ...

Upvotes: 1

Related Questions