c用汇编语言编写的调用函数

时间:2012-10-20 03:01:19

标签: c linux gcc assembly

我正在使用GCC练习C和汇编语言混合编程。这个错误发生了:

1 deng@ubuntu:~/workspace/leos_test$ make
2 ld -o deng c.o asm.o
3 ld: warning: cannot find entry symbol _start; defaulting to 0000000008048074
4 c.o: In function `main':
5 c_and_asm.c:(.text+0x19): undefined reference to `add'
6 c_and_asm.c:(.text+0x2e): undefined reference to `printf'
7 make: *** [deng] Error 1

这是C代码:

1 #include<stdio.h>
2 void extern add(int * i);
3 void main(){
4    int i=1;
5    add(&i);
6    printf("%d ",i);
7}

这是汇编语言代码:

1 .globl _add
2 _add:
3 push %bp;
4 mov %sp,%bp;
5 mov 0x4(%bp),%eax;
6 incw (%eax);
7 popw %bp;
8 ret

这是makefile:

1 deng: c.o asm.o
    2 ld -o deng c.o asm.o
3 c.o:
    4 gcc -o c.o -c c_and_asm.c
5 asm.o:
    6 as -o asm.o c_asm.asm

任何建议都将受到赞赏:)。

2 个答案:

答案 0 :(得分:1)

_移除_add并将.globl修改为global

segment .text
global add

add:

答案 1 :(得分:1)

将c_asm.asm文件更改为:

.section .text
.global add
.type add,@function
add:
push %bp;
mov %sp,%bp;
mov 0x4(%bp),%eax;
incw (%eax);
popw %bp;
ret

在您的ld中包含此参数-lc,并最好将文件扩展名更改为.s