andi
AND Immediate - 70 00 00 00
andi

Instruction Syntax

Mnemonic Format Flags
andi. rA,rS,UIMM Rc = 1
andi rA,rS,UIMM Rc = 0

Instruction Encoding

0
1
1
1
0
0
0
0
S
S
S
S
S
A
A
A
A
A
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
Rc

Field Bits Description
Primary Opcode 0-5 011100 (0x1C)
rS 6-10 Source register
rA 11-15 Destination register
UIMM 16-31 Unsigned immediate value (16-bit)
Rc 31 Record Condition Register

Operation

rA ← (rS) & ZEXT(UIMM)

ANDI performs a bitwise AND operation between the source register and a zero-extended 16-bit unsigned immediate value.

  1. Takes the value from source register rS
  2. Zero-extends the 16-bit immediate value to 32 bits
  3. Performs bitwise AND operation
  4. Stores the result in destination register rA

Note: ANDI is commonly used for masking operations, clearing specific bits, or testing bit patterns.

Affected Registers

General Purpose Registers (GPRs)

Condition Register (CR0 field)

(if Rc = 1)

Examples

Basic AND Operation

# Clear upper 16 bits of a register
andi r3, r4, 0xFFFF    # r3 = r4 & 0x0000FFFF
andi. r3, r4, 0xFFFF   # Same as above, but also sets condition register

Bit Masking

# Extract lower 8 bits
andi r3, r4, 0xFF      # r3 = r4 & 0x000000FF

# Clear specific bits
andi r3, r4, 0xFFFE    # Clear bit 0: r3 = r4 & 0xFFFFFFFE
andi r3, r4, 0xFFFD    # Clear bit 1: r3 = r4 & 0xFFFFFFFD

Testing Bit Patterns

# Test if bits 3-6 are set
andi r3, r4, 0x78      # r3 = r4 & 0x00000078 (bits 3-6)
cmpwi r3, 0x78         # Compare with expected pattern
beq bits_set           # Branch if all bits are set

Flag Checking

# Check if a specific flag is set
andi r3, r4, 0x0001    # Test bit 0 (flag)
cmpwi r3, 0            # Compare with zero
bne flag_set           # Branch if flag is set

Register Clearing

# Clear a register to zero
andi r3, r3, 0         # r3 = r3 & 0x00000000 = 0

# Clear only lower 16 bits
andi r3, r3, 0xFFFF0000  # Keep upper 16 bits, clear lower 16

Data Validation

# Validate data range (0-255)
andi r3, r4, 0xFF      # Ensure value is in valid range
cmpw r3, r4            # Compare with original
bne invalid_data       # Branch if data was outside range

Related Instructions

and (AND), andis (AND Immediate Shifted), ori (OR Immediate), xori (XOR Immediate)

Back to Index