汇编和c函数交叉混合调用

有转载的话希望能尊重原创,谢谢各位!

以下运行在Ubuntu环境下,需要安装qemu-user,使用arm-linux-gcc编译。

////////////////////////////////////////////////////////

////////c文件中调用汇编文件中的汇编“函数”

////////////////////////////////////////////////////////

//b.c

#include<stdio.h>

extern int sum(int a,int b);

int main()
{
	int n;
	n=sum(10,12);
	printf("--%d--\n",n);
}

//a.s

.text

.global sum

sum:

mov r3,r0
add r3,r1
mov r0,r3
mov pc,lr	;#  lr即r14

.end


//编译和执行结果
ubuntu@ubuntu:~/Documents$ 
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o a.o a.s
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o b.o b.c
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -static -o xx a.o b.o
ubuntu@ubuntu:~/Documents$ qemu-arm xx
--22--
ubuntu@ubuntu:~/Documents$ 





////////////////////////////////////////////////////////
////////汇编文件调用c文件中的函数
////////////////////////////////////////////////////////

//a.s

.text

.global main	;#主函数入口
.global fun1
.global fun2

main:
	bl fun1
	bl fun2

halt_loop:
	b halt_loop

.end

//b.c

#include<stdio.h>

extern void fun1(void);
extern void fun2(void);

void fun1(void)
{
	printf("zhong\n");
}

void fun2(void)
{
	printf("kun jiang\n");
}

//编译和执行结果
ubuntu@ubuntu:~/Documents$ 
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o a.o a.s
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o b.o b.c
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -static -o xx a.o b.o
ubuntu@ubuntu:~/Documents$ qemu-arm xx
zhong
kun jiang
^C
ubuntu@ubuntu:~/Documents$ 




///////////////////////////////////////////////////////////////////
////////c文件中定义全局(外部的)变量,汇编文件中调用该变量
///////////////////////////////////////////////////////////////////

//a.s

.text

.global main	;#主函数入口
.global fun1
.global fun2

.global glbval


main:
	mov r0,#100
	ldr r1,=glbval
	str r0,[r1]
	bl fun1
	
	mov r0,#200
	ldr r1,=glbval
	str r0,[r1]
	bl fun2

halt_loop:
	b halt_loop

.end

//b.c

#include<stdio.h>

extern void fun1(void);
extern void fun2(void);

extern int glbval;
int glbval;

void fun1(void)
{
	printf("fun1():glbval=%d\n",glbval);
}

void fun2(void)
{
	printf("fun2():glbval=%d\n",glbval);
}

//编译和执行结果
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o a.o a.s
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o b.o b.c
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -static -o xx a.o b.o
ubuntu@ubuntu:~/Documents$ qemu-arm xx
fun1():glbval=100
fun2():glbval=200
^C
ubuntu@ubuntu:~/Documents$ 


////////////////////////////////////////////////////////////////////////////
///汇编文件调用c文件中的函数,c文件中的函数带参数,实现汇编到c的参数传递
////////////////////////////////////////////////////////////////////////////

//a.s

.text

.global main	;#主函数入口
.extern fun1
.global fun2

.global glbval


main:
	mov r0,#100
	ldr r1,=glbval
	str r0,[r1]
	bl fun1
	
	mov r0,#200
	ldr r1,=glbval
	str r0,[r1]
	bl fun2

	mov r0,#10	;#r0:a
	mov r1,#12	;#r1:b
	mov r2,#14	;#r2:c
	bl sum
	
	ldr r1,=glbval
	str r0,[r1]
	bl fun1
	
halt_loop:
	b halt_loop

.end

//b.c

#include<stdio.h>

extern void fun1(void);
extern void fun2(void);
extern int sum(int a,int b,int c);

extern int glbval;
int glbval;

void fun1(void)
{
	printf("fun1():glbval=%d\n",glbval);
}

void fun2(void)
{
	printf("fun2():glbval=%d\n",glbval);
}

int sum(int a,int b,int c)
{
	int n;
	n=a+b+c;
	return n;
}

//编译和执行结果
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o a.o a.s
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o b.o b.c
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -static -o xx a.o b.o
ubuntu@ubuntu:~/Documents$ qemu-arm xx
fun1():glbval=100
fun2():glbval=200
fun1():glbval=36
^C
ubuntu@ubuntu:~/Documents$ 







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒江独钓2009

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值