I wrote the following code to test if the user input is between 0 and 9.
Unfortunately, when the user enters q or s or u, it will jump to cont.
PS: inv and cont are labels.

.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
有帮助吗?

解决方案

Is this supposed to be your testing code?

sub al,30h
mov firstnum,al

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

There is absolutely no check for the input being a number. Try this:

sub al,30h
jb notanumber
cmp al,9
ja notanumber
mov firstnum,al
[…]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top