0
0
ARM Architectureknowledge~10 mins

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

Choose your learning style9 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.