编写程序:将键盘接收到的四位十六进制数转换为等值的二进制数,再显示在终端(显示器)上。

2024-12-03 15:52:59
推荐回答(3个)
回答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

回答2:

display equ 2h
key_in equ 1h
doscall equ 21h
hexidec segment
main proc far
assume cs:hexidec
start: push ds
sub ax,ax
push ax
call hexibin
call crlf
call disp
call crlf
jmp start
ret
main endp
hexibin proc near
mov di,4
mov bx,0
newchar:mov ah,key_in
int doscall
sub al,30h
jl newchar
cmp al,9d
jle add_to
sub al,27h
cmp al,0ah
jl newchar
cmp al,0fh
jg newchar
add_to: mov cl,4
shl bx,cl
mov ah,0
add bx,ax
dec di
jnz newchar
ret
hexibin endp
disp proc near
mov cx,16
rept:
rol bx,1
mov dl,bl
and dl,1
add dl,30h
mov ah,2
int 21h
loop rept
ret
disp endp
crlf proc near
mov dl,0ah
mov ah,display
int doscall
mov dl,0dh
mov ah,display
int doscall
ret
crlf endp
hexidec ends
end start

回答3:

http://zhidao.baidu.com/question/39024361.html