Linux 下C与汇编交互的小例子
C code:
#include <stdio.h>
extern add(int,int);
int main(int argc,char **argv)
{
int result = add(4,5);
printf("result:%d\n",result);
}
assembly code:
;nasm -f elf add.s -o add.o
[section .data]
Message db "calcuated result:%d",0xa,0
[section .text]
extern printf ;invoke c library
global add
add:
mov eax,[esp+4];
add eax,[esp+8];
mov ebx,eax;
push eax;
push dword Message;
call printf;
add esp,8;
mov eax,ebx;
ret;
编译命令:
nasm -f elf -o add.o add.s
gcc -c testAdd.c -o testAdd.o
gcc -o test_add testAdd.o add.o
./test_add