我的目标是要做到以下几点:获取字符串的长度在NASM与strlen的通话
1)编写调用由C
2的strlen计算字符串长度的NASM代码)调用此在C函数打印提供的字符串的长度
NASM代码:
;nasm -f elf32 getLength.asm -o getLength.o
segment .text
extern strlen
global getLength
getLength:
push ebp ;save the old base pointer value
mov ebp,esp ;base pointer <- stack pointer
mov eax,[ebp+8] ;first argument
call strlen ; call our function to calculate the length of the string
mov edx, eax ; our function leaves the result in EAX
pop ebp
ret
的C代码:
#include <stdio.h>
#include <string.h>
int getLength(char *str);
int main(void)
{
char str[256];
int l;
printf("Enter string: ");
scanf("%s" , str) ;
//l = strlen(str);
l = getLength(str);
printf("The length is: %d\n", l);
return 0;
}
我尝试编译,链接和运行如下:
1)NASM -f ELF32 getLength.asm -o getLength.o
2)GCC -c length.c -o的getLength。 ø-m32
3)的gcc getLength.o getLength.o -o长度-m32
错误我得到:
getLength.o: In function `getLength':
getLength.asm:(.text+0x0): multiple definition of `getLength'
getLength.o:getLength.asm:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
是的。这是编译技巧!然而,在我的NASM代码中似乎有某种错误,因为我得到了0的结果。 – SpiderRico
完美!非常感谢。 – SpiderRico