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     

posted @ 2013-02-21 21:47  retacn_yue  阅读(371)  评论(0编辑  收藏  举报