检查MIPS中字符串中char的出现次数?

时间:2016-11-01 06:03:06

标签: assembly mips qtspim

所以我正在编写一个简单的程序,用于打印自定义字符串在自定义字符串中出现的次数。这是我的代码:

.data 
    command1:   .asciiz "Please enter a sentence "
    command2:   .asciiz "Please enter a character "
    count:      .word 10
    sentence:   .space 100
    character:  .space 4
    command3:   .asciiz " Character "
    command4:   .asciiz " appears in the sentence "
    command5:   .asciiz " times. "

.text
.globl main
main:
    li $t0, 100 #end var for loop
    li $t1, 0   #start var for loop
    li $t2, 0   #number of occurences

    la $a0, command1    #print 'please enter sentence'
    li $v0, 4
    syscall


    la $a0, sentence    #input sentence
    li $a1, 100
    li $v0, 8
    syscall

    la $a0, command2    #print 'please enter char'
    li $v0, 4
    syscall

    la $a0, character   #input character
    li $a1, 4
    li $v0, 8
    syscall

    la $s0, character
    lb $s1, ($s0)
    la $t3, sentence
    lb $a2, ($t3) #gets first char of sentence

loop:
    #addi $a2, $a2, 1
    beq $a2, $zero, end     #once reach end of char array, prints result
    beq $a2, $s1, something #if the char within sentence == comparing char
    addi $a2, $a2, 1        #increment char array
    j loop

something:              
    addi $t2, $t2, 1        #increments number of occurences of char 
end:
    la $a0, command3
    li $v0, 4
    syscall

    la $a0, character
    li $v0, 11
    syscall

    la $a0, command4
    li $v0, 4
    syscall

    la $a0 sentence
    li $v0, 4
    syscall

    #la $a0, $t2
    li $v0, 1
    move $a0, $t2
    syscall

    la $a0, command5
    li $v0, 4
    syscall

我的问题是,在我输入要与句子进行比较的字符后,程序进入无限循环($ s2注册表不断更改值)并且没有任何内容被打印。提前谢谢!

0 个答案:

没有答案