问题由汇编程序组成,该程序从C程序获取输入并将其除以数字,然后将余数返回给C程序作为字符串打印。将整数转换为NASM中的字符串
这里是我的两个代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char *str;
str = malloc(1<<9);
printf("Enter a number: ");
scanf ("%d", &i);
printf("Number: %i\n", i);
str=int2string(i);
printf("Number as string is: %s\n", str);
return 0;
}
ASM =
%include "asm_io.inc"
segment .data
segment .bss
buffer resd 4
segment .text
global int2string
int2string:
enter 0,0 ; setup routine
pusha
mov edx, 0
mov eax, [ebp+8] ; eax contains input value of int2string
mov ebx, 10 ; sets ebx to value of 10
div ebx ; eax = eax/ebx
call print_int ; prints eax = quotient
call print_nl ; next line
mov eax, edx ; store edx (remainder) in eax
call print_int ; print remainder
call print_nl ; next line
add eax, 48 ; convert result into ASCII character
popa
mov dword[buffer], eax ; move ASCII character (if I replace eax with 48-57
; it prints 0-9 correctly)
mov eax, buffer ; move buffer value to eax
leave
ret
我的理解是,数字0-9的ASCII码是从48-57范围内,但如果我不要将数字明确地放在缓冲区移动中,那么输出是垃圾或分段错误。
我在这里丢失了什么(eax值为2 + 48,应该是ASCII(50)='2')?
RESD声明未初始化的存储空间,我的nasm是生锈的,但我看不到你null终止你生成的字符串。 – 2012-02-04 23:58:46
@JoachimIsaksson,我原本以为但是,因为你存储的是一个32位的寄存器,其值为零到九(字符,所以48到57整数),它将被存储为val/0/0/0。自动空终止,虽然可能只是一个无用的副作用:-) – paxdiablo 2012-02-05 00:04:00
在c程序描述中,它声明'str'字符数组大小为20,那么我将如何去初始化存储空间到该规范? – user1074249 2012-02-05 00:06:26