cmpi
Compare Immediate - 2C 00 00 00
cmpi

Instruction Syntax

Mnemonic Format Flags
cmpi crfD,L,rA,SIMM -
cmpwi crfD,rA,SIMM L = 0 (extended mnemonic)

Instruction Encoding

0
0
1
0
1
1
c
c
c
0
L
A
A
A
A
A
S
I
M
M
S
I
M
M
S
I
M
M
S
I
M
M

Field Bits Description
Primary Opcode 0-5 001011 (0x0B)
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
SIMM 16-31 16-bit signed immediate value

Operation

a ← (rA)
if a < EXTS(SIMM) then c ← 0b100
else if a > EXTS(SIMM) then c ← 0b010
else c ← 0b001
CR[4×crfD+32:4×crfD+35] ← c || XER[SO]

The contents of rA are compared with the sign-extended immediate value treating the operands as signed 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 Immediate Comparison

cmpi cr0, 0, r3, 100   # Compare r3 with 100, set CR0
cmpwi cr1, r5, -50     # Compare r5 with -50 (32-bit), set CR1

Testing for Zero

cmpwi cr0, r3, 0       # Test if r3 is zero
beq is_zero            # Branch if r3 == 0
blt is_negative        # Branch if r3 < 0
bgt is_positive        # Branch if r3 > 0

Range Validation

# Check if value is in range 1-10
cmpwi cr0, r3, 1       # Compare with minimum
blt out_of_range       # Branch if < 1
cmpwi cr0, r3, 10      # Compare with maximum
bgt out_of_range       # Branch if > 10
# Value is in valid range

Loop Counter Check

# Check loop termination condition
cmpwi cr0, r4, 0       # Compare counter with 0
ble loop_done          # Branch if counter <= 0
# Continue loop

Constant Comparisons

# Compare with various constants
cmpwi cr0, r3, -1      # Compare with -1
beq negative_one       # Branch if r3 == -1
cmpwi cr1, r4, 32      # Compare with 32
bge thirty_two_plus    # Branch if r4 >= 32
cmpwi cr2, r5, 0x7FFF  # Compare with max positive 16-bit
bgt overflow_check     # Branch if > 32767

Related Instructions

cmp, cmpl, cmpli

Back to Index