星期五, 十一月 6

c 汇编 混合编程

1 汇编调用c函数:

add.c:

extern int add(int x, int y);

int add(int x, int y)
{
return(x + y);
}

asm.asm:

[section .data]
mstr db "%d", 0xA, 0
[section .text]
extern add
extern printf

global main
main:
push 1
push 2
call add

add esp,8
push eax
push dword mstr
call printf
add esp, 8
ret

gcc -c add.c add.o

nasm -f elf asm.asm

gcc -o final add.o asm.o

./final

3

2 c 调用汇编函数

asm.asm

[section .text]
extern add
extern printf

global show50247
show50247:
mov eax, 50247
ret

c.c

#include
#include
extern int show50247();

int main(int argc, char *argv[])
{
int a = show50247();
printf("%d\n", a);

return 0;
}

nasm -f elf asm.asm

gcc -c c.c

gcc -o c asm.o c.o

./c

50247

没有评论:

发表评论