【问题标题】:Infinite loop when I tried to make a word counter当我尝试制作单词计数器时出现无限循环
【发布时间】:2014-10-18 09:38:17
【问题描述】:

我试图创建一个单词计数器(计算字符串中有多少个单词),我使用 jmp 指令返回到 WORDCOUNT 标签,但问题是它不断产生无限环形。当我在ADDCOUNTER 之前添加jmp WORDCOUNT 时,它会形成一个无限循环。这可能是什么原因?如果有人可以向我解释,我将不胜感激。 :)

; count the number of words in the string
        mov ax, 0000 
        mov bx, 0000 ; will contain the data
        mov cx, 0000 ; will count the number of words
        mov dx, 0000 ; erase all content to 0000
        lea bx, USERSTRING ; bx will contain the user string input
WORDCOUNT:
        mov al, [bx] ; al will contain bx data inside
        cmp al, "$" ; check if it is the end of the string already
        je ENDWORDCOUNT ; get out of the loop if done
        cmp al, " " ; if the value has space 20h
        je ADDCOUNTER ; then go to ADDCOUNTER to add
        inc bx ; hop to the next address one at a time
        jmp WORDCOUNT
    ADDCOUNTER:
            inc cx ; add cx with 1 because it means there is space
            jmp WORDCOUNT

ENDWORDCOUNT:
        call TODECIMAL
        mov cx, ax ; ax transfers the decimal version here
        mov word ptr HOWMANYWORDS, cx ; this one will be turned into a decimal dude help me here huhuhu
        lea dx, HOWMANYWORDS
        call PRINTF

【问题讨论】:

    标签: loops assembly word infinite tasm


    【解决方案1】:

    当你找到一个空格字符时,你会跳转到ADDCOUNTER,在那里你会增加cx,但不会增加bx,所以当你跳回WORDCOUNT时,你会再次读取相同的字符(即空格) ,等等,直到永远。
    您还需要在 ADDCOUNTER 的情况下使用 inc bx

    【讨论】:

    • 我会在mov al,[bx]之后增加bx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 2014-09-14
    • 2016-07-31
    • 1970-01-01
    相关资源
    最近更新 更多