How to use Rust's iced-x86 to generate opcodes from instructions string

Viewed 224

Im new to Rust and I was messing around with the iced library to disassemble stuff and it is extremely useful.

What I can't seem to understand is how to use the Encoder to generate opcodes from a string and not an Instruction struct.

As far as I can tell the iced-x86 documentations doesnt cover this.

According to the documentation: The encoder can be used to re-encode decoded instructions at any address - doesnt say anything about encoding instructions from string

This is what I basically trying to achieve:

use iced_x86::*;

let instr = "xchg ah,[rdx+rsi+16h]";

let mut encoder = Encoder::new(64);
match encoder.encode(&instr, 0x5555_5555) {
    Ok(len) => assert_eq!(4, len),
    Err(err) => panic!("{}", err),
}
// We're done, take ownership of the buffer
let buffer = encoder.take_buffer();
assert_eq!(vec![0x86, 0x64, 0x32, 0x16], buffer);
0 Answers
Related