mcrf
Move Condition Register Field - 4C 00 00 00
mcrf

Instruction Syntax

Mnemonic Format Flags
mcrf crfD,crfS None

Instruction Encoding

0
1
1
1
1
1
D
D
D
0
0
S
S
S
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
0

Field Bits Description
Primary Opcode 0-5 011111 (0x1F)
crfD 6-8 Destination condition register field
Reserved 9-10 Should be zero
crfS 11-13 Source condition register field
Reserved 14-20 Should be zero
XO 21-30 0x0A0 (160) - Extended opcode
Reserved 31 Should be zero

Operation

CR[4×crfD:4×crfD+3] ← CR[4×crfS:4×crfS+3]

The contents of condition register field crfS are copied to condition register field crfD. Each condition register field contains 4 bits representing the condition codes (LT, GT, EQ, SO).

Note: This instruction allows for efficient manipulation of condition codes without requiring additional comparison operations. It's particularly useful in complex conditional logic where condition codes need to be preserved or transferred between different contexts.

Affected Registers

Condition Register (CR)

(always)

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 Field Copy

# Copy condition codes from CR0 to CR1
cmpwi cr0, r3, 0        # Compare r3 with 0, set CR0
mcrf cr1, cr0           # Copy CR0 to CR1

Preserve Condition Codes

# Save condition codes before another comparison
cmpwi cr0, r4, 10       # Compare r4 with 10, set CR0
mcrf cr7, cr0           # Save result in CR7
cmpwi cr0, r5, 20       # New comparison overwrites CR0
# CR7 still contains result of r4 comparison

Multiple Condition Tracking

# Track multiple comparison results
cmpwi cr0, r3, 0        # Check if r3 is zero
mcrf cr1, cr0           # Save zero check result
cmpwi cr0, r4, 100      # Check if r4 is 100
mcrf cr2, cr0           # Save range check result
cmpwi cr0, r5, -50      # Check if r5 is negative
mcrf cr3, cr0           # Save sign check result

Complex Conditional Logic

# Implement complex conditional logic
cmpwi cr0, r3, 0        # Check first condition
mcrf cr1, cr0           # Save first condition
cmpwi cr0, r4, 0        # Check second condition
mcrf cr2, cr0           # Save second condition
cmpwi cr0, r5, 0        # Check third condition
mcrf cr3, cr0           # Save third condition

# Now can use all three conditions independently
bt cr1, first_true      # Branch if first condition true
bt cr2, second_true     # Branch if second condition true
bt cr3, third_true      # Branch if third condition true

Function Return Value Checking

# Check multiple function return values
bl function1            # Call first function
cmpwi cr0, r3, 0        # Check return value
mcrf cr1, cr0           # Save function1 result

bl function2            # Call second function
cmpwi cr0, r3, 0        # Check return value
mcrf cr2, cr0           # Save function2 result

# Check both results
bt cr1, func1_success   # Branch if function1 succeeded
bt cr2, func2_success   # Branch if function2 succeeded

Loop Condition Preservation

# Preserve loop condition for nested logic
loop_start:
    cmpwi cr0, r4, 0    # Check loop condition
    mcrf cr7, cr0       # Save loop condition
    beq cr0, loop_end   # Exit if done
    
    # Nested condition that might overwrite CR0
    cmpwi cr0, r5, 10   # Some other comparison
    bt cr0, do_something
    
    # Restore loop condition
    mcrf cr0, cr7       # Restore original loop condition
    b loop_start        # Continue loop

Error Condition Tracking

# Track multiple error conditions
cmpwi cr0, r3, -1       # Check for error code -1
mcrf cr1, cr0           # Save error -1 condition
cmpwi cr0, r4, -2       # Check for error code -2
mcrf cr2, cr0           # Save error -2 condition
cmpwi cr0, r5, -3       # Check for error code -3
mcrf cr3, cr0           # Save error -3 condition

# Handle different error types
beq cr1, handle_error1  # Handle error -1
beq cr2, handle_error2  # Handle error -2
beq cr3, handle_error3  # Handle error -3

Range Checking

# Check if value is in multiple ranges
cmpwi cr0, r3, 0        # Check if >= 0
mcrf cr1, cr0           # Save >= 0 result
cmpwi cr0, r3, 100      # Check if <= 100
mcrf cr2, cr0           # Save <= 100 result
cmpwi cr0, r3, 50       # Check if == 50
mcrf cr3, cr0           # Save == 50 result

# Use range information
bt cr1, positive_value  # Branch if >= 0
bt cr2, in_range        # Branch if <= 100
beq cr3, exact_50       # Branch if == 50

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

# Check status combinations
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

Condition Code Restoration

# Restore condition codes after function call
cmpwi cr0, r3, 0        # Set up condition codes
mcrf cr7, cr0           # Save them before function call

bl some_function        # Function call (might modify CR0)

mcrf cr0, cr7           # Restore original condition codes
beq cr0, original_true  # Use restored condition

Related Instructions

mcrfs, mcrxr, mfcr, mtcrf

Back to Index