思路:
把十六进制(比如4位)数除以10000d(十进制数,因为十进制最大是5位--65535)
如果二位十六进制数就除以100d(因为十进制最大是三位--255)
商就是十进制的最高位
再把余数除以1000d
商就是十进制的次高位
再把余数除以100d
...
除以10d
....
除以1d
...以此类推
就得到十进制数
下面是四位十六进制数转为十进制代码:
code segment
main proc far
assume cs:code
start:
call hexibin ;16-2
call crlf ;换行
call binidec ;2-10
call crlf
jmp main ;get next input
ret
main endp
hexibin proc near
mov bx,0 ;clear BX for number
newchar:
mov ah,01h ;keyboard input
int 21h ;call DOS
sub al,30h ;ASCII to binary
jl exit ;jump if<0
cmp al,10d ;is it > 9d
jl add_to ;yes,so it's digit
;not digit(0-9),may be letter(a to f)
sub al,27h ;convert ASCII to binary
cmp al,0ah ;is it <0a hex?
jl exit ;yes,not letter
cmp al,10h ;is it > 0f hex?
jge exit ;yes,not letter
;is hex digit,add to number in BX
add_to:
mov cl,4
shl bx,cl
mov ah,0
add bx,ax
jmp newchar
exit:
ret
hexibin endp
binidec proc near
mov cx,10000d
call dec_div
mov cx,1000d
call dec_div
mov cx,100d
call dec_div
mov cx,10d
call dec_div
mov cx,1d
call dec_div
ret
dec_div proc near
mov ax,bx ;number low half
mov dx,0 ;zero out high half
div cx
mov bx,dx ;remainder into BX
mov dl,al ;quotient into DL
;print the contents of DL on screen
add dl,30h ;convert to ASCII
mov ah,02h
int 21h
ret
dec_div endp
binidec endp
crlf proc near
mov dl,0ah
mov ah,02h
int 21h
mov dl,0dh
mov ah,02h
int 21h
ret
crlf endp
code ends
end start
温馨提示:答案为网友推荐,仅供参考