有一个关于NASM中整数到字符串转换的问题。
我的问题是,当循环终止时,如何连接数字,使输出包含所有数字,而不是最后计算的数字?
例如。1234->1234div 10=余数4=>缓冲区="4"123->123div 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: %sn", 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
好吧,+int的位数不能超过10,所以你能为10个字符分配空间,并在填充数字和返回数组基数时增加循环中的赋值索引吗?
--我不太相信这就是你所需要的,所以我把它作为一个评论,但由于是,而且你是新来的,我真的应该把它移到这里,作为一个官方答案。