mflr
Move from Link Register - 7C 08 02 A6
mflr

Instruction Syntax

Mnemonic Format Flags
mflr 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
0
0
0
0
0
0
0
0
0
0
0

Field Bits Description
Primary Opcode 0-5 011111 (0x1F)
rD 6-10 Destination register
Reserved 11-20 Should be zero
XO 21-30 0101000111 (0x287) - Extended opcode
Reserved 31 Should be zero

Operation

rD ← LR

The contents of the Link Register (LR) are copied to general-purpose register rD. The LR contains the return address for the current function or subroutine.

Note: This instruction provides a way to save the current return address. This is particularly useful for function prologues, context switching, or when implementing complex calling conventions that require preserving the return address.

Affected Registers

General Purpose Register

(always)

Link Register (LR)

(read only)

For more information on the Link Register see Section 2.1.6, "Link Register (LR)," in the PowerPC Microprocessor Family: The Programming Environments manual.

Examples

Basic Link Register Save

# Save current return address
mflr r3                 # Copy LR to r3
# ... perform operations that might modify LR ...
mtlr r3                # Restore LR from r3

Function Prologue

# Standard function prologue
function_prologue:
    mflr r0             # Save return address
    stw r0, 4(r1)       # Store on stack
    stwu r1, -16(r1)    # Allocate stack frame
    # ... function code ...
    lwz r0, 20(r1)      # Restore return address
    mtlr r0             # Restore LR
    addi r1, r1, 16     # Deallocate stack frame
    blr                 # Return

Context Switching

# Save context before function call
mflr r10                # Save return address
# ... save other registers ...

bl some_function        # Function call

# Restore context after function call
mtlr r10               # Restore return address
# ... restore other registers ...

Interrupt Handler

# Interrupt handler entry
interrupt_handler:
    mflr r20            # Save return address
    # ... handle interrupt ...
    mtlr r20           # Restore return address
    rfi                # Return from interrupt

Nested Function Calls

# Handle nested function calls
outer_function:
    mflr r4             # Save outer return address
    bl inner_function   # Call inner function
    mtlr r4            # Restore outer return address
    blr                # Return from outer function

inner_function:
    # Inner function code
    blr                # Return to outer function

Exception Handler

# Exception handler setup
exception_handler:
    mflr r5             # Save return address
    # ... handle exception ...
    mtlr r5            # Restore return address
    rfi                # Return from exception

Debugging Return Address

# Debug return address
debug_point:
    mflr r6             # Save current return address
    # ... perform operations ...
    
    # Debug: check if return address changed unexpectedly
    mflr r7             # Get current return address
    cmpw r6, r7         # Compare saved vs current
    beq lr_ok          # Branch if unchanged
    # Handle unexpected LR change

Custom Calling Convention

# Custom calling convention
custom_call:
    mflr r8             # Save return address
    # ... custom calling logic ...
    mtlr r8            # Restore return address
    blr                # Return

Related Instructions

mtlr, blr, bl, bclr, mfspr

Back to Index