lbz
Load Byte and Zero - 88 00 00 00
lbz

Instruction Syntax

Mnemonic Format Flags
lbz rD,d(rA) -

Instruction Encoding

1
0
0
0
1
0
D
D
D
D
D
A
A
A
A
A
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d

Field Bits Description
Primary Opcode 0-5 100010 (0x22)
rD 6-10 Destination register
rA 11-15 Source register A
d 16-31 16-bit signed displacement

Operation

if rA = 0 then EA ← EXTS(d)
else EA ← (rA) + EXTS(d)
rD ← 0x00 || MEM(EA, 1)

A byte is loaded from memory and placed in the low-order 8 bits of register rD. The upper 24 bits of rD are set to zero. The effective address is computed by adding the sign-extended displacement to the contents of register rA, or zero if rA is 0.

Note: This instruction loads an 8-bit value from memory and zero-extends it to 32 bits. The effective address calculation treats rA=0 as the value 0, not register r0.

Affected Registers

None - This instruction does not affect any condition register fields or XER register bits.

For more information on memory addressing see Section 2.1.6, "Effective Address Calculation," in the PowerPC Microprocessor Family: The Programming Environments manual.

Examples

Basic Byte Loading

lbz r3, 0(r1)           # Load byte from address in r1
lbz r4, 100(r2)         # Load byte from address r2+100
lbz r5, -50(r3)         # Load byte from address r3-50

Loading from Absolute Address

lbz r3, 0x1000(r0)      # Load byte from absolute address 0x1000
# Note: r0 treated as value 0, not register contents

Character String Processing

# Process null-terminated string
lis r3, string_ptr@ha
addi r3, r3, string_ptr@l
li r4, 0                # Character index

string_loop:
    lbz r5, 0(r3)       # Load current character
    cmpwi r5, 0         # Check for null terminator
    beq string_done     # Branch if end of string
    
    # Process character in r5
    bl process_char
    
    addi r3, r3, 1      # Move to next character
    addi r4, r4, 1      # Increment count
    b string_loop

string_done:

Byte Array Access

# Access elements in byte array
lis r3, byte_array@ha
addi r3, r3, byte_array@l
li r4, 5                # Array index
lbz r5, 0(r3, r4)       # Would use lbzx for indexed access
# Instead use displacement:
add r6, r3, r4          # Calculate address
lbz r7, 0(r6)           # Load array[5]

Loading Configuration Bytes

# Load device configuration bytes
lis r3, config_base@ha
addi r3, r3, config_base@l

lbz r4, 0(r3)           # Load config byte 0
lbz r5, 1(r3)           # Load config byte 1  
lbz r6, 2(r3)           # Load config byte 2
lbz r7, 3(r3)           # Load config byte 3

# Combine into word (big-endian)
slwi r4, r4, 24         # Shift byte 0 to bits 0-7
slwi r5, r5, 16         # Shift byte 1 to bits 8-15
slwi r6, r6, 8          # Shift byte 2 to bits 16-23
or r8, r4, r5           # Combine bytes 0,1
or r8, r8, r6           # Add byte 2
or r8, r8, r7           # Add byte 3 - r8 now has full word

Status Register Reading

# Read device status registers
lis r3, device_base@ha
addi r3, r3, device_base@l

lbz r4, STATUS_REG(r3)  # Load status byte
andi. r5, r4, 0x80      # Check bit 7 (ready flag)
beq device_not_ready    # Branch if not ready

andi. r5, r4, 0x40      # Check bit 6 (error flag)
bne device_error        # Branch if error

# Device ready and no error
lbz r6, DATA_REG(r3)    # Load data byte

Bitmap Font Character Loading

# Load bitmap font character data
lis r3, font_data@ha
addi r3, r3, font_data@l
li r4, 'A'              # Character to load
li r5, 8                # 8 bytes per character
mullw r6, r4, r5        # Calculate offset
add r7, r3, r6          # Character data address

# Load 8 bytes of character bitmap
lbz r8, 0(r7)           # Row 0
lbz r9, 1(r7)           # Row 1
lbz r10, 2(r7)          # Row 2
lbz r11, 3(r7)          # Row 3
lbz r12, 4(r7)          # Row 4
lbz r13, 5(r7)          # Row 5
lbz r14, 6(r7)          # Row 6
lbz r15, 7(r7)          # Row 7

Network Packet Header Parsing

# Parse network packet header bytes
lis r3, packet_buffer@ha
addi r3, r3, packet_buffer@l

lbz r4, 0(r3)           # Load version/header length
lbz r5, 1(r3)           # Load type of service
lbz r6, 2(r3)           # Load total length high byte
lbz r7, 3(r3)           # Load total length low byte

# Extract header length (lower 4 bits of first byte)
andi. r8, r4, 0x0F      # Header length in 32-bit words
slwi r8, r8, 2          # Convert to bytes

# Combine total length
slwi r6, r6, 8          # High byte to upper position
or r9, r6, r7           # Combine high and low bytes

Game State Loading

# Load game state from save data
lis r3, save_data@ha
addi r3, r3, save_data@l

lbz r4, PLAYER_LEVEL(r3)    # Load player level
lbz r5, PLAYER_HEALTH(r3)   # Load health percentage
lbz r6, PLAYER_MANA(r3)     # Load mana percentage
lbz r7, CURRENT_WORLD(r3)   # Load world number
lbz r8, CURRENT_STAGE(r3)   # Load stage number

# Validate loaded values
cmpwi r4, 99            # Check max level
bgt invalid_save        # Invalid if > 99
cmpwi r5, 100           # Check max health
bgt invalid_save        # Invalid if > 100

Color Component Extraction

# Extract RGB components from 32-bit color value
# Assuming color stored as 4 bytes: A, R, G, B
lis r3, color_buffer@ha
addi r3, r3, color_buffer@l

lbz r4, 0(r3)           # Load alpha component
lbz r5, 1(r3)           # Load red component
lbz r6, 2(r3)           # Load green component
lbz r7, 3(r3)           # Load blue component

# Apply alpha blending calculation
# NewColor = (SourceColor * Alpha + DestColor * (255 - Alpha)) / 255
li r8, 255
sub r9, r8, r4          # 255 - Alpha
mullw r10, r5, r4       # Red * Alpha
mullw r11, r6, r4       # Green * Alpha
mullw r12, r7, r4       # Blue * Alpha

MIDI Data Parsing

# Parse MIDI message bytes
lis r3, midi_buffer@ha
addi r3, r3, midi_buffer@l

lbz r4, 0(r3)           # Load status byte
andi. r5, r4, 0x80      # Check if status byte (bit 7 set)
beq data_byte           # Branch if data byte

# Parse status byte
andi. r6, r4, 0xF0      # Extract command nibble
cmpi cr0, 0, r6, 0x90   # Check for note on
beq note_on_msg
cmpi cr0, 0, r6, 0x80   # Check for note off
beq note_off_msg

note_on_msg:
    lbz r7, 1(r3)       # Load note number
    lbz r8, 2(r3)       # Load velocity
    b process_note_on

note_off_msg:
    lbz r7, 1(r3)       # Load note number
    lbz r8, 2(r3)       # Load velocity (usually 0)
    b process_note_off

data_byte:
    # Handle running status
    mr r7, r4           # First data byte
    lbz r8, 1(r3)       # Second data byte

Related Instructions

lbzu, lbzx, lbzux, stb, lhz, lwz

Back to Index