mfcr
Move from Condition Register - 4C 00 00 02
mfcr
Instruction Syntax
Mnemonic | Format | Flags |
mfcr | rD | None |
Instruction Encoding
0
1
1
1
1
1
D
D
D
D
D
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
Field | Bits | Description |
Primary Opcode | 0-5 | 011111 (0x1F) |
rD | 6-10 | Destination register |
Reserved | 11-20 | Should be zero |
XO | 21-30 | 0x0A2 (162) - Extended opcode |
Reserved | 31 | Should be zero |
Operation
rD ← CR
The entire contents of the Condition Register (CR) are copied to general-purpose register rD. The CR contains 32 bits representing all 8 condition register fields (CR0-CR7), each containing 4 bits (LT, GT, EQ, SO).
Note: This instruction provides a way to save the complete state of all condition register fields. This is particularly useful for context switching, interrupt handling, or when complex conditional logic requires preserving all condition codes.
Affected Registers
General Purpose Register
(always)
- rD - Loaded with complete Condition Register contents
Condition Register (CR)
(read only)
- CR - Source register (read only)
For more information on condition codes see Section 2.1.3, "Condition Register," in the PowerPC Microprocessor Family: The Programming Environments manual.
Examples
Basic Condition Register Save
# Save complete condition register state mfcr r3 # Copy CR to r3 # ... perform operations that might modify CR ... mtcrf 0xFF, r3 # Restore CR from r3
Context Switching
# Save context before function call mfcr r10 # Save condition register # ... save other registers ... bl some_function # Function call # Restore context after function call mtcrf 0xFF, r10 # Restore condition register # ... restore other registers ...
Interrupt Handler
# Interrupt handler entry interrupt_handler: mfcr r20 # Save condition register # ... handle interrupt ... mtcrf 0xFF, r20 # Restore condition register rfi # Return from interrupt
Complex Conditional Logic
# Save condition codes for complex logic cmpwi cr0, r3, 0 # First comparison cmpwi cr1, r4, 10 # Second comparison cmpwi cr2, r5, 20 # Third comparison mfcr r6 # Save all condition codes # Perform operations that might modify CR add r3, r3, r4 sub r5, r5, r6 # Restore and use saved condition codes mtcrf 0xFF, r6 # Restore all condition codes bt cr0, first_true # Use first comparison result bt cr1, second_true # Use second comparison result bt cr2, third_true # Use third comparison result
Nested Function Calls
# Preserve condition codes across nested calls cmpwi cr0, r3, 0 # Set up condition codes mfcr r7 # Save them bl outer_function # Outer function call # Outer function might call inner function # Inner function might modify CR mtcrf 0xFF, r7 # Restore original condition codes beq cr0, original_zero # Use original comparison result
Multiple Condition Tracking
# Track multiple conditions simultaneously cmpwi cr0, r3, 0 # Check zero cmpwi cr1, r4, 100 # Check range cmpwi cr2, r5, -50 # Check negative cmpwi cr3, r6, 200 # Check upper bound mfcr r8 # Save all conditions # Process data (might modify CR) process_data: # ... data processing ... # Check all original conditions mtcrf 0xFF, r8 # Restore all conditions bt cr0, was_zero # Handle zero case bt cr1, in_range # Handle range case bt cr2, was_negative # Handle negative case bt cr3, was_large # Handle large value case
Error Condition Preservation
# Preserve error conditions across operations cmpwi cr0, r3, -1 # Check for error -1 cmpwi cr1, r4, -2 # Check for error -2 cmpwi cr2, r5, -3 # Check for error -3 mfcr r9 # Save error conditions # Perform recovery operations bl recovery_function # Might modify CR # Check original error conditions mtcrf 0xFF, r9 # Restore error conditions beq cr0, handle_error1 # Handle error -1 beq cr1, handle_error2 # Handle error -2 beq cr2, handle_error3 # Handle error -3
Loop Condition Preservation
# Preserve loop conditions for nested logic loop_start: cmpwi cr0, r4, 0 # Check loop condition mfcr r10 # Save loop condition # Nested operations that might modify CR cmpwi cr0, r5, 10 # Some other comparison bt cr0, do_something # Restore loop condition mtcrf 0xFF, r10 # Restore original conditions beq cr0, loop_end # Exit if loop condition false b loop_start # Continue loop
Status Flag Management
# Manage multiple status flags cmpwi cr0, r3, 0 # Zero flag cmpwi cr1, r4, 0 # Overflow flag cmpwi cr2, r5, 0 # Error flag cmpwi cr3, r6, 0 # Ready flag mfcr r11 # Save all status flags # Perform system operations bl system_operation # Might modify CR # Check all status conditions mtcrf 0xFF, r11 # Restore status flags bt cr0, zero_detected # Handle zero condition bt cr1, overflow_detected # Handle overflow bt cr2, error_detected # Handle error bt cr3, ready_detected # Handle ready condition
Debugging Support
# Save condition register for debugging debug_point: mfcr r12 # Save CR for debugging # ... perform operations ... # Debug: check if conditions changed unexpectedly mfcr r13 # Get current CR cmpw r12, r13 # Compare saved vs current beq conditions_ok # Branch if unchanged # Handle unexpected condition change