Bird
Raised Fist0
ARM Architectureknowledge~10 mins

Conditional branch with flags in ARM Architecture - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Conditional branch with flags
Execute instruction
↓
Update flags (N,Z,C,V)
↓
Evaluate condition code
↓
Branch
After an instruction runs, the processor updates special flags. Then it checks these flags against a condition. If the condition is true, it jumps (branches); otherwise, it keeps going.
Execution Sample
ARM Architecture
CMP R0, #0
BEQ label
MOV R1, #1
label: MOV R1, #2
Compare R0 to zero, branch if equal to label, else set R1 to 1; at label set R1 to 2.
Analysis Table
StepInstructionFlags Updated (N,Z,C,V)Condition CheckedBranch Taken?ActionR1 Value
1CMP R0, #0 (R0=0)N=0,Z=1,C=0,V=0BEQ (Z==1)?YesJump to labelN/A
2label: MOV R1, #2No flags changeN/AN/ASet R1=22
3EndN/AN/AN/AProgram continues2
💡 Branch taken at step 1 because zero flag Z=1, condition BEQ is true.
State Tracker
VariableStartAfter Step 1After Step 2Final
R00000
R1undefinedundefined22
Flags (N,Z,C,V)N=0,Z=0,C=0,V=0N=0,Z=1,C=0,V=0N=0,Z=1,C=0,V=0N=0,Z=1,C=0,V=0
Key Insights - 2 Insights
Why does the branch happen even though R1 was not set before?
The branch depends on flags set by CMP, not on R1. As shown in execution_table step 1, CMP sets Z=1, so BEQ condition is true and branch occurs before MOV R1, #1.
What if R0 was not zero? Would the branch still happen?
No. If R0 was not zero, CMP would set Z=0, making BEQ false. The branch would not be taken, and MOV R1, #1 would execute instead (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the value of the zero flag (Z)?
AUndefined
B0
C1
DNot set
💡 Hint
Check the 'Flags Updated' column at step 1 in execution_table.
At which step does the program jump to the label?
AStep 1
BStep 2
CStep 3
DNo jump occurs
💡 Hint
Look at the 'Branch Taken?' column in execution_table.
If R0 was 5 instead of 0, what would happen at step 1?
ABranch would be taken
BBranch would not be taken
CFlags would not update
DProgram would crash
💡 Hint
Recall CMP sets Z=1 only if values are equal; see key_moments explanation.
Concept Snapshot
Conditional branch uses flags set by instructions like CMP.
Flags: N (negative), Z (zero), C (carry), V (overflow).
Branch instructions check these flags to decide jump.
If condition true, jump; else continue sequentially.
Example: BEQ branches if Z=1 (equal).
Full Transcript
In ARM architecture, after an instruction executes, special flags are updated to reflect the result. These flags include Negative (N), Zero (Z), Carry (C), and Overflow (V). Conditional branch instructions check these flags to decide whether to jump to another part of the program or continue in order. For example, the BEQ instruction checks if the zero flag Z is set to 1, meaning the previous comparison found equality. If true, the program jumps to the specified label; if false, it continues with the next instruction. This visual trace shows a CMP comparing R0 to zero, setting flags accordingly, and then a BEQ that branches if equal. The variable tracker shows how R1 is set only after the branch. Understanding how flags control branching is key to reading ARM assembly flow.

Practice

(1/5)
1. What does the zero flag (Z) indicate in ARM conditional branching?
easy
A. The processor is in privileged mode
B. The previous operation caused an overflow
C. The previous operation resulted in zero
D. The previous operation resulted in a negative value

Solution

  1. Step 1: Understand the zero flag meaning

    The zero flag (Z) is set when the result of the last operation is exactly zero.
  2. Step 2: Relate zero flag to conditional branching

    Conditional branches use the zero flag to decide if the program should jump when a zero result occurs.
  3. Final Answer:

    The previous operation resulted in zero -> Option C
  4. Quick Check:

    Zero flag means result is zero [OK]
Hint: Zero flag means result is zero, check it first [OK]
Common Mistakes:
  • Confusing zero flag with negative flag
  • Thinking zero flag means overflow
  • Assuming zero flag indicates processor mode
