2012-02-05 61 views
2

有一个关于NASM中整数到字符串转换的问题。NASM - 整数到字符串

我的问题是如何连接数字,使输出包含循环终止时的所有数字,而不是最后计算的数字?

例如, 1234 - > 1234 DIV 10 =剩余4 =>缓冲= “4” 123 - > 123 DIV 10 =剩余3 =>缓冲= “3” 等等

我的程序只是存储计算的最后一位数字('1')并打印。

这里是我的代码文件:

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int i; 
    char *str; 
    printf("Enter a number: "); 
    scanf ("%d", &i); 
    str=int2string(i); 
    printf("Number as string is: %s\n", str); 

    return 0; 
} 

%include "asm_io.inc" 

segment .data 

segment .bss 
buffer db 20 ; buffer of size 8 bits (in reference to c file) 

segment .text 
    global int2string 
int2string: 
     enter 0,0    ; setup routine 
     pusha 
    mov esi, 0  ; set sign to 0 
    mov ebx, 0  ; set current remainder to 0 
    mov edi, 0  ; set the current digit in eax 

     mov eax, [ebp+8]  ; eax contains input value of int2string 

    jmp placeValues 

placeValues: 
    mov edx, 0  ; set remainder to 0 
    mov eax, eax  ; redundancy ensures dividend is eax 
    mov ebx, 10  ; sets the divisor to value of 10 
    div ebx   ; eax = eax/ebx 

    mov ebx, 48  ; set ebx to 48 for ASCII conversion 
    add ebx, edx  ; add remainder to ebx for ASCII value 
    add dword[buffer], ebx ; store the number in the buffer 

    cmp eax, 0 
    jz return 
    jmp placeValues 

return: 
    popa 
    mov eax, buffer  ; move buffer value to eax 
     leave      
     ret 
+0

那么+ int的数字计数不能超过10,那么您可以为10个字符分配空间,并在填充数字并返回数组的基数时递增循环内的赋值索引? – 2012-02-05 05:56:11

+0

完美运行,感谢您的快速响应 – user1074249 2012-02-05 06:00:48

回答

1

嗯,+ INT数字计数不能十更长的时间,所以你可以分配房间十个字符和环路内递增分配索引你填充数字并返回数组的基数?

- 我不是很有信心,这是你需要的,所以我把它作为一个评论,但因为它而且你是新的,我应该真的把它移动到这里,作为一个正式的答案。