bdnzt
Branch if Decrement Not Zero True - 42 00 00 01
bdnzt
Instruction Syntax
Mnemonic | Format | Flags |
bdnzt | target | LK = 0 |
bdnztl | target | LK = 1 |
Note: BDNZT 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 true.
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
1
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 true) 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 true, a branch is taken to the target address. If LK=1, the address of the instruction following BDNZT 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 BDNZT
Next Instruction Address (NIA)
(if CTR ≠ 0 after decrement AND condition is true)
- Set to the branch target address
Examples
Conditional Loop
# Loop with condition check li r3, 10 # Load loop count mtctr r3 # Set CTR to loop count loop: # ... loop body ... cmpwi r4, 0 # Compare r4 with 0 bdnzt loop # Decrement CTR, branch if not zero AND r4 != 0
Array Search with Condition
# Search array for specific value lis r3, array@ha addi r3, r3, array@l li r4, 100 # Array size li r5, 42 # Value to search for mtctr r4 # Set CTR to array size search_loop: lwz r6, 0(r3) # Load array element cmpw r6, r5 # Compare with search value bdnzt search_loop # Continue if CTR != 0 AND element != search value addi r3, r3, 4 # Move to next element
String Processing with Condition
# Process string until null terminator lis r3, string@ha addi r3, r3, string@l li r4, 1000 # Maximum length mtctr r4 # Set CTR to max length process_loop: lbz r5, 0(r3) # Load character cmpwi r5, 0 # Check for null terminator bdnzt process_loop # Continue if CTR != 0 AND char != 0 addi r3, r3, 1 # Move to next character
Error Handling Loop
# Retry operation with error check li r3, 3 # Maximum retry attempts mtctr r3 # Set CTR to retry count retry_loop: # ... attempt operation ... lwz r4, error_flag # Check error flag cmpwi r4, 0 # Check if error occurred bdnzt retry_loop # Retry if CTR != 0 AND error occurred