ARM汇编与C语言分支对照
ARMv7汇编与C语言分支对照
一、if语句
C代码:
int i = 10;
int j = 0;
int k = 0;
if(i > j)
{
k = 9;
} else {
k = 8;
}
ARM汇编:
mov r0, #0x0A
mov r1, #0x00
mov r2, #0x00
cmp r0, r1
movgt r2, #0x09
movle r2, #0x08
===================================================
二、for循环
C语言
int i = 0;
int j = 10;
int sum = 0;
for(i = 0; i < j; i++)
{
sum += i;
}
ARM汇编:
mov r0, #0x00 // i
mov r1, #0x0A // j
mov r2, #0x00 // sum
LOO_SUM:
add r2, r2, r0 // sum = sum + i
add r0, r0, #0x01 //i++
cmp r0, r1
bge END_LOOP
b LOOP_SUM
END_LOOP: //退出循环
==============================================================
三、switch(key)....case
C语言
int key = 0;
int ret = 0;
switch(key)
{
case 0:
ret = 0;
break;
case 1:
ret = 1;
break;
case 2:
ret = 2;
break;
}
ARM汇编:
//r0 = ret
//r1 = key
mov r1, #0x00
cmp r0, r1
moveq r0, #0x00
beq END_TO_CONVERT
mov r1, #0x01
cmp r0, r1
moveq r0, #0x01
beq END_TO_CONVERT
mov r1, #0x02
cmp r0, r1
moveq r0, #0x32
beq END_TO_CONVERT
END_TO_CONVERT: