bne
Branch if Not Equal - 40 82 00 00
bne
Instruction Syntax
Mnemonic | Format | Flags |
bne | target | LK = 0 |
bnel | target | LK = 1 |
Instruction Encoding
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
LK
Field | Bits | Description |
Primary Opcode | 0-5 | 010010 (0x12) |
BO | 6-10 | 00010 (Branch Option) |
BI | 11-15 | 00000 (Branch Input - CR0[EQ]) |
BD | 16-29 | Branch displacement (14-bit signed) |
AA | 30 | Absolute Address |
LK | 31 | Link bit |
Operation
if CR0[EQ] = 0 then if AA = 0 then NIA ← CIA + EXTS(BD || 0b00) else NIA ← EXTS(BD || 0b00) if LK = 1 then LR ← CIA + 4
BNE branches to the target address if the Equal (EQ) bit in Condition Register field 0 is clear (i.e., if the values are not equal).
- Checks if the Equal bit (EQ) in CR0 is clear
- If EQ is clear (not equal), calculates the target address
- Branches to the target address
- Optionally saves the return address in LR if LK=1
Note: BNE is a derived form of the BC (Branch Conditional) instruction with BO=00010 and BI=00000.
Affected Registers
Link Register (LR)
(if LK = 1)
- LR - Set to CIA + 4 (return address)
Next Instruction Address (NIA)
- NIA - Set to target address if condition is met
Examples
Basic Not Equal Branch
# Compare two values and branch if not equal cmpw r3, r4 # Compare r3 with r4 bne not_equal # Branch if r3 != r4 # Continue here if r3 == r4 b continue_code not_equal: # Code executed when r3 != r4 continue_code:
Loop Control
# Loop until counter reaches zero loop_start: # ... loop body ... subi r3, r3, 1 # Decrement counter cmpwi r3, 0 # Compare with zero bne loop_start # Continue loop if counter != 0 # Loop finished
Error Detection
# Check for error conditions cmpwi r3, 0 # Check return value bne error_occurred # Branch if error (return value != 0) # Continue normal execution b normal_execution error_occurred: # Handle error case
String Comparison
# Compare strings character by character string_compare: lbz r4, 0(r3) # Load character from string1 lbz r5, 0(r6) # Load character from string2 cmpw r4, r5 # Compare characters bne strings_different # Branch if characters not equal # Characters are equal, continue addi r3, r3, 1 # Move to next character addi r6, r6, 1 b string_compare
Array Processing
# Process array until end marker found process_array: lwz r4, 0(r3) # Load array element cmpwi r4, -1 # Check for end marker bne continue_process # Continue if not end marker # End marker found, exit b array_end continue_process: # Process element addi r3, r3, 4 # Move to next element b process_array
Validation Checking
# Validate input value cmpwi r3, 0 # Check if input is zero bne valid_input # Branch if input is not zero # Handle invalid input (zero) b invalid_input valid_input: # Process valid input # ... processing ...
Flag Testing
# Test if a flag is set andi r4, r3, 0x0001 # Extract bit 0 (flag) cmpwi r4, 0 # Compare with zero bne flag_set # Branch if flag is set # Flag is clear b flag_clear flag_set: # Handle flag set case
Related Instructions
bc (Branch Conditional), beq (Branch if Equal), bge (Branch if Greater or Equal), blt (Branch if Less Than)