← Back to Articles

Branching and Control Flow

Branch Instructions

Branches change the program counter to execute different code paths.

Unconditional Branch

B label         // Always branch

Conditional Branches

  • BEQ: Branch if equal (Z flag set)
  • BNE: Branch if not equal (Z flag clear)
  • BGT: Branch if greater than
  • BLT: Branch if less than

Example: Simple Loop

MOV R0, #0       // Counter\nMOV R1, #5       // Limit\n// Loop start at line 3\nADD R0, R0, #1   // Increment\nCMP R0, R1       // Compare\nBLT 3            // Branch to line 3 if R0 < R1\nHALT