2. Which of the following is the correct syntax for a conditional branch in ARM assembly that jumps if zero flag is set?
easy
A. BEQ label
B. BNE label
C. BGT label
D. BLT label

Solution

  1. Step 1: Identify the condition codes for branch instructions

    BEQ means 'Branch if Equal', which checks if the zero flag (Z) is set.
  2. Step 2: Match the zero flag condition to the instruction

    Since zero flag set means equality, BEQ is the correct instruction to branch on zero flag.
  3. Final Answer:

    BEQ label -> Option A
  4. Quick Check:

    BEQ branches if zero flag set [OK]
Hint: BEQ means branch if zero flag set [OK]
Common Mistakes:
  • Using BNE which branches if zero flag is clear
  • Confusing BGT and BLT which check signed comparisons
  • Mixing branch instructions with function calls
3. Given the ARM assembly snippet:
SUBS R0, R1, R2
BEQ equal_label
BNE notequal_label

If R1 equals R2, which label will the program jump to?
medium
A. equal_label
B. notequal_label
C. No jump, continue sequentially
D. Undefined behavior

Solution

  1. Step 1: Understand the SUBS instruction effect on flags

    SUBS subtracts R2 from R1, updates flags including zero flag if result is zero.
  2. Step 2: Check condition when R1 equals R2

    If R1 equals R2, subtraction result is zero, so zero flag is set.
  3. Step 3: Determine which branch executes

    BEQ branches if zero flag set, so program jumps to equal_label.
  4. Final Answer:

    equal_label -> Option A
  5. Quick Check:

    Equal inputs set zero flag, BEQ jumps [OK]
Hint: If subtraction zero, BEQ jumps to equal_label [OK]
Common Mistakes:
  • Choosing BNE which jumps if zero flag clear
  • Assuming no jump happens on equality
  • Confusing SUBS with non-flag updating SUB
4. Identify the error in this ARM assembly code snippet:
CMP R3, R4
BGT greater_label
BLT less_label
BEQ equal_label

Assuming R3 and R4 are signed integers, what is wrong?
medium
A. CMP does not update flags
B. BGT and BLT cannot be used after CMP
C. BGT and BLT check unsigned, should use BHI and BLS
D. No error, code is correct

Solution

  1. Step 1: Understand CMP and flag updates

    CMP subtracts R4 from R3 and updates flags for signed comparisons.
  2. Step 2: Check branch instructions for signed comparisons

    BGT (branch if greater than) and BLT (branch if less than) are correct for signed comparisons.
  3. Step 3: Confirm code correctness

    All branches correctly use signed condition codes after CMP, so no error.
  4. Final Answer:

    No error, code is correct -> Option D
  5. Quick Check:

    CMP + BGT/BLT correct for signed [OK]
Hint: BGT/BLT are for signed, BHI/BLS for unsigned [OK]
Common Mistakes:
  • Confusing signed and unsigned branch instructions
  • Thinking CMP does not update flags
  • Assuming BGT/BLT are invalid after CMP
5. You want to write ARM assembly code that jumps to handle_negative if a subtraction results in a negative value, and to handle_zero if the result is zero. Which conditional branch instructions should you use after SUBS R5, R6, R7?
hard
A. BGT handle_negative and BNE handle_zero
B. BLT handle_negative and BEQ handle_zero
C. BLE handle_negative and BNE handle_zero
D. BHS handle_negative and BLO handle_zero

Solution

  1. Step 1: Understand SUBS and flag updates

    SUBS subtracts and updates flags including negative (N) and zero (Z).
  2. Step 2: Match flags to branch instructions

    BLT branches if negative flag set (result negative), BEQ branches if zero flag set (result zero).
  3. Step 3: Confirm correct instructions for conditions

    BLT for negative result and BEQ for zero result are the correct choices.
  4. Final Answer:

    BLT handle_negative and BEQ handle_zero -> Option B
  5. Quick Check:

    Negative uses BLT, zero uses BEQ [OK]
Hint: Use BLT for negative, BEQ for zero after SUBS [OK]
Common Mistakes:
  • Using BGT or BHS for negative values
  • Mixing zero and negative flag conditions
  • Using unsigned branch instructions incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard