我的代码应该打印出一个十进制数字,但它不会,我不知道为什么。
我只是一个初学者,我已经阅读了很多建议,但我无法解决这个问题。谢谢你的反应! (要打印的数字是COUNT
)
printout proc
xor dl, dl
mov dl, OFFSET COUNT
xor bx,bx
mov bh, 0ah
mov ax,dx
div bh
mov bh,ah
mov bl,al
xor dx,dx
mov dl,bl
add dx,’0′
mov ah,02h
int 21h
xor dx,dx
mov dl,bh
add dx,’0′
mov ah,02h
int 21h
ret
endp
答案 0 :(得分:2)
要阅读COUNT的值,请使用mov dl,byte [COUNT]
或mov dl,COUNT
这取决于您使用的汇编程序。
您还应该使用xor dh,dh
代替xor dl,dl
。
您可以正确处理的最大数字是99.
您可以简化代码。
xor ah, ah
mov al, COUNT
mov bh, 0ah
div bh
mov bx,ax
mov dl,bl
add dl,’0′
mov ah,02h
int 21h
mov dl,bh
add dl,’0′
mov ah,02h
int 21h
ret
答案 1 :(得分:0)
首先,您必须将数字转换为字符串,然后您可以"打印出来"。下一个代码是用EMU8086制作的,并展示了如何操作(评论将帮助您理解):
.stack 100h
.data
num dw 10382 ;ANY NUMBER.
str db 7 dup('$') ;STRING TO STORE NUMBER.
.code
;INITIALIZE DATA SEGMENT.
mov ax, @data
mov ds, ax
;CONVERT "NUM" TO STRING. RESULT COMES BACK IN "STR".
mov ax, num
call number2string
;DISPLAY NUMBER.
mov ah, 9
mov dx, offset str ;NUMBER CONVERTED TO STRING.
int 21h
;STOP UNTIL USER PRESS ANY KEY.
mov ah,7
int 21h
;FINISH THE PROGRAM PROPERLY.
mov ax, 4c00h
int 21h
;------------------------------------------
;NUMBER TO CONVERT MUST ENTER IN AX.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING.
proc number2string
mov bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:
mov dx, 0 ;NECESSARY TO DIVIDE BY BX.
div bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.
push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.
inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
cmp ax, 0 ;IF NUMBER IS
jne cycle1 ;NOT ZERO, LOOP.
;NOW RETRIEVE PUSHED DIGITS.
mov si, offset str
cycle2:
pop dx
add dl, 48 ;CONVERT DIGIT TO CHARACTER.
mov [ si ], dl
inc si
loop cycle2
ret
endp