Struct iced_x86::MasmFormatter

source ·
pub struct MasmFormatter { /* private fields */ }
Expand description

Masm formatter

Examples

use iced_x86::*;

let bytes = b"\x62\xF2\x4F\xDD\x72\x50\x01";
let mut decoder = Decoder::new(64, bytes, DecoderOptions::NONE);
let instr = decoder.decode();

let mut output = String::new();
let mut formatter = MasmFormatter::new();
formatter.options_mut().set_uppercase_mnemonics(true);
formatter.format(&instr, &mut output);
assert_eq!("VCVTNE2PS2BF16 zmm2{k5}{z},zmm6,dword bcst [rax+4]", output);

Using a symbol resolver

use iced_x86::*;
use std::collections::HashMap;

let bytes = b"\x48\x8B\x8A\xA5\x5A\xA5\x5A";
let mut decoder = Decoder::new(64, bytes, DecoderOptions::NONE);
let instr = decoder.decode();

struct MySymbolResolver { map: HashMap<u64, String> }
impl SymbolResolver for MySymbolResolver {
    fn symbol(&mut self, _instruction: &Instruction, _operand: u32, _instruction_operand: Option<u32>,
         address: u64, _address_size: u32) -> Option<SymbolResult> {
        if let Some(symbol_string) = self.map.get(&address) {
            // The 'address' arg is the address of the symbol and doesn't have to be identical
            // to the 'address' arg passed to symbol(). If it's different from the input
            // address, the formatter will add +N or -N, eg. '[rax+symbol+123]'
            Some(SymbolResult::with_str(address, symbol_string.as_str()))
        } else {
            None
        }
    }
}

// Hard code the symbols, it's just an example!😄
let mut sym_map: HashMap<u64, String> = HashMap::new();
sym_map.insert(0x5AA55AA5, String::from("my_data"));

let mut output = String::new();
let resolver = Box::new(MySymbolResolver { map: sym_map });
let mut formatter = MasmFormatter::with_options(Some(resolver), None);
formatter.format(&instr, &mut output);
assert_eq!("mov rcx,[rdx+my_data]", output);

Implementations§

Creates a masm formatter

Examples found in repository?
src/formatter/masm.rs (line 96)
95
96
97
	fn default() -> Self {
		MasmFormatter::new()
	}
More examples
Hide additional examples
src/instruction.rs (line 3774)
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		//
		// if the order of #[cfg()] checks gets updated, also update the `display_trait()` test method
		//
		#[cfg(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm"))]
		{
			#[cfg(feature = "masm")]
			let mut formatter = MasmFormatter::new();

			#[cfg(all(not(feature = "masm"), feature = "nasm"))]
			let mut formatter = NasmFormatter::new();

			#[cfg(all(not(feature = "masm"), not(feature = "nasm"), feature = "intel"))]
			let mut formatter = IntelFormatter::new();

			#[cfg(all(not(feature = "masm"), not(feature = "nasm"), not(feature = "intel"), feature = "gas"))]
			let mut formatter = GasFormatter::new();

			let mut output = FmtFormatterOutput::new(f);
			formatter.format(self, &mut output);
			output.result
		}
		#[cfg(not(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm")))]
		{
			let mut formatter = FastFormatter::new();

			let mut output = alloc::string::String::new();
			formatter.format(self, &mut output);
			f.write_str(&output)
		}
	}

Creates a masm formatter

Arguments
  • symbol_resolver: Symbol resolver or None
  • options_provider: Operand options provider or None
Examples found in repository?
src/formatter/masm.rs (line 115)
114
115
116
	pub fn new() -> Self {
		MasmFormatter::with_options(None, None)
	}

Trait Implementations§

Returns the “default value” for a type. Read more
Gets the formatter options (immutable)
Gets the formatter options (mutable)
Formats the mnemonic and/or any prefixes Read more
Gets the number of operands that will be formatted. A formatter can add and remove operands Read more
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(). Read more
Converts a formatter operand index to an instruction operand index. Returns None if it’s an operand added by the formatter Read more
Converts an instruction operand index to a formatter operand index. Returns None if the instruction operand isn’t used by the formatter Read more
Formats an operand. This is a formatter operand and not necessarily a real instruction operand. A formatter can add and remove operands. Read more
Formats an operand separator Read more
Formats all operands Read more
Formats the whole instruction: prefixes, mnemonic, operands Read more
Formats a register Read more
Formats a i8 Read more
Formats a i16 Read more
Formats a i32 Read more
Formats a i64 Read more
Formats a u8 Read more
Formats a u16 Read more
Formats a u32 Read more
Formats a u64 Read more
Formats a i8 Read more
Formats a i16 Read more
Formats a i32 Read more
Formats a i64 Read more
Formats a u8 Read more
Formats a u16 Read more
Formats a u32 Read more
Formats a u64 Read more
Formats the mnemonic and any prefixes Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.