extsb
Extend Sign Byte - 7C 00 07 74
extsb

Instruction Syntax

Mnemonic Format Flags
extsb rA,rS Rc = 0
extsb. rA,rS Rc = 1

Instruction Encoding

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

Field Bits Description
Primary Opcode 0-5 011111 (0x1F)
rS 6-10 Source register S
rA 11-15 Destination register A
Reserved 16-20 00000
Reserved 21 0
XO 22-30 1110111010 (954)
Rc 31 Record Condition Register

Operation

rA ← EXTS(rS[24-31])

The least significant byte of rS (bits 24-31) is sign-extended to 32 bits and placed into rA. If bit 24 of rS is 0, then bits 0-23 of rA are set to 0. If bit 24 of rS is 1, then bits 0-23 of rA are set to 1.

Note: This instruction is used to convert signed 8-bit values to signed 32-bit values.

Affected Registers

Condition Register (CR0 field)

(if Rc = 1)

For more information on condition codes see Section 2.1.3, "Condition Register," in the PowerPC Microprocessor Family: The Programming Environments manual.

Examples

Basic Sign Extension

li r1, 0x0080       # Load positive byte (0x80 = 128 unsigned)
extsb r2, r1        # r2 = 0xFFFFFF80 (-128 signed)

li r3, 0x007F       # Load positive byte (0x7F = 127)
extsb r4, r3        # r4 = 0x0000007F (+127 signed)

Converting Signed Characters

# Load a signed character and extend to word
lbz r1, char_value(r0)  # Load unsigned byte
extsb r2, r1            # Sign extend to 32-bit signed value
# r2 now contains proper signed 32-bit representation

Array Processing with Sign Extension

# Process array of signed bytes
lis r3, signed_bytes@ha
addi r3, r3, signed_bytes@l
li r4, 0                # Index counter
li r5, 10               # Array size

process_loop:
    lbzx r1, r3, r4     # Load byte from array
    extsb r1, r1        # Sign extend to 32 bits
    # Process the signed value in r1
    addi r4, r4, 1      # Next element
    cmpw r4, r5         # Check if done
    blt process_loop    # Continue if more elements

Sign Extension with Condition Check

lbz r1, input_byte(r0)  # Load byte value
extsb. r2, r1           # Sign extend and set condition register
blt negative_value      # Branch if result is negative
beq zero_value          # Branch if result is zero
bgt positive_value      # Branch if result is positive

Protocol Buffer Decoding

# Decode signed byte from protocol buffer
lis r3, buffer@ha
addi r3, r3, buffer@l
lbz r1, 0(r3)           # Load encoded byte
extsb r2, r1            # Convert to signed 32-bit value
stw r2, decoded_value(r0)  # Store decoded value

Temperature Conversion

# Convert signed byte temperature to 32-bit value
lbz r1, temp_sensor(r0) # Read temperature sensor (signed byte)
extsb r2, r1            # Sign extend to 32 bits
# r2 now contains temperature as signed 32-bit value
# Range: -128 to +127 degrees

Arithmetic with Byte Values

# Add signed byte values correctly
lbz r1, value1(r0)      # Load first signed byte
lbz r2, value2(r0)      # Load second signed byte
extsb r1, r1            # Sign extend first value
extsb r2, r2            # Sign extend second value
add r3, r1, r2          # Add as 32-bit signed values
# Result in r3 is correct signed sum

Related Instructions

extsh, lbz, lbzx, stb

Back to Index