cmpl
Compare Logical - 7C 00 00 40
cmpl

Instruction Syntax

Mnemonic Format Flags
cmpl crfD,L,rA,rB -
cmplw crfD,rA,rB L = 0 (extended mnemonic)

Instruction Encoding

0
1
1
1
1
1
c
c
c
0
L
A
A
A
A
A
B
B
B
B
B
0
0
0
0
1
0
0
0
0
0
0

Field Bits Description
Primary Opcode 0-5 011111 (0x1F)
crfD 6-8 Condition register field
Reserved 9 Should be zero
L 10 64-bit comparison (0 = 32-bit)
rA 11-15 Source register A
rB 16-20 Source register B
Reserved 21 Should be zero
XO 22-30 32 (Extended opcode)
Reserved 31 Should be zero

Operation

a ← (rA)
b ← (rB)
if a u b then c ← 0b010
else c ← 0b001
CR[4×crfD+32:4×crfD+35] ← c || XER[SO]

The contents of rA are compared with the contents of rB treating the operands as unsigned integers. The result of the comparison is placed into condition register field crfD.

Note: In 32-bit implementations, if L=1 the instruction form is invalid.

Affected Registers

Condition Register (crfD field)

(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 Unsigned Comparison

cmpl cr0, 0, r3, r4    # Compare r3 and r4 unsigned, set CR0
cmplw cr1, r5, r6      # Compare r5 and r6 unsigned (32-bit), set CR1

Comparing Addresses/Pointers

cmpl cr0, 0, r3, r4    # Compare two addresses
blt lower_address      # Branch if r3 < r4 (unsigned)
beq same_address       # Branch if addresses are equal
bgt higher_address     # Branch if r3 > r4 (unsigned)

Range Checking for Unsigned Values

# Check if unsigned value is within bounds
cmpl cr0, 0, r3, r4    # Compare with lower bound
blt out_of_range       # Branch if below range
cmpl cr0, 0, r3, r5    # Compare with upper bound  
bgt out_of_range       # Branch if above range
# Value is within range

Array Index Validation

# Validate array index (unsigned)
cmpl cr0, 0, r3, r4    # Compare index with array size
bge bounds_error       # Branch if index >= size
# Index is valid (0 <= index < size)

Memory Address Comparison

# Compare memory addresses for sorting
cmpl cr0, 0, r10, r11  # Compare two pointers
blt ptr1_lower         # r10 points to lower address
beq same_location      # Both point to same location
bgt ptr1_higher        # r10 points to higher address

Unsigned Arithmetic Overflow Check

# Check for unsigned addition overflow
add r5, r3, r4         # Add two unsigned values
cmpl cr0, 0, r5, r3    # Compare result with first operand
blt overflow_detected  # If result < operand, overflow occurred

Related Instructions

cmp, cmpi, cmpli