Control Structures in ARM Assembly
In questa pagina 6
Branches and conditions
Definition (branches B and B{cond})
B label: unconditional branch (PC-relative, plus or minus 32 MB).B{cond} label: branches only if the condition on the flags is true. The flags are set withCMPor with an instruction carrying the suffixS(see ARM Data-Processing InstructionsAdd, subtract, logical, move and multiply instructions of ARM, the immediate and shifted-register second operand, flag updates with S, CMP and TST, and translation of C expressions into ARM code.ARM Data-Processing Instructions →).
Example. After CMP r0, r1, BEQ end branches only if r0=r1; with r0=3 and r1=4 the next instruction is executed.
Definition (signed and unsigned condition codes)
Suffix Meaning Flags Type EQ/NEequal / not equal Z = 1 / Z = 0 both GT/GEgreater / greater or equal Z = 0 and N = V / N = V signed LT/LEless / less or equal N != V / Z = 1 or N != V signed HI/HS(=CS)greater / greater or equal C = 1 and Z = 0 / C = 1 unsigned LO(=CC) /LSless / less or equal C = 0 / C = 0 or Z = 1 unsigned MI/PLnegative / positive or zero N = 1 / N = 0 VS/VCoverflow / no overflow V = 1 / V = 0
Example. With r0=0xFFFFFFFF and r1=1, CMP r0, r1 makes both LT ( signed) and HI ( unsigned) true. This is why two families of conditions exist.
if and if-else
if (a == b) c = c + 1; else c = c - 1; // a, b, c in r0, r1, r2 CMP r0, r1
BNE otherwise @ negated condition: jump to the else branch
ADD r2, r2, #1
B end
otherwise:
SUB r2, r2, #1
end:The same without branches, using conditional execution:
CMP r0, r1
ADDEQ r2, r2, #1
SUBNE r2, r2, #1It pays off for branches of a few instructions: no jumps, hence no pipeline flush.
Loops
while (while (i < n) { s += i; i++; }, with i, n, s in r0, r1, r2, signed):
loop: CMP r0, r1
BGE out @ exits if i >= n
ADD r2, r2, r0
ADD r0, r0, #1
B loop
out:for with a down-counter: for (i = 10; i != 0; i--) s += i; becomes
MOV r0, #10
MOV r2, #0
loop: ADD r2, r2, r0
SUBS r0, r0, #1 @ decrements and updates Z
BNE loopAt the end r2 = 10 + 9 + ... + 1 = 55. Counting down to zero saves the CMP.
do-while: the test goes at the bottom, as in the previous example.
Maximum of two numbers, absolute value
CMP r0, r1
MOVLT r0, r1 @ r0 = max(r0, r1) signed
CMP r0, #0
RSBLT r0, r0, #0 @ r0 = |r0|switch with a jump table
If the cases are 0, 1, ..., a table of addresses is used:
CMP r0, #3
BHS default @ unsigned: also covers r0 < 0
ADR r1, table
LDR pc, [r1, r0, LSL #2] @ jumps to table[r0]
table:
.word case0, case1, case2Typical mistakes
- Using
BGT/BLTto compare addresses orunsignedvalues:BHI/BLOare needed. - Forgetting the
B endat the end of the "then" branch: the "else" branch runs too. - Putting an instruction with
Sbetween theCMPand the branch: the flags are overwritten.
Versione ripasso
Conditions
B{cond} branches if the flags (from CMP or suffix S, ARM Data-Processing InstructionsAdd, subtract, logical, move and multiply instructions of ARM, the immediate and shifted-register second operand, flag updates with S, CMP and TST, and translation of C expressions into ARM code.ARM Data-Processing Instructions →) satisfy the condition. Signed: GT, GE, LT, LE. Unsigned: HI, HS, LO, LS. With r0 = 0xFFFFFFFF, r1 = 1: LT and HI are both true.
Constructs
- if-else (
if (a == b) c++; else c--;):CMP r0,r1;BNE otherwise;ADD r2,r2,#1;B end;otherwise: SUB r2,r2,#1. Without branches:ADDEQ r2,r2,#1;SUBNE r2,r2,#1(no pipeline flush). - while:
CMP r0,r1;BGE out; body;B loop. - for counting down
i = 10 ... 1:SUBS r0,r0,#1;BNE loop(noCMP);r2 = 55. - switch:
CMP r0,#3;BHS default;ADR r1,table;LDR pc,[r1,r0,LSL #2].
Typical mistakes: BGT/BLT for addresses or unsigned (use BHI/BLO); forgetting B end in the then branch; S between CMP and branch overwrites the flags.