encode_function/
encode_function.rs

1extern crate x86asm;
2
3use std::io::Cursor;
4use x86asm::{Instruction, InstructionWriter, Mnemonic, Mode, Operand, OperandSize, Reg};
5
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Protected); 
9
10    let instructions = &[
11        Instruction::new1(Mnemonic::PUSH, Operand::Direct(Reg::EBP)),
12        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBP), Operand::Direct(Reg::ESP)),
13        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EAX), Operand::IndirectDisplaced(Reg::EBP, 12, Some(OperandSize::Dword), None)),
14        Instruction::new2(Mnemonic::ADD, Operand::Direct(Reg::EAX), Operand::IndirectDisplaced(Reg::EBP, 8, Some(OperandSize::Dword), None)),
15        Instruction::new0(Mnemonic::LEAVE),
16        Instruction::new0(Mnemonic::RET),
17    ];
18
19    let mut bytes_written = 0;
20
21    for instr in instructions {
22        bytes_written += writer.write(instr).unwrap();
23    }
24    
25    print!("Output ({} bytes): ", bytes_written);
26    for byte in writer.get_inner_writer_ref().get_ref().iter() {
27        print!("{:02X} ", byte);
28    }
29    println!("");
30}