Formatter¶
- class iced_x86.Formatter(syntax)¶
x86 formatter that supports GNU Assembler, Intel XED, masm and nasm syntax
- Parameters:
syntax (
FormatterSyntax) – Formatter syntax
Examples:
from iced_x86 import * data = b"\x62\xF2\x4F\xDD\x72\x50\x01" decoder = Decoder(64, data) instr = decoder.decode() formatter = Formatter(FormatterSyntax.MASM) formatter.uppercase_mnemonics = True disasm = formatter.format(instr) assert disasm == "VCVTNE2PS2BF16 zmm2{k5}{z},zmm6,dword bcst [rax+4]"
- add_leading_zero_to_hex_numbers¶
Add a leading zero to hex numbers if there’s no prefix and the number starts with hex digits
A-FDefault
Value
Example
👍
True0FFhFalseFFh- Type:
- always_show_scale¶
Always show the scale value even if it’s
*1Default
Value
Example
Truemov eax,[rbx+rcx*1]👍
Falsemov eax,[rbx+rcx]- Type:
- always_show_segment_register¶
Always show the effective segment register.
If the option is
False, only show the segment register if there’s a segment override prefix.Default
Value
Example
Truemov eax,ds:[ecx]👍
Falsemov eax,[ecx]- Type:
- binary_digit_group_size¶
(
u8) Size of a digit group, see alsoFormatter.digit_separatorDefault
Value
Example
011010111👍
41101_0111- Type:
- binary_prefix¶
Binary number prefix or an empty string
Default:
""(masm/nasm/intel),"0b"(gas)- Type:
- binary_suffix¶
Binary number suffix or an empty string
Default:
"b"(masm/nasm/intel),""(gas)- Type:
- branch_leading_zeros¶
Add leading zeros to branch offsets. Used by
CALL NEAR,CALL FAR,JMP NEAR,JMP FAR,Jcc,LOOP,LOOPcc,XBEGINDefault
Value
Example
👍
Trueje 00000123hFalseje 123h- Type:
- cc_ae¶
Mnemonic condition code selector (eg.
JAE/JNB/JNC)Default:
JAE,CMOVAE,SETAE- Type:
- cc_e¶
Mnemonic condition code selector (eg.
JE/JZ)Default:
JE,CMOVE,SETE,LOOPE,REPE- Type:
- cc_ne¶
Mnemonic condition code selector (eg.
JNE/JNZ)Default:
JNE,CMOVNE,SETNE,LOOPNE,REPNE- Type:
- decimal_digit_group_size¶
(
u8) Size of a digit group, see alsoFormatter.digit_separatorDefault
Value
Example
012345678👍
312_345_678- Type:
- digit_separator¶
Digit separator or an empty string. See also eg.
Formatter.hex_digit_group_sizeDefault
Value
Example
👍
""0x12345678"_"0x1234_5678- Type:
- displacement_leading_zeros¶
Add leading zeros to displacements
Default
Value
Example
Truemov al,[eax+00000012h]👍
Falsemov al,[eax+12h]- Type:
- first_operand_char_index¶
(
u32) Character index (0-based) where the first operand is formatted. Can be set to 0 to format it immediately after the mnemonic. At least one space or tab is always added between the mnemonic and the first operand.Default
Value
Example
👍
0mov•rcx,rbp8mov•••••rcx,rbp- Type:
- format(instruction)¶
Formats the whole instruction: prefixes, mnemonic, operands
- Parameters:
instruction (Instruction) – Instruction to format
- Returns:
The formatted string
- Return type:
- format_all_operands(instruction)¶
Formats all operands
- Parameters:
instruction (Instruction) – Instruction to format
- Returns:
The formatted string
- Return type:
- format_i16(value)¶
Formats a
i16
- format_i32(value)¶
Formats a
i32
- format_i64(value)¶
Formats a
i64
- format_i8(value)¶
Formats a
i8
- format_mnemonic(instruction, options=0)¶
Formats the mnemonic and any prefixes
- Parameters:
instruction (Instruction) – Instruction to format
options (
FormatMnemonicOptions) – (default =FormatMnemonicOptions.NONE) Options
- Returns:
The formatted string
- Return type:
- format_operand(instruction, operand)¶
Formats an operand.
- Parameters:
instruction (Instruction) – Instruction
operand (int) – Operand number, 0-based. This is a formatter operand and isn’t necessarily the same as an instruction operand. See
Formatter.operand_count
- Returns:
The formatted string
- Return type:
- Raises:
ValueError – If operand is invalid
- format_operand_separator(instruction)¶
Formats an operand separator
- Parameters:
instruction (Instruction) – Instruction
- Returns:
The formatted string
- Return type:
- format_register(register)¶
Formats a register
- format_u16(value)¶
Formats a
u16
- format_u32(value)¶
Formats a
u32
- format_u64(value)¶
Formats a
u64
- format_u8(value)¶
Formats a
u8
- gas_naked_registers¶
(gas only): If
True, the formatter doesn’t add%to registersDefault
Value
Example
Truemov eax,ecx👍
Falsemov %eax,%ecx- Type:
- gas_show_mnemonic_size_suffix¶
(gas only): Shows the mnemonic size suffix even when not needed
Default
Value
Example
Truemovl %eax,%ecx👍
Falsemov %eax,%ecx- Type:
- gas_space_after_memory_operand_comma¶
(gas only): Add a space after the comma if it’s a memory operand
Default
Value
Example
True(%eax, %ecx, 2)👍
False(%eax,%ecx,2)- Type:
- get_formatter_operand(instruction, instruction_operand)¶
Converts an instruction operand index to a formatter operand index.
Returns
Noneif the instruction operand isn’t used by the formatter- Parameters:
instruction (Instruction) – Instruction
instruction_operand (int) – Instruction operand
- Returns:
Instruction operand or
Noneif the instruction operand isn’t used by the formatter- Return type:
int, None
- Raises:
ValueError – If instruction_operand is invalid
- get_instruction_operand(instruction, operand)¶
Converts a formatter operand index to an instruction operand index.
Returns
Noneif it’s an operand added by the formatter- Parameters:
instruction (Instruction) – Instruction
operand (int) – Operand number, 0-based. This is a formatter operand and isn’t necessarily the same as an instruction operand. See
Formatter.operand_count
- Returns:
Instruction operand or
Noneif it’s an operand added by the formatter- Return type:
int, None
- Raises:
ValueError – If operand is invalid
- hex_digit_group_size¶
(
u8) Size of a digit group, see alsoFormatter.digit_separatorDefault
Value
Example
00x12345678👍
40x1234_5678- Type:
- hex_prefix¶
Hex number prefix or an empty string, eg.
"0x"Default:
""(masm/nasm/intel),"0x"(gas)- Type:
- hex_suffix¶
Hex number suffix or an empty string, eg.
"h"Default:
"h"(masm/nasm/intel),""(gas)- Type:
- leading_zeros¶
Add leading zeros to hexadecimal/octal/binary numbers.
This option has no effect on branch targets and displacements, use
Formatter.branch_leading_zerosandFormatter.displacement_leading_zeros.Default
Value
Example
True0x0000000A/0000000Ah👍
False0xA/0Ah- Type:
- masm_add_ds_prefix32¶
(masm only): Add a
DSsegment override even if it’s not present. Used if it’s 16/32-bit code and mem op is a displDefault
Value
Example
👍
Truemov eax,ds:[12345678]Falsemov eax,[12345678]- Type:
- masm_displ_in_brackets¶
(masm only): Show displacements in brackets
Default
Value
Example
👍
True[ecx+1234h]False1234h[ecx]- Type:
- masm_symbol_displ_in_brackets¶
(masm only): Show symbols in brackets
Default
Value
Example
👍
True[ecx+symbol]/[symbol]Falsesymbol[ecx]/symbol- Type:
- memory_size_options¶
Options that control if the memory size (eg.
DWORD PTR) is shown or not.This is ignored by the gas (AT&T) formatter.
Default:
MemorySizeOptions.DEFAULT- Type:
- nasm_show_sign_extended_immediate_size¶
(nasm only): Shows
BYTE,WORD,DWORDorQWORDif it’s a sign extended immediate operand valueDefault
Value
Example
Trueor rcx,byte -1👍
Falseor rcx,-1- Type:
- number_base¶
Number base (
2,8,10,16)- Raises:
ValueError – If it’s an invalid number base
Default:
16- Type:
- octal_digit_group_size¶
(
u8) Size of a digit group, see alsoFormatter.digit_separatorDefault
Value
Example
012345670👍
41234_5670- Type:
- octal_prefix¶
Octal number prefix or an empty string
Default:
""(masm/nasm/intel),"0"(gas)- Type:
- octal_suffix¶
Octal number suffix or an empty string
Default:
"o"(masm/nasm/intel),""(gas)- Type:
- op_access(instruction, operand)¶
Returns the operand access but only if it’s an operand added by the formatter.
If it’s an operand that is part of
Instruction, you should call eg.InstructionInfoFactory.info.- Parameters:
instruction (Instruction) – Instruction
operand (int) – Operand number, 0-based. This is a formatter operand and isn’t necessarily the same as an instruction operand. See
Formatter.operand_count
- Returns:
Operand access or
None- Return type:
OpAccess, None- Raises:
ValueError – If operand is invalid
- operand_count(instruction)¶
Gets the number of operands that will be formatted. A formatter can add and remove operands
- Parameters:
instruction (Instruction) – Instruction
- Returns:
Operand count
- Return type:
- prefer_st0¶
Use
st(0)instead ofstifstcan be used. Ignored by the nasm formatter.Default
Value
Example
Truefadd st(0),st(3)👍
Falsefadd st,st(3)- Type:
- rip_relative_addresses¶
Show
RIP+displor the virtual addressDefault
Value
Example
Truemov eax,[rip+12345678h]👍
Falsemov eax,[1029384756AFBECDh]- Type:
- scale_before_index¶
Show memory operand scale value before the index register
Default
Value
Example
Truemov eax,[8*rdx]👍
Falsemov eax,[rdx*8]- Type:
- show_branch_size¶
Show
NEAR,SHORT, etc if it’s a branch instructionDefault
Value
Example
👍
Trueje short 1234hFalseje 1234h- Type:
- show_symbol_address¶
Show the original value after the symbol name
Default
Value
Example
Truemov eax,[myfield (12345678)]👍
Falsemov eax,[myfield]- Type:
- show_useless_prefixes¶
Show useless prefixes. If it has useless prefixes, it could be data and not code.
Default
Value
Example
Truees rep add eax,ecx👍
Falseadd eax,ecx- Type:
- show_zero_displacements¶
Show zero displacements
Default
Value
Example
Truemov eax,[rcx*2+0]👍
Falsemov eax,[rcx*2]- Type:
- signed_immediate_operands¶
Show immediate operands as signed numbers
Default
Value
Example
Truemov eax,-1👍
Falsemov eax,FFFFFFFF- Type:
- signed_memory_displacements¶
Displacements are signed numbers
Default
Value
Example
👍
Truemov al,[eax-2000h]Falsemov al,[eax+0FFFFE000h]- Type:
- small_hex_numbers_in_decimal¶
Small hex numbers (-9 .. 9) are shown in decimal
Default
Value
Example
👍
True9False0x9- Type:
- space_after_memory_bracket¶
Add a space between the memory expression and the brackets
Default
Value
Example
Truemov eax,[ rcx+rdx ]👍
Falsemov eax,[rcx+rdx]- Type:
- space_after_operand_separator¶
Add a space after the operand separator
Default
Value
Example
Truemov rax, rcx👍
Falsemov rax,rcx- Type:
- space_between_memory_add_operators¶
Add spaces between memory operand
+and-operatorsDefault
Value
Example
Truemov eax,[rcx + rdx*8 - 80h]👍
Falsemov eax,[rcx+rdx*8-80h]- Type:
- space_between_memory_mul_operators¶
Add spaces between memory operand
*operatorDefault
Value
Example
Truemov eax,[rcx+rdx * 8-80h]👍
Falsemov eax,[rcx+rdx*8-80h]- Type:
- uppercase_all¶
Everything is uppercased, except numbers and their prefixes/suffixes
Default
Value
Example
TrueMOV EAX,GS:[RCX*4+0ffh]👍
Falsemov eax,gs:[rcx*4+0ffh]- Type:
- uppercase_decorators¶
Uppercase decorators, eg.
{z},{sae},{rd-sae}(but not opmask registers:{k1})Default
Value
Example
Truevunpcklps xmm2{k5}{Z},xmm6,dword bcst [rax+4]👍
Falsevunpcklps xmm2{k5}{z},xmm6,dword bcst [rax+4]- Type:
- uppercase_keywords¶
Keywords are uppercased (eg.
BYTE PTR,SHORT)Default
Value
Example
Truemov BYTE PTR [rcx],12h👍
Falsemov byte ptr [rcx],12h- Type:
- uppercase_mnemonics¶
Mnemonics are uppercased
Default
Value
Example
TrueMOV rcx,rax👍
Falsemov rcx,rax- Type:
- uppercase_prefixes¶
Prefixes are uppercased
Default
Value
Example
TrueREP stosd👍
Falserep stosd- Type:
- uppercase_registers¶
Registers are uppercased
Default
Value
Example
Truemov RCX,[RAX+RDX*8]👍
Falsemov rcx,[rax+rdx*8]- Type: