1.在C/C++中嵌入汇编
#include <stdio.h>
void my_strcpy(const char *src, char *dest)
{
char ch;
__asm
{
loop:
ldrb ch, [src], #1
strb ch, [dest],#1
cmp ch, #0
bne loop
}
}
int Main()
{
char *a = "forget it and move on!";
char b[64];
my_strcpy(a, b);
//printf("original: %s", a);
//printf("copyed: %s", b);
return 0;
}
2.汇编中访问C/C++中的全局变量
#include <stdio.h>
int gVar_1 = 12;
extern asmDouble(void);
int Main()
{
//printf("original value of gVar_1 is: %d", gVar_1);
asmDouble();
//printf(" modified value of gVar_1 is: %d", gVar_1);
return 0;
}
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++调用汇编函数
#include <stdio.h>
extern void asm_strcpy(const char *src, char *dest);
int Main()
{
const char *s = "seasons in the sun";
char d[32];
asm_strcpy(s, d);
//printf("source: %s", s);
//printf("destination: %s",d);
return 0;
}
AREA asmfile, CODE, READONLY
EXPORT asm_strcpy
asm_strcpy
start
loop
ldrb r4, [r0], #1
cmp r4, #0
beq over
strb r4, [r1], #1
b loop
over
mov pc, lr
END
4.汇编调用C/C++函数
int cFun(int a, int b, int c)
{
return a + b + c;
}
EXPORT asmfile
AREA asmfile, CODE, READONLY
IMPORT cFun
ENTRY
mov r0, #11
mov r1, #22
mov r2, #33
BL cFun
END