主题:又找到一个程序,一步一步的读
从键盘接收一个4位的16进制数,并在终端上显示与它等值的2进制数
data segment
buf db 4,?
ascstg db 10 dup(?)
integer dw 0
promt db 'Input hexadecimal(4):$'
dsperr db 0dh,0ah,'error!no hexadecimal!$'
data ends
code segment use16
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov dx,offset promt
mov ah,09h
int 21h
mov dx,offset buf
mov ah,oah
int 21h
mov cl,4
lea si,ascstg
mov al,[si]
cld
xor dx,dx
mov bl,0
mov ch,buf[1]
cmp ch,0
jz exit
cmp al,'+'
jnz next
inc si
dec ch
jmp conv
next: cmp al,'-'
jnz conv
inc si
dec ch
mov bl,0ffh
conv: cmp ch,0c
jz store
lodsb
cmp al,'0'
jb error
cmp al,'9'
ja next1
sub al,30h
jmp short rotate
next1: call a_to_f
jc error
rotate: shl dx,cl
or dl,al
dec ch
jmp short conv
store: cmp ,bl,0
jz lp
neg dx
lp: mov integer,dx
exit: mov dx,offset dsperr
mov ah,09h
int 21h
jmp exit
a_to_f proc
cmp al,'a'
jb nohex
cmp al,'f'
ja next2
sub al,37h
jmp hex
next2: cmp al,'a'
jb nohex
cmp al,'f'
ja nohex
sub al,547h
hex: clc
ret
nohex: stc
ret
a_to_f endp
code ends
end start
data segment
buf db 4,?
ascstg db 10 dup(?)
integer dw 0
promt db 'Input hexadecimal(4):$'
dsperr db 0dh,0ah,'error!no hexadecimal!$'
data ends
code segment use16
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov dx,offset promt
mov ah,09h
int 21h
mov dx,offset buf
mov ah,oah
int 21h
mov cl,4
lea si,ascstg
mov al,[si]
cld
xor dx,dx
mov bl,0
mov ch,buf[1]
cmp ch,0
jz exit
cmp al,'+'
jnz next
inc si
dec ch
jmp conv
next: cmp al,'-'
jnz conv
inc si
dec ch
mov bl,0ffh
conv: cmp ch,0c
jz store
lodsb
cmp al,'0'
jb error
cmp al,'9'
ja next1
sub al,30h
jmp short rotate
next1: call a_to_f
jc error
rotate: shl dx,cl
or dl,al
dec ch
jmp short conv
store: cmp ,bl,0
jz lp
neg dx
lp: mov integer,dx
exit: mov dx,offset dsperr
mov ah,09h
int 21h
jmp exit
a_to_f proc
cmp al,'a'
jb nohex
cmp al,'f'
ja next2
sub al,37h
jmp hex
next2: cmp al,'a'
jb nohex
cmp al,'f'
ja nohex
sub al,547h
hex: clc
ret
nohex: stc
ret
a_to_f endp
code ends
end start