Mips:Count使用嵌套函数的小写字母数

时间:2016-01-11 21:54:48

标签: assembly mips

简单地说,用户输入一个字符串......我必须使用两个函数(过程)计算小写字母的数量:

countSmallLetters 在字符串上循环的函数,每次调用函数 isSmall 来检查此字符是否小,如果是,则增加计数器(小写字母数) 1

我已经不止一次地追踪我的代码了 - 即使是在纸上 - 似乎逻辑是正确的但它打印计数器(小写字母的数量)有0值?!

有任何帮助吗?提前谢谢......

.data
str: .asciiz""

.text
main:

la $a0,str   #Enter Stirng
li $v0,8
syscall

jal countSmallLetters

move $a0,$v0    #Print Result (number of lowercase letters)
li $v0,1
syscall

#end
li $v0,10
syscall

countSmallLetters:
    subu $sp,$sp,4 
    sw $ra,0($sp)       #use stack to save tha main address(the link)

    add $v0,$zero,0 #counter (return)

    add $t2,$zero,1     #char is small
    add $t3,$zero,0     #char is capital

    loop:
    lb $a1,0($a0)
    beq $a1,$zero,backToMain    #terminator
    jal isSmall               #check if small or not
    add $a0,$a0,1
    beq $v1,$t2,increase
    j loop
    increase:
    add $v0,$v0,1
    j loop

    backToMain:
    lw $ra,0($sp)
    addu $sp,$sp,4
    jr $ra


isSmall:
    add $t0,$zero,97
    add $t1,$zero,122

    bge $a1,$t0,first   #a1>=97
    j out
    first:  
    ble $a1,$t1,second  #a1<=122
    j out
    second:
    add $v1,$zero,1         #char is small
    out:
    add $v1,$zero,0         #char is capital
    jr $ra

1 个答案:

答案 0 :(得分:0)

您将$v1设置为1,但您会立即使用0覆盖它。您可以在jr $ra标签前添加out:,或在最初添加零$v1,例如:

second:
    add $v1,$zero,1         #char is small
    jr $ra
out:
    add $v1,$zero,0         #char is capital
    jr $ra