Compare and branch patterns help the processor decide what to do next based on a condition. They let the program choose different paths, like making decisions in real life.
Compare and branch patterns in ARM Architecture
CMP Rn, Operand2
B{cond} labelCMP compares two values by subtracting but does not store the result.
B{cond} branches (jumps) to a label if the condition is true.
zero_label.CMP R0, #0
BEQ zero_labelnot_equal_label.CMP R1, R2 BNE not_equal_label
greater_than_label.CMP R3, #10
BGT greater_than_labelThis program compares two registers R0 and R1. If they are equal, it sets R2 to 1. Otherwise, it sets R2 to 0.
MOV R0, #5 MOV R1, #5 CMP R0, R1 BEQ equal_label BNE not_equal_label equal_label: MOV R2, #1 B end not_equal_label: MOV R2, #0 end:
The CMP instruction updates special flags in the processor to remember the comparison result.
Branch instructions use these flags to decide if they should jump or continue.
Common conditions include EQ (equal), NE (not equal), GT (greater than), LT (less than), GE (greater or equal), and LE (less or equal).
Compare and branch patterns let the processor make decisions by checking conditions.
CMP compares values without changing them but sets flags.
Branch instructions jump to different code parts based on these flags.
Practice
CMP instruction in ARM assembly?Solution
Step 1: Understand the role of CMP
The CMP instruction compares two values by subtracting one from the other without storing the result, but it sets flags based on the outcome.Step 2: Identify what CMP affects
These flags are then used by conditional branch instructions to decide program flow.Final Answer:
To compare two values and set condition flags -> Option BQuick Check:
CMP sets flags for branching decisions [OK]
- Thinking CMP jumps directly
- Confusing CMP with arithmetic instructions
- Assuming CMP stores the subtraction result
Solution
Step 1: Recall condition codes for branching
BEQ stands for 'Branch if Equal', which means it jumps if the zero flag is set after a CMP instruction.Step 2: Match condition with instruction
BNE means 'Branch if Not Equal', BGT is 'Branch if Greater Than', and BLT is 'Branch if Less Than'. Only BEQ matches the condition for equality.Final Answer:
BEQ label -> Option CQuick Check:
BEQ jumps when values are equal [OK]
- Using BNE instead of BEQ for equality
- Confusing greater than and equal conditions
- Forgetting to use CMP before branch
r2 after execution?CMP r0, r1 BEQ equal_label MOV r2, #0 B end_label equal_label: MOV r2, #1 end_label:
Assume
r0 = 5 and r1 = 5.Solution
Step 1: Analyze the CMP and BEQ instructions
CMP compares r0 and r1. Since both are 5, they are equal, so the zero flag is set. BEQ will branch to equal_label.Step 2: Follow the branch and MOV instructions
The code jumps to equal_label and executes MOV r2, #1, setting r2 to 1. The instructions after BEQ are skipped.Final Answer:
1 -> Option DQuick Check:
Equal values cause BEQ to set r2 to 1 [OK]
- Ignoring the branch and setting r2 to 0
- Confusing BEQ with BNE
- Assuming MOV after BEQ always runs
CMP r3, r4 BGT greater_label MOV r5, #10 greater_label: MOV r5, #20
What is the problem?
Solution
Step 1: Understand the flow of conditional branch
BGT branches to greater_label if r3 > r4. If not, execution continues to MOV r5, #10.Step 2: Identify missing jump to skip unwanted instruction
After executing MOV r5, #10, the code falls through to greater_label and executes MOV r5, #20, overwriting the previous value. To avoid this, an unconditional branch is needed after MOV r5, #10.Final Answer:
Missing unconditional branch to skip MOV after BGT -> Option AQuick Check:
Need unconditional branch to prevent fall-through [OK]
- Thinking CMP is wrong
- Assuming MOV instructions are incorrect
- Confusing BGT with BGE
r0 to 1 if r1 is less than r2, and to 0 otherwise. Which sequence correctly implements this using compare and branch patterns?Solution
Step 1: Understand the comparison needed
We want to check if r1 < r2. The CMP instruction compares r1 and r2, setting flags accordingly.Step 2: Use the correct branch condition
BLT (Branch if Less Than) jumps if r1 < r2. If true, branch to set_one to set r0 to 1. Otherwise, set r0 to 0.Step 3: Confirm the flow
If BLT is taken, r0 = 1; else r0 = 0. The unconditional branch after MOV r0, #0 skips the set_one label to avoid overwriting.Final Answer:
CMP r1, r2 BLT set_one MOV r0, #0 B end set_one: MOV r0, #1 end: -> Option AQuick Check:
Use CMP + BLT for less than check [OK]
- Swapping operands in CMP
- Using BGT instead of BLT
- Forgetting unconditional branch after MOV 0
Ready to prove it?
Pass the test and this pattern moves unknown
