x86汇编与C相互调用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/listener51/article/details/78760718

C函数调用x86纯汇编

实现简单的加法:例如add(2,3);

1、新建main.c文件

#include<stdio.h>
#include<stdlib.h>
extern int add(int a, int b);

int main()
{
	int sum = add(2,3);
        printf("sum = %d\n", sum);
	return 0;
}

2、新建add.sam文件

section .data
label db 4

section .text
global add
add:
	push ebp
	mov ebp, esp
	mov eax, [esp + 8]
	mov edx, [esp + 12]
	add eax, edx;
	mov esp, ebp
	pop ebp
	ret

3、编译方法

gcc -g -m32 -c main.c -fPIC -I. -o main.o
yasm -m x86 -f elf -DPIC -I. add.asm -o add.o
gcc -g -m32 -o demo main.o add.o

堆栈参考网址:http://blog.csdn.net/u013471946/article/details/39994223

猜你喜欢

转载自blog.csdn.net/listener51/article/details/78760718