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.
CMP R0, R1 BEQ label ; next instructions label: NOP
| Step | Instruction | Operation | Condition Flags | Branch Taken | Next PC |
|---|---|---|---|---|---|
| 1 | CMP R0, R1 | Compare R0 and R1 | Z=0 (not equal) | No | PC+4 |
| 2 | BEQ label | Check Z flag | Z=0 | No | PC+4 |
| 3 | Next instruction | Execute sequentially | - | No | PC+8 |
| 4 | label: NOP | No operation | - | N/A | PC+12 |
| Variable | Start | After CMP | After BEQ | Final |
|---|---|---|---|---|
| R0 | 5 | 5 | 5 | 5 |
| R1 | 3 | 3 | 3 | 3 |
| Z flag | ? | 0 | 0 | 0 |
| PC | 1000 | 1004 | 1008 | 1012 |
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.
CMP instruction in ARM assembly?r2 after execution?CMP r0, r1 BEQ equal_label MOV r2, #0 B end_label equal_label: MOV r2, #1 end_label:
r0 = 5 and r1 = 5.CMP r3, r4 BGT greater_label MOV r5, #10 greater_label: MOV r5, #20
r0 to 1 if r1 is less than r2, and to 0 otherwise. Which sequence correctly implements this using compare and branch patterns?Pass the test and this pattern moves unknown