有转载的话希望能尊重原创,谢谢各位!
以下运行在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$