dseg segment
Buf1 db 4, ?, 4 dup( ? )
Ermsg db 'Input error, input again!'拦誉橡, 0ah, 0dh, '$'
flag db 0
num1 db ?
dseg ends
sseg segment stack
dw 512 dup( ? )
sseg ends
cseg segment
assume ds:dseg, cs:cseg, ss:sseg
BEGIN:
mov ax, dseg
mov ds, ax
call inputnum
mov num1, bl
call inputnum
add bl, num1
mov al, bl
call dectobinary
mov ah, 04ch
int 21h
inputnum proc
Input:
mov ah, 1
int 21h
mov bl, al
mov ah, 2
mov dl, 0ah
int 21h
mov ah, 2
mov dl, 0dh
int 21h
cmp bl, 'f'
jg InputError
cmp bl, 'a'
jl LessThanLa
sub bl, 87
jmp InputDone
LessThanLa:
cmp bl, 'F'
jg InputError
cmp bl, 'A'
jl LessThanUa
sub bl, 55
jmp InputDone
LessThanUa:
cmp bl, '9'
jg InputError
cmp bl, '0'
jl InputError
sub bl, 48
jmp InputDone
InputError:
lea dx, Ermsg
mov ah, 9
int 21h
jmp Input
InputDone:
ret
inputnum endp
dectobinary proc
push cx
mov flag, 0
lea si, Buf1
mov dl, 10
mov cl, 10
Loop1:
xor ah, ah
div dl
cmp al, 0
jne Next2
cmp flag, 0
je Next3
Next2:
mov flag, 1
mov [si], al
add byte ptr[si], '0'
inc si
Next3:
push ax
mov ax, 0
mov al,dl
div cl
mov dl, al
pop ax
mov al, ah
cmp dl, 0
jne Loop1
mov byte ptr[si], ' '
mov byte ptr[si+1], '$'
lea dx, Buf1
mov ah, 9
int 21h
pop cx
ret
dectobinary endp
cseg ends
end BEGIN
说明:
先输入一个字母,合法的范围是:0~9、A~F、a~f。如果合法,则接简旁着输入第二个。任何一个输入不虚敏合法,会提示重新输入。两个字母都合法后,输出结果。
MASM6.15+DEBUG,已测通过。