0
0
ARM Architectureknowledge~10 mins

Compare and branch patterns in ARM Architecture - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Compare and branch patterns
Start
Compare Registers
Set Condition Flags
Evaluate Condition
Yes | No
Branch Taken
Execute Branch Target
End
The CPU compares values, sets flags, then decides to branch or continue based on conditions.
Execution Sample
ARM Architecture
CMP R0, R1
BEQ label
; next instructions
label: NOP
Compare R0 and R1; if equal, branch to label; otherwise continue.
Analysis Table
StepInstructionOperationCondition FlagsBranch TakenNext PC
1CMP R0, R1Compare R0 and R1Z=0 (not equal)NoPC+4
2BEQ labelCheck Z flagZ=0NoPC+4
3Next instructionExecute sequentially-NoPC+8
4label: NOPNo operation-N/APC+12
💡 Branch not taken because Z flag is 0 (R0 != R1)
State Tracker
VariableStartAfter CMPAfter BEQFinal
R05555
R13333
Z flag?000
PC1000100410081012
Key Insights - 3 Insights
Why does the branch not happen even though there is a BEQ instruction?
Because the CMP sets the zero flag (Z) to 0 since R0 and R1 are not equal, so the BEQ condition is false (see execution_table step 2).
What does the CMP instruction actually do?
CMP subtracts the second register from the first and sets condition flags but does not store the result (see execution_table step 1).
How does the CPU decide where to go next after a compare and branch?
It checks the condition flags set by CMP and if the branch condition matches, it jumps to the label; otherwise, it continues sequentially (see execution_table steps 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the zero flag (Z) value after CMP?
A1
B0
CUndefined
DNot set
💡 Hint
Check the 'Condition Flags' column in execution_table row 1.
At which step does the CPU decide not to branch?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Branch Taken' column in execution_table row 2.
If R0 and R1 were equal, how would the 'Branch Taken' value change at step 2?
AUndefined
BNo
CYes
DDepends on PC
💡 Hint
BEQ branches if zero flag is 1, see execution_table step 2 logic.
Concept Snapshot
CMP compares two registers and sets flags.
BEQ branches if zero flag is set.
CPU checks flags after CMP to decide branch.
If condition false, execution continues sequentially.
Branch changes program counter to target label.
Full Transcript
In ARM assembly, compare and branch patterns work by first comparing two registers using the CMP instruction. CMP subtracts one register from another and sets condition flags like the zero flag (Z) based on the result. Then, a branch instruction like BEQ checks these flags. If the zero flag is set (meaning the compared values are equal), the branch is taken and the program counter jumps to the label. Otherwise, execution continues to the next instruction. This flow ensures decisions in code based on comparisons.