【子程序】将word型数据转为十进制字符串

image

assume cs:code

data segment

	db 10 dup (0)

data ends

code segment

start: 
	mov ax,12666
	mov bx,data
	mov ds,bx
	mov si,0
	call dtoc

	mov dh,8
	mov dl,3
	mov cl,2
	call show_str

	mov ax,4c00h
	int 21h

; ----------------------------------

dtoc:
	push ax
	push cx
	push dx
	push si
	push di
	
	mov di,0
	mov bx,10

dtoc_div:
	mov cx,ax
	jcxz dtoc_div_ok

	mov dx,0                 ; 12666 / 10
	mov cx,10
	call divdw

	mov dx,cx                ; remainder
	add dx,30h               ; turn hex number to ASCII
	push dx

	inc di                   ; record count of numbers
	jmp short dtoc_div

dtoc_div_ok:
	mov cx,di

write_ascii:
	pop dx                   ; dl: remainder
	mov ds:[si],dl
	inc si
	loop write_ascii

	mov byte ptr ds:[si],0   ; last 0

	pop di
	pop si
	pop dx
	pop cx
	pop ax
	ret

; ----------------------------------

show_str:
	; protect the scene
	push ax
	push dx
	push es
	push cx
	push si
	push di
	
	; ptr of str: 0b800h:dh * 160 + dl * 2
	mov ah,0
	mov al,160
	mul dh
	mov dh,0
	add ax,dx
	add ax,dx
	mov di,ax
	mov ax,0b800h
	mov es,ax

	; tmp of cl
	mov dl,cl

	mov ch,0

show_one:		
	mov cl,ds:[si]
	jcxz show_ok
	mov al,ds:[si]
	mov es:[di],al
	mov es:[di+1],dl
	inc si
	add di,2
	jmp short show_one

show_ok:		
	; restore the secen
	pop di
	pop si
	pop cx
	pop es
	pop dx
	pop ax
	ret

; ---------------------------------

divdw:
	push bx

	; high 16 bit: int(H / N) * 65536
	; low 16 bit:  (rem(H / N) * 65536 + L) / N

	push ax
	mov ax,dx
	mov dx,0
	div cx
	mov bx,ax   ; bx: high 16 bit

	pop ax
	div cx      ; ax: low 16 bit
	mov cx,dx   ; cx: remainder
	mov dx,bx   ; dx: high 16 bit

	pop bx
	ret

code ends

end start
posted @ 2022-05-31 13:58  moon_orange  阅读(85)  评论(0编辑  收藏  举报