srwi
Shift Right Word Immediate - 54 00 00 3C
srwi

Instruction Syntax

Mnemonic Format Flags
srwi rA,rS,n None

Instruction Encoding

0
1
0
1
0
1
S
S
S
S
S
A
A
A
A
A
n
n
n
n
n
n
0
0
0
0
0
0

Field Bits Description
Primary Opcode 0-5 010101 (0x15)
rS 6-10 Source register
rA 11-15 Destination register
n 16-20 Shift count (0-31)
MB 21-25 00000 (Begin bit)
ME 26-30 00000 (End bit)

Operation

rA ← (rS) >> n

SRWI shifts the value in source register rS right by n bit positions and stores the result in destination register rA.

  1. Takes the value from source register rS
  2. Shifts it right by n bit positions (logical shift)
  3. Stores the result in destination register rA

Note: SRWI is a derived form of the RLWINM instruction with MB=0 and ME=31-n.

Affected Registers

General Purpose Registers (GPRs)

Examples

Basic Right Shift

# Shift r3 right by 2 bits
srwi r4, r3, 2     # r4 = r3 >> 2 (divide by 4)

# Shift r5 right by 8 bits
srwi r6, r5, 8     # r6 = r5 >> 8 (divide by 256)

Division by Powers of 2

# Divide by 2
srwi r3, r4, 1     # r3 = r4 / 2

# Divide by 4
srwi r3, r4, 2     # r3 = r4 / 4

# Divide by 8
srwi r3, r4, 3     # r3 = r4 / 8

# Divide by 16
srwi r3, r4, 4     # r3 = r4 / 16

Bit Field Extraction

# Extract upper 8 bits
srwi r3, r4, 24    # r3 = r4 >> 24 (bits 24-31)

# Extract middle 8 bits
srwi r3, r4, 8     # r3 = r4 >> 8 (bits 8-15)
andi r3, r3, 0xFF  # Mask to 8 bits

Address Calculation

# Convert byte offset to word offset
srwi r3, r4, 2     # r3 = r4 / 4 (byte offset to word offset)

# Extract page number from address
srwi r3, r4, 12    # r3 = r4 / 4096 (page number)

Data Packing/Unpacking

# Unpack 16-bit value from upper half
srwi r3, r4, 16    # r3 = r4 >> 16 (upper 16 bits)

# Extract bit field
srwi r3, r4, 5     # r3 = r4 >> 5
andi r3, r3, 0x1F  # Mask to 5 bits

Flag Testing

# Test if bit 7 is set
srwi r3, r4, 7     # Shift bit 7 to position 0
andi r3, r3, 1     # Extract bit 0
cmpwi r3, 0        # Compare with zero
bne bit_set        # Branch if bit was set

Related Instructions

slwi (Shift Left Word Immediate), rlwinm (Rotate Left Word Immediate then AND with Mask), srw (Shift Right Word), slw (Shift Left Word)

Back to Index