英特尔8086,无限循环

时间:2014-02-28 19:12:58

标签: assembly x86-16

我正在尝试编写一个小应用程序,其中使用系统时钟生成1-5之间的随机数,用户必须猜测数字,如果猜测正确,则会显示一条消息。

到目前为止,这是我的代码:

org 0x100


;Guess:
        MOV AH,2CH  ;DOS CALL TO GET SYSTEM CLOCK
        INT 21H     ;EXECUTE INTERRUPT 21H
        MOV AL,DL   ;RESULT RETURNED IN DL
        MOV AH,0    ;CLEAR THE HIGH BYTE
        MOV CL,20   ;GET DIVISOR
        DIV CL      ;DIVIDE VALUE IN AX BY VALUE IN CL
        MOV BL,AL   ;MOVE RESULT OF DIVISION INTO BL
        INC BL      ;INCREMENT BL TO GET NO BETWEEN 1-5

;Prompt:
        JMP START
        MSG DB "Enter a number between 1-5:$"

        START:  

        MOV DX, OFFSET MSG
        MOV AH, 09h 
        INT 21h

;User input:

        MOV AH, 1h  ;GET INPUT FROM THE KEYBOARD 
        INT 21h     ;THE CHARACTER INPUT IS STORED IN AL

;The Comparison: 

        ;MOV AX,AL   ;LOAD VALUE OF AL (USER INPUT)
        CMP AL,BL   ;COMPARE WITH BL

        JE Equal    ;IF EQUAL JUMP TO Equal
        JNE Not_Equal;IF NOT EQUAL JUMP TO Not_Equal        

        Equal:

        ABC DB "Your guess was correct!$"
        MOV DX, OFFSET ABC
        MOV AH, 09h 
        INT 21h        


        Not_Equal:

        DEF DB "Your guess was incorrect!$"
        MOV DX, OFFSET DEF
        MOV AH, 09h 
        INT 21h

我的问题是它只是进入无限循环。

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些不同的问题:

你不能混合这样的代码和数据:

    ABC DB "Your guess was correct!$"
    MOV DX, OFFSET ABC

CPU无法区分代码和数据,并会尝试执行它遇到的任何事情。将结果字符串移动到您已获得MSG字符串的相同位置。


您正在将数值与相应的ASCII字符进行比较:

    MOV AH, 1h  ;GET INPUT FROM THE KEYBOARD 
    INT 21h     ;THE CHARACTER INPUT IS STORED IN AL

如果您在终端中输入数字2,则会在'2'中获得字符AL(等于50)。要成功地将其与BL中的内容进行比较,您必须从'0'中减去AL


您将始终打印“您的猜测不正确”字符串。在打印出“你的猜测是正确的”字符串的代码之后,没有跳转到跳过下一个打印:

    ABC DB "Your guess was correct!$"
    MOV DX, OFFSET ABC
    MOV AH, 09h 
    INT 21h        

    ; Execution will simply continue here since there's nothing that changes the
    ; flow of execution.

    Not_Equal:

您没有正确终止程序。在程序结束时,您应该添加以下内容:

MOV AX,4C00h
INT 21h    ; Terminate with exit code 00h