从C源代码调用汇编程序

问题描述 投票:3回答:3

我有这个简单的C源代码:

#include <stdio.h>

extern int Sum(int,int);

int main()
{
  int a,b,s;
  a=1 , b=2;
  s = Sum(a,b);
  return 0;
}

而且我有这个s.asm,它定义了函数_Sum:

global _Sum

     _Sum:

        push    ebp             ; create stack frame
        mov     ebp, esp
        mov     eax, [ebp+8]    ; grab the first argument
        mov     ecx, [ebp+12]   ; grab the second argument
        add     eax, ecx        ; sum the arguments
        pop     ebp             ; restore the base pointer
        ret

现在,我使用:]编译了.asm。

nasm s.asm -f elf -o s.o

并使用:]编译并链接.c文件。

gcc s.o test.o -o testapp

这是结果:

/tmp/ccpwYHDQ.o: In function `main':
test.c:(.text+0x29): undefined reference to `Sum'
collect2: ld returned 1 exit status

那是什么问题?

我正在使用Ubuntu-Linux

任何帮助将不胜感激,谢谢

[[已解决]:我用nm检查了test.o文件,它期望找到符号'Sum'而不是'_Sum',因此进行更改即可解决问题。

我有这个简单的C源代码:#include extern int Sum(int,int); int main(){int a,b,s; a = 1,b = 2; s =总和(a,b);返回0; },我有这个s.asm,它定义了...

c linux assembly nasm linker-errors
3个回答
4
投票

据我从您的问题中可以看到的,您用C程序覆盖了来自汇编器s.o的目标文件。因此,您再也不需要汇编程序了。

您可能应该写


7
投票

在典型的汇编程序中,标签默认为本地。要告诉汇编器使它们对外部例程可见,必须添加一个声明,例如:


0
投票

最好在c文件中内联声明asm。这是我自己的代码中的一个示例:

© www.soinside.com 2019 - 2024. All rights reserved.