arm学习笔记五(c/c++与arm汇编混合编程)

混合编程


常见方式:
1 在c/c++程序中嵌入汇编指令
语法格式:
__asm
{
汇编语言程序
}
2 在汇编程序中访问c/c++定义的全局变量
示例代码如下:
test.c
#include <stdio.h>
int gVar_1=12;
extern asmDouble(void)
int main(void){
printf("original value of gVar_1 is %d",gVar_1);
admDouble();
printf("modified value of gVar_1 is %d",gVar_1);
return 0;
}

test.s
AREA asmfile,CODE,READONLY
EXPORT asmDouble;声明全局引用标号
IMPORT gVar_1;引用
asmDouble
ldr r0,=gVar_1
ldr r1,[r0]
mov r2,#2
mul r3,r1,r2
str r3,[r0]
mov pc,lr
END
3 在c/c++程序中调用汇编函数
示例代码如下:


test1.s
AREA asmfile,COCE,READONLY
EXPORT asm_strcpy;声明全局引用标号
asm_strcpy;函数名
loop:
ldrb r4,[r0],#1
cmp r4,#0
beq over
strb r4,[r1],#1
b loop
over:
mov pc,lr;用于函数返回
END




test1.c
#include <stdio.h>
extern void asm_strcpy(const char *src,char *dest);
int main(){
const char *s ="hello world";
char d[32];
asm_strcpy(s,d);
printf("source:%s",s);
printf("destination: %s",d);
return 0;
}
上面程序jni的味道有木有?

4 汇编程序中调用c/c++函数
示例代码如下:
test2.c
int cFun(int a,int b,int c){
return a+b+c;
}

test2.s
EXPORT asmfile
AREA asmfile,CODE,READONLY
IMPORT cFun;引用函数
ENTRY;指定应用程序入口
mov r0,#11
mov r1,#22
mov r2,#33
BL cFun;返回
END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值