整数的长度为9,这样就超过了16位,单纯用8086的运算已经不能满足,加法的进位就会丢失,
而乘除法很可能会出现overflow.
所以要用到32位寄存器,这样就用到386指令.
如果不用386指令,也可以用两个寄存器对高16位和低16位分别进行操作.
下面是用386指令的源代码
ENTER MACRO
MOV DL,0AH
MOV AH,2H
INT 21H
MOV DL,0DH
MOV AH,2H
INT 21H
ENDM
.model hex
.386
.data
msg db 'Input numbers (length<=9):$'
.stack
db 100 dup(?)
.code
main proc far
;*********************************
start:
push ds
mov ax,0
push ax
mov ax,@data
mov ds,ax
mov dx,offset msg
mov ah,9
int 21h
call rec
enter
call tohex
enter
call dec1
enter
ret
main endp
;-----------------------------------
rec proc near ;子程序rec是用来接收一个长度小于9的整数
mov ebx,0 ;并把它放入ebx中
newnum:
mov ah,01
int 21h
sub al,30h
jl exit
cmp al,9d
jg exit
cbw
xchg eax,ebx
mov ecx,10d
mul ecx
xchg eax,ebx
add ebx,eax
jmp newnum
exit:
ret
rec endp
;------------------------------------
tohex proc near ;子程序tohex是把ebx中的数用16进制输出
mov ch,8
rotate:
mov cl,4
rol ebx,cl
mov al,bl
and al,0fh
add al,30h
cmp al,3ah
jl print
add al,7h
print:
mov dl,al
mov ah,2h
int 21h
dec ch
jnz rotate
ret
tohex endp
;----------------------------------
dec1 proc near ;dec1是转化为十进制输出
mov ecx,100000000d
call todec
mov ecx,10000000d
call todec
mov ecx,1000000d
call todec
mov ecx,100000d
call todec
mov ecx,10000d
call todec
mov ecx,1000d
call todec
mov ecx,100d
call todec
mov ecx,10d
call todec
mov ecx,1d
call todec
ret
;--------------------------------
todec proc near
mov eax,ebx
mov edx,0
div ecx
mov ebx,edx
mov dx,ax
add dl,30h
mov ah,02h
int 21h
ret
todec endp
dec1 endp
;----------------------------
end main
本文地址:http://com.8s8s.com/it/it26143.htm