从调用C源$ C ​​$ C汇编程序 [英] Calling assembly routines from C source code

查看:105
本文介绍了从调用C源$ C ​​$ C汇编程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this simple C source code :

#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:

and i have this s.asm which defines the function _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:

now , i compiled the .asm using :

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

和编译和链接使用.c文件:

and compiled and linked the .c file using :

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

那么,什么是问题呢?

So what is the problem ?

我在使用Ubuntu Linux的

I'm using Ubuntu-Linux

任何帮助将大大AP preciated,谢谢

Any help would be greatly appreciated , Thanks

[解决]:我用纳米test.o文件检查,它希望找到符号求和而不是_Sum',所以改变这种解决了这个问题。

[SOLVED] : i checked with nm the test.o file and it expected to find the symbol 'Sum' not '_Sum' so changing that solved the problem.

推荐答案

据我可以从你的问题看,你覆盖从汇编来到你的目标文件所以由C程序。所以,你没有汇编程序了。

As far as I can see from your question, you overwrite your object file that came from the assembler s.o by the C program. So you don't have the assembler routine any more.

您或许应该写

生成 s.o 目标文件

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

生成 test.o (您的命令创建另一个s.o)

Generate the test.o (your command created another s.o)

 gcc test.c -c 

链接应用

 gcc s.o test.o -o testapp 

(我选择testapp作为输出二进制因为测试往往是一个项目一个非常糟糕的名字,它与Unix命令测试<碰撞/ code>)

(I chose testapp as output binary because test is often a very bad name for a program, it collides with the Unix command test)

这篇关于从调用C源$ C ​​$ C汇编程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