为什么跳到错误的地方?

时间:2013-12-19 17:23:31

标签: assembly x86 conditional-statements x86-16

我编写了以下代码来测试用户输入是否介于0和9之间 不幸的是,当用户输入qsu时,它会跳转到cont。 PS:invcont是标签。

.model small
.stack
.data
    msg db 0ah,0dh,'Enter TWO odd integer values (0 to 9): $'
    invalid db 0ah,0dh,'The number is not an odd number.$'
    firstnum db ?
    secondnum db ?
    result db 0ah,0dh,'The average of the input values is: $'
.code
main proc
    mov ax,@data
    mov ds,ax
loopin:
    mov ah,09h
    lea dx,msg
    int 21h

    mov ah,01h
    int 21h

    sub al,30h
    mov firstnum,al

    sub ah,ah
    mov bl,2
    div bl
    cmp ah,0
    je inv
    jmp cont
inv:
    mov ah,09h
    lea dx,invalid
    int 21h
    jmp loopin
cont:
    mov ah,02h
    mov dl,','
    int 21h

    mov ah,01h
    int 21h

    sub al,30h
    mov secondnum,al

    sub ah,ah
    mov bl,2
    div bl
    cmp ah,0
    je inv

    sub ah,ah
    mov al,firstnum
    add al,secondnum
    mov bl,2
    div bl
    mov bh,ah

    mov ah,09h
    lea dx,result
    int 21h

    add al,30h
    mov ah,02h
    mov dl,al
    int 21h

    mov ah,02h
    mov dl,'.'
    int 21h

    sub ah,ah
    mov al,bh
    mov bl,5
    mul bl
    add al,30h

    mov ah,02h
    mov dl,al
    int 21h

    mov ah,4ch
    int 21h
main endp
end main

1 个答案:

答案 0 :(得分:0)

这应该是你的测试代码吗?

sub al,30h
mov firstnum,al

sub ah,ah
mov bl,2
div bl
cmp ah,0
je inv
jmp cont

绝对不会检查输入是否为数字。试试这个:

sub al,30h
jb notanumber
cmp al,9
ja notanumber
mov firstnum,al
[…]