Conditional branches let the program choose what to do next based on results from earlier steps. Flags are special markers that tell if something like a number is zero or negative.
Conditional branch with flags in ARM Architecture
B{condition} labelB stands for branch, which means jump to another part of the program.
The {condition} is a two-letter code that checks flags like zero (Z), negative (N), carry (C), or overflow (V).
label if the zero flag (Z) is set, meaning the last result was zero.BEQ label
label if the zero flag (Z) is clear, meaning the last result was not zero.BNE label
label if the last result was greater than zero (signed comparison).BGT label
label if the last result was less than zero (signed comparison).BLT label
This program counts down from 5 to 0. It subtracts 1 from register R0 each time. The SUBS instruction updates the flags. If R0 becomes zero, the BEQ END instruction jumps to the label END, stopping the loop. Otherwise, it jumps back to LOOP to continue counting down.
MOV R0, #5 LOOP: SUBS R0, R0, #1 BEQ END B LOOP END:
Flags are set by instructions that end with S, like SUBS or ADDS.
Common condition codes include EQ (equal), NE (not equal), GT (greater than), LT (less than), GE (greater or equal), and LE (less or equal).
Using conditional branches helps make your program decision-based and efficient.
Conditional branches use flags to decide where the program goes next.
Flags like zero (Z) and negative (N) come from previous instructions that update them.
Branch instructions with conditions let you create loops and decisions in your code.