basic/
basic.rs

1use rydis::{
2    FormatStyle, Instruction, MachineMode, MemOperand, Mnemonic, Operand, Prefixes, Register,
3    StackWidth,
4};
5
6fn main() -> rydis::Result<()> {
7    let state = rydis::RydisState::new(MachineMode::Long64, StackWidth::Width64)?;
8
9    // encode an instruction
10    let encoded = state.encode(Instruction {
11        prefixes: Prefixes::empty(),
12        mnemonic: Mnemonic::XCHG,
13        operands: [Operand::Reg(Register::RAX), Operand::Reg(Register::RBX)]
14            .into_iter()
15            .collect(),
16    })?;
17    println!("encoded = {:x?}", encoded);
18
19    // decode it
20    let decoded_instruction = state.decode_one(encoded.as_slice())?;
21
22    // modify it
23    let mut modified_instruction = decoded_instruction.to_instruction();
24    modified_instruction.operands[1] = Operand::Mem(MemOperand {
25        base: Some(Register::RBP),
26        index: None,
27        displacement: 0x1234,
28        size: decoded_instruction.operand_width,
29        segment_register_override: None,
30    });
31
32    // format it
33    println!(
34        "modified insn: {}",
35        modified_instruction.format(&state, FormatStyle::Intel, Some(0x123400))?
36    );
37
38    // re-encode the modified instruction
39    let re_encoded = state.encode(modified_instruction)?;
40    println!("re-encoded = {:x?}", re_encoded);
41
42    Ok(())
43}