mtctr
Move to Count Register - 7C 09 03 A6
mtctr
Instruction Syntax
Mnemonic | Format | Description |
mtctr | rS | Move to Count Register |
Instruction Encoding
0
1
1
1
1
1
S
S
S
S
S
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
0
0
0
0
0
Field | Bits | Description |
Primary Opcode | 0-5 | 011111 (0x1F) |
rS | 6-10 | Source register |
Reserved | 11-15 | 00000 |
Reserved | 16-20 | 00000 |
Reserved | 21-29 | 000000000 |
Reserved | 30 | 0 |
SPR | 31-31 | Special Purpose Register (9 = CTR) |
Operation
CTR ← (rS)
The contents of register rS are copied into the Count Register (CTR).
Affected Registers
Count Register (CTR)
(always)
- Updated with the value from rS
Examples
Basic Move to Count Register
mtctr r3 # Copy value from r3 to CTR
Function Pointer Setup
# Set up function pointer call lis r3, function_addr@ha addi r3, r3, function_addr@l mtctr r3 # Load function address into CTR bctrl # Call function
Loop Counter Setup
# Set up loop counter li r3, 10 # Load loop count mtctr r3 # Set CTR to loop count loop: # loop body bdnz loop # Decrement CTR and branch if not zero
Jump Table Implementation
# Jump table using CTR lis r3, jump_table@ha addi r3, r3, jump_table@l slwi r4, r4, 2 # Multiply index by 4 lwzx r5, r3, r4 # Load jump address mtctr r5 # Load address into CTR bctr # Jump to address
Dynamic Code Execution
# Execute code at runtime address lis r3, dynamic_code@ha addi r3, r3, dynamic_code@l mtctr r3 # Set CTR to code address bctr # Execute the code