查找字符串中字符出现的次数 (mips 32)

Finding number of character occurrences in string (mips 32)

我正在编写一个代码,将一个字符串和一个来自用户的字符作为输入,然后查找字符串中该字符的出现次数。 这是我的尝试:

.data 
string:      .space 100                    # allc space for string 
.text
main: 
la $a0, string                             # input string
li $a1, 100                                # maximum size of string
li $v0, 8                                  # represents reading string
syscall                                    # call system
la $s0, 0($a0)                             # $s0 contains address of first element of the string
    
li $v0, 12                                 # 12 represents reading character
syscall                                    # call system  
move $s2, $v0                              # $s2= character
li $s1, 100                                # $s1= maximum size of string
                       
li  $t4, 0                                 # count (number of occurrances)
li  $t0, 0                                 # i(index)
loop:
bge $t0, $s1, print                        # i>= string length, exit                            
add $t1, $s0, $t0                          # &A[i]
lb  $t2, 0($t1)                            # A[i]
bne $t2, $s2, skip                         # skip increasing the counter if item does not equal               
add $t4, $t4, 1                            # otherwise increment the counter    
skip: 
add $t0, $t0, 1                            # increment index
j  loop                                    # go back to loop
print:                                     # print the result
addi $v0, $t4, 0                           # $v0= $v4= number of occurrences
li $v0,1                                   # 1 represents printing integer                   
syscall                                    # call system    
# Terminate the program
li $v0, 10                                 # 10 represents exit
syscall                                    # call system

但是我的代码不能正常工作。它打印一个非常大的数字而不是出现的次数。我什至检查了它是否有与分配的 space 大小相同的字符串。但它仍然给出错误的结果。这个问题与字符串长度或其他问题有关吗?请帮我调试一下。

试试这个代码:

        .data 
    string:      .space 100                    # allc space for string 
    .text
    main: 
    la $a0, string                             # input string
    li $a1, 100                                # maximum size of string
    li $v0, 8                                  # represents reading string
    syscall                                    # call system

    li $v0, 12                                 # 12 represents reading character
    syscall                                    # call system  
    move $s2, $v0                              # $s2= character
    li $s1, 100                                # $s1= maximum size of string
               
    li  $t4, 0                                 # count (number of occurrances)
    li  $t0, 0                                 # i(index)

    bge $t0, $s1, print                        # i>= string length, exit    
    loop:                        

    lb  $s0,($a0)                              # the first character of string into $s0 
    bne $s0, $s2, skip                         # skip increasing the counter if item does not equal               
    add $t4, $t4, 1                            # otherwise increment the counter    
    skip: 
    addiu $a0, $a0, 1                            # increment index
    beq $s0,0,print                             # go to print label if $s0=0 so $s0 ='[=10=]' 
    j  loop                                    # go back to loop
    print:                                     # print the result
    #addi $v0, $t4, 0                           # $v0= $v4= number of occurrences
    li $v0,1                                   # 1 represents printing integer                   
    move $a0,$t4                               # $a0=$t4 = number of occurances of character
    syscall                                    # call system    
    # Terminate the program
    li $v0, 10                                 # 10 represents exit
    syscall                                    # call system