bdnzf
Branch if Decrement Not Zero False - 42 00 00 02
bdnzf
Instruction Syntax
Mnemonic | Format | Flags |
bdnzf | target | LK = 0 |
bdnzfl | target | LK = 1 |
Note: BDNZF is a derived form of BC with BO=16 and BI=0, meaning it decrements CTR and branches if CTR is not zero AND the condition is false.
Instruction Encoding
0
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
B
D
I
A
A
A
A
A
A
A
A
A
A
A
A
A
2
Field | Bits | Description |
Primary Opcode | 0-5 | 010000 (0x10) |
BO | 6-10 | Branch Option (16 = decrement CTR, branch if CTR≠0) |
BI | 11-15 | Branch Input (0 = not used) |
BD | 16-29 | Branch Displacement (signed, 14-bit) |
AA | 30 | Absolute Address (0 = relative, 1 = absolute) |
LK | 31 | Link (0 = no link, 1 = link) |
Operation
CTR ← CTR - 1 if (CTR ≠ 0) AND (condition is false) then if (AA = 0) then NIA ← CIA + EXTS(BD || '00') else NIA ← EXTS(BD || '00') if (LK = 1) then LR ← CIA + 4
The Count Register (CTR) is decremented by 1. If the result is not zero AND the condition is false, a branch is taken to the target address. If LK=1, the address of the instruction following BDNZF is placed into LR.
Affected Registers
Count Register (CTR)
(always)
- Decremented by 1
Link Register (LR)
(if LK = 1)
- Updated with the address of the instruction following BDNZF
Next Instruction Address (NIA)
(if CTR ≠ 0 after decrement AND condition is false)
- Set to the branch target address
Examples
Loop Until Condition Met
# Loop until condition becomes true li r3, 10 # Load loop count mtctr r3 # Set CTR to loop count loop: # ... loop body ... cmpwi r4, 0 # Compare r4 with 0 bdnzf loop # Decrement CTR, branch if not zero AND r4 == 0
Array Processing Until Found
# Process array until value found lis r3, array@ha addi r3, r3, array@l li r4, 100 # Array size li r5, 42 # Value to find mtctr r4 # Set CTR to array size search_loop: lwz r6, 0(r3) # Load array element cmpw r6, r5 # Compare with search value bdnzf search_loop # Continue if CTR != 0 AND element == search value addi r3, r3, 4 # Move to next element
String Copy Until Null
# Copy string until null terminator found lis r3, source@ha addi r3, r3, source@l lis r4, dest@ha addi r4, r4, dest@l li r5, 1000 # Maximum length mtctr r5 # Set CTR to max length copy_loop: lbz r6, 0(r3) # Load character from source stb r6, 0(r4) # Store character to destination cmpwi r6, 0 # Check for null terminator bdnzf copy_loop # Continue if CTR != 0 AND char == 0 addi r3, r3, 1 # Increment source pointer addi r4, r4, 1 # Increment destination pointer
Wait for Ready Signal
# Wait for device ready signal li r3, 1000 # Maximum wait cycles mtctr r3 # Set CTR to max cycles wait_loop: lwz r4, device_status # Check device status cmpwi r4, 1 # Check if device is ready bdnzf wait_loop # Continue if CTR != 0 AND device not ready