2015-02-24 24 views
0

以下是我尝试编写一个函数来将整数转换为字符串。我不确定我是否正确使用推送功能。我试图将整数除以10,并将剩余的48小时添加到堆栈中。然后重复该过程,直到整个整数转换为字符串。此函数在Ascii中打印字符串,但我想打印字符串表示中的确切整数。例如,如果存储在变量答案中的整数是75,那么我希望这个函数以字符串的形式打印'75',但它打印'K'。功能正在打印Ascii相当于整数作为字符串

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 

这是我如何调用函数来转换存储在答复sting和打印它的整数。

lea esi, answer 
call num2str 
call PrintString 

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

回答

0

问题是div cl是一个8位分隔符。它通过cl分割ax,把结果放在al中,其余的在ah-edx中不受影响。您需要使用div ecx来获得除edx/eax寄存器对中64位值的差值,并将结果放在eax中,其余部分放在edx中,如果您希望代码正常工作 - 这需要清除高位24位的ecx和清除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 
相关问题