bctr
Branch to Count Register - 4E 80 04 20
bctr
Instruction Syntax
Mnemonic | Format | Flags |
bctr | LK = 0 | |
bctrl | LK = 1 |
Note: BCTR is a derived form of BCCTR with BO=20 and BI=0, meaning it always branches (unconditional).
Instruction Encoding
0
1
0
0
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Field | Bits | Description |
Primary Opcode | 0-5 | 010011 (0x13) |
BO | 6-10 | Branch Option (20 = always branch) |
BI | 11-15 | Branch Input (0 = not used) |
LK | 31 | Link (0 = no link, 1 = link) |
Operation
if (LK == 0) then NIA ← CTR else NIA ← CTR LR ← CIA + 4
Branch to the address contained in the Count Register (CTR). If LK=1, the address of the instruction following the BCTR is placed into LR.
Affected Registers
Link Register (LR)
(if LK = 1)
- Updated with the address of the instruction following BCTR
Next Instruction Address (NIA)
- Set to the value in CTR
Examples
Function Pointer Call
# Call function through function pointer mtctr r3 # Load function address into CTR bctrl # Branch to function and link
Switch Statement Implementation
# Jump table implementation lis r3, jump_table@ha addi r3, r3, jump_table@l slwi r4, r4, 2 # Multiply index by 4 (word size) lwzx r5, r3, r4 # Load jump address from table mtctr r5 # Load address into CTR bctr # Jump to address
Dynamic Code Execution
# Execute dynamically generated code lis r3, code_addr@ha addi r3, r3, code_addr@l mtctr r3 # Set CTR to code address bctr # Execute the code
Return from Function Pointer
# Return from function called via function pointer mflr r3 # Save return address mtctr r3 # Load return address into CTR bctr # Return to caller