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 ? ??
轉載于:https://www.cnblogs.com/retacn-yue/archive/2013/02/21/3263135.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的arm学习笔记五(c/c++与arm汇编混合编程)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈ref与out区别
- 下一篇: python中常用的函数