函数打印 Ascii 等价物而不是将整数打印为字符串

Function is printing Ascii equivalent rather then the integer as a string

下面是我尝试编写一个将整数转换为字符串的函数。我不确定我是否正确使用了推送功能。我正在尝试将整数除以 10,然后将余数加上 48h,然后将其添加到堆栈中。然后重复该过程,直到将整个整数转换为字符串。此函数以 Ascii 格式打印字符串,但我想以字符串表示形式打印精确的整数。例如,如果存储在变量 answer 中的整数是 75,那么我希望此函数将 '75' 打印为字符串,但它会打印 'K'.

XOR eax, eax
    mov eax, [esi]
    mov cl, 10             ;move 10 to cl
    div cl                 ;divide by eax by 10
    add edx, 48h           ;add 48h to remainder
    push edx
    mov [edi], edx
    pop eax
    inc edi                ;increments the edi pointer  

这就是我调用函数将存储在 answer 中的整数转换为 sting 并打印它的方式。

lea esi, answer
call num2str
call PrintString

P.S。我正在使用 visual studio 2012 进行编译。谢谢!

问题是 div cl 是一个 8 位除法。它将 ax 除以 cl,将结果放入 al,余数放入 ah——edx 不受影响。您需要使用 div ecx 来除以 edx/eax 寄存器对中的 64 位值,并将结果放入 eax 中,如果您希望代码正常工作,则将余数放入 edx 中——需要清除 ecx 的高 24 位并同时清除 edx:

;; eax = number to be converted to ASCII
;; edi = END of the buffer in which to store string
    xor ecx,ecx
    mov [edi], cl
    mov cl,10
loop:
    xor edx,edx
    div ecx
    add dl, 48h
    dec edi
    mov [edi], dl
    test eax,eax
    jnz loop
;; edi now points at the ASCII string