此示例描述a数组中的元素累加的结果,c和b交换的实例:
·by fulinux(blog.csdn.net/fulinus)
例如:
#include
int main (int argc, char **argv)
{
int a[4] = {1, 2, 3, 4};
int b;
__asm__ ("MOV R1, %1;"
"LDMIA R1!, {R2-R5};"
"ADD R2, R2, R3;"
"ADD R4, R4, R5;"
"ADD R2, R2, R4;"
"MOV %0, R2;"
:"=r"(b)
:"r"(a));
printf ("b = %d\n", b);
int c=100,d=200;
int result;
__asm__ __volatile__(
"mov %0,%3\n\t" //%0是一个占位符,表示result,之后的类推
"ldr r0,%1\n\t"
"ldr r1,%2\n\t"
"str r0,%2\n\t"
"str r1,%1\n\t"
:"=r"(result),"+m"(c),"+m"(d)
:"i"(123));
printf ("c = %d, d=%d, result=%d\n", c, d, result);
int e[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7,8}};
int f;
int *g;
int h;
g = &e[3][1];
printf ("g=%p, %p\n", g, &e[2][1]);
__asm__ __volatile__(
"MOV R0, %2;"
"MOV R3, #0;"
"MOV R4, #0;"
"MOV R5, %3;"
"LOOP:;"
"LDMIA R0!, {R1-R2};"
"ADD R1, R1, R2;"
"ADD R3, R3, R1;" //sum=sum+R1
"ADD R4, R4, #1;" //i++
"CMP R0, R5;"
"BLE LOOP\n"
"MOV %0, R3;"
"MOV %1, R5;"
:"=r"(f),"=r"(h)
:"r"(e),"r"(g)
);
printf ("f = %d, h = %p\n", f, h);
return 0;
} /* ----- End of main() ----- */
double j=0.001;
short int k = 10;
double l;
asm volatile (
"vmov s8, %[k]\n\t"
"vcvt.f64.s32 d6, s8\n\t"
"vmul.f64 %P[l], %P[j], d6\n\t"
: [l] "=w" (l)
: [j] "w" (j), [k] "r" (k)
: "d8", "s8"
);
printf ("l = %f\n", l);
cmp:把一个寄存器的内容和另一个寄存器的内容或立即数进行比较。但不存储结果,只是正确的更改标志。
一般 cmp 做完判断后会进行跳转,后面通常会跟上B指令。
BL 标号:跳转到标号处执行
B.LT 标号:比较结果是小于(less than),执行标号,否则不跳转;
B.LE 标号:比较结果是小于等于(less than or equal to),执行标号,否则不跳转;
B.GT 标号:比较结果是大于(greater than),执行标号,否则不跳转;
B.GE 标号:比较结果是大于等于(greater than or equal to),执行标号,否则不跳转;
B.EQ 标号:比较结果是等于,执行标号,否则不跳转;
B.HI 标号:比较结果是无符号大于,执行标号,否则不跳转;
作者:原子弹
链接:https://www.jianshu.com/p/7e1eb1d72c83
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。