求汇编程序:从键盘上输入一个十六进制数,将其转换成十进制数并在显示器上显示出来

2025-01-07 08:24:49
推荐回答(1个)
回答1:

assume cs:code, ds:datas

datas segment
hex db 4 dup (0)
error db 13,10,'input error$'
datas ends

code segment
start:
mov ax, datas
mov ds, ax

mov ah, 1h
mov bx, offset hex
jmp ts0
up0:
inc bx
ts0:
cmp bx, offset hex + 4
jge next0
int 21h
mov byte ptr [bx], al
jmp up0

next0:
mov cl, 4
mov bx, offset hex
xor dx, dx

jmp ts1
up1:
inc bx
ts1:
cmp bx, offset hex + 4
jge blank
mov al, byte ptr [bx]
cmp al, '0'
jl err
cmp al, '9'
jle below_ten

cmp al, 'A'
jl err
cmp al, 'F'
jle above_ten

cmp al, 'a'
jl err
cmp al, 'f'
jg err

above_ten:
and al, 0fh
add al, 9
below_ten:
and al, 0fh
shl dx, cl
add dl, al
jmp up1

blank:
mov bx, dx

mov ah, 2h
mov dl, 13
int 21h
sub dl, 3
int 21h

xor cx, cx
mov ah, 2h
stc
next1:
rcr cx, 1
jc done
test bx, cx
jnz ones
mov dl, '0'
int 21h
jmp next1
ones:
mov dl, '1'
int 21h
jmp next1

err:
mov ah, 9h
mov dx, offset error
int 21h
done:
mov ax, 4c00h
int 21h
code ends
end start