程序的运行时异常以查找单词的出现

问题描述 投票:0回答:1

在这个程序中,我试图在文本中找到“how”这个词的出现,这个词在开头是硬编码的“你好吗”。但由于某些原因,当我运行该程序时,它不会停止并且不打印任何内容。所以我只是想知道谁能告诉我哪里出错了?提前致谢

text: .ascii "how are you"
word: .ascii "how"
    .text
    .globl main
main:
Search:#search the occurrence of th word 
        li $s0, 0               # pointer for the text
        li $s1, 0               # pointer for the word 
        li $s7, 0               # counter for occrence

compare:lb $t0, text($s0)        
        addi $s0, $s0, 1
        lb $t1, word($s1)
        addi $s1, $s1, 1
        bne $t0, $t1, next_word # compare next word 
        beq $t6, $s1, bingo     # t6 is the length of the keyword
        j compare               # keep comparing 
bingo:    
        addi $s7, $s7, 1        # occrence + 1    
next_word:     
        li $s1, 0               # refresh the keyword    
loop:   lb $t0, text($s0)    
        addi $s0, $s0, 1    
        bne $t0, 32, loop    
        beq $t7, $s0, print_result     # we have searched the all text 
        j compare 

print_result: 
        la $a0, ($s7)
        li $v0, 1
        syscall

        li $v0, 10
        syscall
assembly mips mars-simulator
1个回答
-1
投票

程序执行将导致两者之间的无限循环

loop:   lb $t0, text($s0)    
        addi $s0, $s0, 1    
        bne $t0, 32, loop    

因为$ t0在第一次跳回'比较'之后保持为0。使用Mars中包含的逐步调试程序。也许尝试加载你的价值观

la $a0, text
lb $t0, 0($a0)

代替

lb $t0, text($s0)  
© www.soinside.com 2019 - 2024. All rights reserved.