BNE is a fundamental branch instruction in ARM and Thumb assembly, critical for implementing conditional logic, loops, and decision-making in your code.
BNE stands for Branch if Not Equal.
1. What BNE Does
BNE is a conditional branch instruction. It causes the program execution to jump to a new memory address (a "branch") only if a specific condition is met. The condition it checks is whether the result of a previous operation was not equal to zero.
More precisely, BNE checks the Z (Zero) flag in the ARM Processor Status Register (APSR).
- If the Z flag is clear (0), meaning the result of the previous operation was not zero (not equal), then the branch is taken.
- If the Z flag is set (1), meaning the result of the previous operation was zero (equal), then the branch is not taken, and execution continues with the next instruction in sequence.
2. How the Z Flag is Affected
The Z flag is updated by many ARM instructions, particularly those that perform arithmetic or logical operations, and especially by compare instructions:
CMP (Compare): Subtracts one operand from another but discards the result, only updating the flags. If CMP R0, R1 results in R0 == R1, then R0 - R1 is 0, and the Z flag is set.
SUBS (Subtract with Set flags): Performs subtraction and updates flags. If the result is 0, Z flag is set.
ADDS (Add with Set flags): Performs addition and updates flags. If the result is 0, Z flag is set.
ANDS (Logical AND with Set flags): Performs logical AND and updates flags. If the result is 0, Z flag is set.
- Most instructions with an
S suffix (e.g., MOVS, ORRS, EORS) will update the flags.
3. Syntax
The general syntax for BNE is:
BNE <label>
<label>: This is a symbolic name for the memory address where execution should jump if the condition (Not Equal) is met. Assemblers translate this label into an actual memory offset.
4. Common Use Cases
BNE is essential for implementing:
A. Loops
BNE is frequently used to create loops by checking a counter.
Example: A simple decrementing loop
MOV R0, #10 ; Initialize loop counter to 10
loop_start:
; ... your loop body instructions here ...
; For example, decrement a value or process data
SUBS R0, R0, #1 ; Decrement R0 by 1 and update flags (including Z flag)
BNE loop_start ; If R0 is not zero (Z flag is 0), branch back to loop_start
; If R0 is zero (Z flag is 1), fall through to next instruction
; ... code after the loop ...
In this example:
SUBS R0, R0, #1 decrements R0. If R0 becomes 0, the Z flag is set. If R0 is any non-zero value, the Z flag is cleared.
BNE loop_start checks the Z flag. If R0 was 1 before SUBS (making it 0 after SUBS), then Z is set, BNE doesn't branch, and the loop terminates. Otherwise, Z is clear, BNE does branch, and the loop continues.
B. Conditional Branches (If/Else logic)
BNE is often used after a CMP instruction to implement "if (X != Y)" or "if (X == Y)" logic.
Example: If R0 is not equal to R1, then branch to different_values
CMP R0, R1 ; Compare R0 with R1 (effectively R0 - R1).
; If R0 == R1, Z flag is set.
; If R0 != R1, Z flag is cleared.
BNE different_values ; If R0 is NOT equal to R1 (Z flag clear), branch.
; ... code for when R0 IS equal to R1 ...
B end_of_if_else ; Unconditional branch to skip the "different_values" block
different_values:
; ... code for when R0 is NOT equal to R1 ...
end_of_if_else:
; ... code after the conditional block ...
5. Other Conditional Branches
BNE is just one of many conditional branch instructions in ARM/Thumb. Each checks a different combination of the status flags. Some common ones include:
BEQ: Branch if Equal (Z flag is set)
BGT: Branch if Greater Than (signed comparison)
BLT: Branch if Less Than (signed comparison)
BGE: Branch if Greater Than or Equal (signed comparison)
BLE: Branch if Less Than or Equal (signed comparison)
BHI: Branch if Higher (unsigned comparison)
BLO: Branch if Lower (unsigned comparison)
BCS / BHS: Branch if Carry Set / Higher or Same (unsigned comparison, often used for "no borrow")
BCC / BLO: Branch if Carry Clear / Lower (unsigned comparison, often used for "borrow")
BMI: Branch if Minus (N flag is set)
BPL: Branch if Plus (N flag is clear)
Understanding BNE and how it interacts with the Z flag is fundamental to reading and writing control flow in ARM and Thumb assembly.