Encoder

class iced_x86.Encoder(bitness, capacity=0)

Encodes instructions decoded by the decoder or instructions created by other code.

See also BlockEncoder which can encode any number of instructions.

Parameters:
  • bitness (int) – 16, 32 or 64

  • capacity (int) – (default = 0) Initial capacity of the byte buffer

Raises:

ValueError – If bitness is invalid

Examples:

from iced_x86 import *

# xchg ah,[rdx+rsi+16h]
data = b"\x86\x64\x32\x16"
decoder = Decoder(64, data, ip=0x1234_5678)
instr = decoder.decode()

encoder = Encoder(64)
try:
    instr_len = encoder.encode(instr, 0x5555_5555)
    assert instr_len == 4
except ValueError as ex:
    print(f"Failed to encode the instruction: {ex}")
    raise

# We're done, take ownership of the buffer
buffer = encoder.take_buffer()
assert buffer == b"\x86\x64\x32\x16"
bitness

Gets the bitness (16, 32 or 64)

Type:

int

encode(instruction, rip)

Encodes an instruction and returns the size of the encoded instruction

Parameters:
  • instruction (Instruction) – Instruction to encode

  • rip (int) – (u64) RIP of the encoded instruction

Returns:

Size of the encoded instruction

Return type:

int

Raises:

ValueError – If it failed to encode the instruction (eg. a target branch / RIP-rel operand is too far away)

Examples:

from iced_x86 import *

# je short $+4
data = b"\x75\x02"
decoder = Decoder(64, data, ip=0x1234_5678)
instr = decoder.decode()

encoder = Encoder(64)
try:
    # Use a different IP (orig rip + 0x10)
    instr_len = encoder.encode(instr, 0x1234_5688)
    assert instr_len == 2
except ValueError as ex:
    print(f"Failed to encode the instruction: {ex}")
    raise

# We're done, take ownership of the buffer
buffer = encoder.take_buffer()
assert buffer == b"\x75\xF2"
evex_lig

(u8) Value of the EVEX.L'L bits to use if it’s an instruction that ignores the bits. Default is 0.

Type:

int

evex_wig

(u8) Value of the EVEX.W bit to use if it’s an instruction that ignores the bit. Default is 0.

Type:

int

get_constant_offsets()

Gets the offsets of the constants (memory displacement and immediate) in the encoded instruction.

The caller can use this information to add relocations if needed.

Returns:

Offsets and sizes of immediates

Return type:

ConstantOffsets

mvex_wig

(u8) Value of the MVEX.W bit to use if it’s an instruction that ignores the bit. Default is 0.

Type:

int

prevent_vex2

Disables 2-byte VEX encoding and encodes all VEX instructions with the 3-byte VEX encoding

Type:

bool

take_buffer()

Returns the buffer and initializes the internal buffer to an empty array.

Should be called when you’ve encoded all instructions and need the raw instruction bytes.

Returns:

The encoded instructions

Return type:

bytes

vex_lig

(u8) Value of the VEX.L bit to use if it’s an instruction that ignores the bit. Default is 0.

Type:

int

vex_wig

(u8) Value of the VEX.W bit to use if it’s an instruction that ignores the bit. Default is 0.

Type:

int

write_u8(value)

Writes a byte to the output buffer

Parameters:

value (int) – (u8) Value to write

Examples:

from iced_x86 import *

# je short $+4
data = b"\x75\x02"
decoder = Decoder(64, data, ip=0x1234_5678)
instr = decoder.decode()

encoder = Encoder(64)
# Add a random byte
encoder.write_u8(0x90)

try:
    # Use a different IP (orig rip + 0x10)
    instr_len = encoder.encode(instr, 0x1234_5688)
    assert instr_len == 2
except ValueError as ex:
    print(f"Failed to encode the instruction: {ex}")
    raise

# Add a random byte
encoder.write_u8(0x90)

# We're done, take ownership of the buffer
buffer = encoder.take_buffer()
assert buffer == b"\x90\x75\xF2\x90"