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.