Salta al contenuto
Note per Studenti Control Structures in ARM Assembly Report

Control Structures in ARM Assembly

In questa pagina 6

Branches and conditions

Definition (branches B and B{cond})

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 / NE equal / not equal Z = 1 / Z = 0 both
GT / GE greater / greater or equal Z = 0 and N = V / N = V signed
LT / LE less / 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) / LS less / less or equal C = 0 / C = 0 or Z = 1 unsigned
MI / PL negative / positive or zero N = 1 / N = 0
VS / VC overflow / no overflow V = 1 / V = 0

Example. With r0=0xFFFFFFFF and r1=1, CMP r0, r1 makes both LT (−1<1-1<1 signed) and HI (4 294 967 295>14\,294\,967\,295>1 unsigned) true. This is why two families of conditions exist.

if and if-else

c
if (a == b) c = c + 1; else c = c - 1;      // a, b, c in r0, r1, r2
armasm
        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:

armasm
        CMP   r0, r1
        ADDEQ r2, r2, #1
        SUBNE r2, r2, #1

It 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):

armasm
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

armasm
        MOV  r0, #10
        MOV  r2, #0
loop:   ADD  r2, r2, r0
        SUBS r0, r0, #1         @ decrements and updates Z
        BNE  loop

At 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

armasm
        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, ..., k−1k-1 a table of addresses is used:

armasm
        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, case2

Typical mistakes

  • Using BGT/BLT to compare addresses or unsigned values: BHI/BLO are needed.
  • Forgetting the B end at the end of the "then" branch: the "else" branch runs too.
  • Putting an instruction with S between the CMP and 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 (no CMP); 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.

Teoria collegata