Struct InstructionWriter

Source
pub struct InstructionWriter<T: Write> { /* private fields */ }

Implementations§

Source§

impl<T: Write> InstructionWriter<T>

Source

pub fn new(writer: T, mode: Mode) -> InstructionWriter<T>

Examples found in repository?
examples/encode_basic.rs (line 8)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Protected); 
9    
10    let bytes_written = 
11        writer.write2(Mnemonic::MOV, Operand::Direct(Reg::EAX), Operand::Literal32(10)).unwrap() + // mov eax, 10
12        writer.write2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Literal32(20)).unwrap() + // mov ebx, 20
13        writer.write2(Mnemonic::ADD, Operand::Direct(Reg::EAX), Operand::Direct(Reg::EBX)).unwrap(); // add eax, ebx
14
15    print!("Output ({} bytes): ", bytes_written);
16    for byte in writer.get_inner_writer_ref().get_ref().iter() {
17        print!("{:02X} ", byte);
18    }
19    println!("");
20}
More examples
Hide additional examples
examples/encode_real.rs (line 8)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Real); 
9
10    // mov ax, [bx+si]
11    // add ax, bx
12    // mov [bp+si], ax
13    
14    let instructions = &[
15        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::AX), Operand::IndirectScaledIndexed(Reg::BX, Reg::SI, RegScale::One, Some(OperandSize::Word), None)), // mov ax, [bx+si]
16        Instruction::new2(Mnemonic::ADD, Operand::Direct(Reg::AX), Operand::Direct(Reg::BX)), // add ax, bx
17        Instruction::new2(Mnemonic::MOV, Operand::IndirectScaledIndexed(Reg::BX, Reg::SI, RegScale::One, Some(OperandSize::Word), None), Operand::Direct(Reg::AX)), // mov [bp+si]
18    ];
19
20    let mut bytes_written = 0;
21
22    for instr in instructions {
23        bytes_written += writer.write(instr).unwrap();
24    }
25    
26    print!("Output ({} bytes): ", bytes_written);
27    for byte in writer.get_inner_writer_ref().get_ref().iter() {
28        print!("{:02X} ", byte);
29    }
30    println!("");
31}
examples/encode_function.rs (line 8)
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}
examples/encode_long.rs (line 8)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Long); 
9
10    // mov rax, qword ptr [rip+100]
11    // mov rbx, 500
12    // sub rax, rbx
13    // mov [rcx+rdx*4], rax
14    
15    let instructions = &[
16        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::RAX), Operand::IndirectDisplaced(Reg::RIP, 100, Some(OperandSize::Qword), None)),
17        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::RBX), Operand::Literal32(500)),
18        Instruction::new2(Mnemonic::SUB, Operand::Direct(Reg::RAX), Operand::Direct(Reg::RBX)),
19        Instruction::new2(Mnemonic::MOV, Operand::IndirectScaledIndexed(Reg::RCX, Reg::RDX, RegScale::Four, Some(OperandSize::Qword), None), Operand::Direct(Reg::RAX)),
20    ];
21
22    let mut bytes_written = 0;
23
24    for instr in instructions {
25        bytes_written += writer.write(instr).unwrap();
26    }
27    
28    print!("Output ({} bytes): ", bytes_written);
29    for byte in writer.get_inner_writer_ref().get_ref().iter() {
30        print!("{:02X} ", byte);
31    }
32    println!("");
33}
examples/addressing_modes.rs (line 8)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Protected); 
9
10    let instructions = &[
11        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Indirect(Reg::EAX, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax]
12        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectDisplaced(Reg::EAX, 5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+5]
13        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectScaledIndexed(Reg::EAX, Reg::ECX, RegScale::Two, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+ecx*2]
14        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectScaledIndexedDisplaced(Reg::EAX, Reg::ECX, RegScale::Two, 5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+ecx*2+5]
15        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Memory(5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr ds:5
16    ];
17
18    let mut bytes_written = 0;
19
20    for instr in instructions {
21        bytes_written += writer.write(instr).unwrap();
22    }
23    
24    print!("Output ({} bytes): ", bytes_written);
25    for byte in writer.get_inner_writer_ref().get_ref().iter() {
26        print!("{:02X} ", byte);
27    }
28    println!("");
29}
Source

pub fn get_inner_writer_ref(&self) -> &T

Examples found in repository?
examples/encode_basic.rs (line 16)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Protected); 
9    
10    let bytes_written = 
11        writer.write2(Mnemonic::MOV, Operand::Direct(Reg::EAX), Operand::Literal32(10)).unwrap() + // mov eax, 10
12        writer.write2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Literal32(20)).unwrap() + // mov ebx, 20
13        writer.write2(Mnemonic::ADD, Operand::Direct(Reg::EAX), Operand::Direct(Reg::EBX)).unwrap(); // add eax, ebx
14
15    print!("Output ({} bytes): ", bytes_written);
16    for byte in writer.get_inner_writer_ref().get_ref().iter() {
17        print!("{:02X} ", byte);
18    }
19    println!("");
20}
More examples
Hide additional examples
examples/encode_real.rs (line 27)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Real); 
9
10    // mov ax, [bx+si]
11    // add ax, bx
12    // mov [bp+si], ax
13    
14    let instructions = &[
15        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::AX), Operand::IndirectScaledIndexed(Reg::BX, Reg::SI, RegScale::One, Some(OperandSize::Word), None)), // mov ax, [bx+si]
16        Instruction::new2(Mnemonic::ADD, Operand::Direct(Reg::AX), Operand::Direct(Reg::BX)), // add ax, bx
17        Instruction::new2(Mnemonic::MOV, Operand::IndirectScaledIndexed(Reg::BX, Reg::SI, RegScale::One, Some(OperandSize::Word), None), Operand::Direct(Reg::AX)), // mov [bp+si]
18    ];
19
20    let mut bytes_written = 0;
21
22    for instr in instructions {
23        bytes_written += writer.write(instr).unwrap();
24    }
25    
26    print!("Output ({} bytes): ", bytes_written);
27    for byte in writer.get_inner_writer_ref().get_ref().iter() {
28        print!("{:02X} ", byte);
29    }
30    println!("");
31}
examples/encode_function.rs (line 26)
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}
examples/encode_long.rs (line 29)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Long); 
9
10    // mov rax, qword ptr [rip+100]
11    // mov rbx, 500
12    // sub rax, rbx
13    // mov [rcx+rdx*4], rax
14    
15    let instructions = &[
16        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::RAX), Operand::IndirectDisplaced(Reg::RIP, 100, Some(OperandSize::Qword), None)),
17        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::RBX), Operand::Literal32(500)),
18        Instruction::new2(Mnemonic::SUB, Operand::Direct(Reg::RAX), Operand::Direct(Reg::RBX)),
19        Instruction::new2(Mnemonic::MOV, Operand::IndirectScaledIndexed(Reg::RCX, Reg::RDX, RegScale::Four, Some(OperandSize::Qword), None), Operand::Direct(Reg::RAX)),
20    ];
21
22    let mut bytes_written = 0;
23
24    for instr in instructions {
25        bytes_written += writer.write(instr).unwrap();
26    }
27    
28    print!("Output ({} bytes): ", bytes_written);
29    for byte in writer.get_inner_writer_ref().get_ref().iter() {
30        print!("{:02X} ", byte);
31    }
32    println!("");
33}
examples/addressing_modes.rs (line 25)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Protected); 
9
10    let instructions = &[
11        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Indirect(Reg::EAX, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax]
12        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectDisplaced(Reg::EAX, 5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+5]
13        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectScaledIndexed(Reg::EAX, Reg::ECX, RegScale::Two, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+ecx*2]
14        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectScaledIndexedDisplaced(Reg::EAX, Reg::ECX, RegScale::Two, 5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+ecx*2+5]
15        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Memory(5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr ds:5
16    ];
17
18    let mut bytes_written = 0;
19
20    for instr in instructions {
21        bytes_written += writer.write(instr).unwrap();
22    }
23    
24    print!("Output ({} bytes): ", bytes_written);
25    for byte in writer.get_inner_writer_ref().get_ref().iter() {
26        print!("{:02X} ", byte);
27    }
28    println!("");
29}
Source

pub fn write( &mut self, instr: &Instruction, ) -> Result<usize, InstructionEncodingError>

Examples found in repository?
examples/encode_real.rs (line 23)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Real); 
9
10    // mov ax, [bx+si]
11    // add ax, bx
12    // mov [bp+si], ax
13    
14    let instructions = &[
15        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::AX), Operand::IndirectScaledIndexed(Reg::BX, Reg::SI, RegScale::One, Some(OperandSize::Word), None)), // mov ax, [bx+si]
16        Instruction::new2(Mnemonic::ADD, Operand::Direct(Reg::AX), Operand::Direct(Reg::BX)), // add ax, bx
17        Instruction::new2(Mnemonic::MOV, Operand::IndirectScaledIndexed(Reg::BX, Reg::SI, RegScale::One, Some(OperandSize::Word), None), Operand::Direct(Reg::AX)), // mov [bp+si]
18    ];
19
20    let mut bytes_written = 0;
21
22    for instr in instructions {
23        bytes_written += writer.write(instr).unwrap();
24    }
25    
26    print!("Output ({} bytes): ", bytes_written);
27    for byte in writer.get_inner_writer_ref().get_ref().iter() {
28        print!("{:02X} ", byte);
29    }
30    println!("");
31}
More examples
Hide additional examples
examples/encode_function.rs (line 22)
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}
examples/encode_long.rs (line 25)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Long); 
9
10    // mov rax, qword ptr [rip+100]
11    // mov rbx, 500
12    // sub rax, rbx
13    // mov [rcx+rdx*4], rax
14    
15    let instructions = &[
16        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::RAX), Operand::IndirectDisplaced(Reg::RIP, 100, Some(OperandSize::Qword), None)),
17        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::RBX), Operand::Literal32(500)),
18        Instruction::new2(Mnemonic::SUB, Operand::Direct(Reg::RAX), Operand::Direct(Reg::RBX)),
19        Instruction::new2(Mnemonic::MOV, Operand::IndirectScaledIndexed(Reg::RCX, Reg::RDX, RegScale::Four, Some(OperandSize::Qword), None), Operand::Direct(Reg::RAX)),
20    ];
21
22    let mut bytes_written = 0;
23
24    for instr in instructions {
25        bytes_written += writer.write(instr).unwrap();
26    }
27    
28    print!("Output ({} bytes): ", bytes_written);
29    for byte in writer.get_inner_writer_ref().get_ref().iter() {
30        print!("{:02X} ", byte);
31    }
32    println!("");
33}
examples/addressing_modes.rs (line 21)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Protected); 
9
10    let instructions = &[
11        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Indirect(Reg::EAX, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax]
12        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectDisplaced(Reg::EAX, 5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+5]
13        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectScaledIndexed(Reg::EAX, Reg::ECX, RegScale::Two, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+ecx*2]
14        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::IndirectScaledIndexedDisplaced(Reg::EAX, Reg::ECX, RegScale::Two, 5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr [eax+ecx*2+5]
15        Instruction::new2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Memory(5, Some(OperandSize::Dword), None)), // mov ebx, dword ptr ds:5
16    ];
17
18    let mut bytes_written = 0;
19
20    for instr in instructions {
21        bytes_written += writer.write(instr).unwrap();
22    }
23    
24    print!("Output ({} bytes): ", bytes_written);
25    for byte in writer.get_inner_writer_ref().get_ref().iter() {
26        print!("{:02X} ", byte);
27    }
28    println!("");
29}
Source

pub fn write0( &mut self, mnemonic: Mnemonic, ) -> Result<usize, InstructionEncodingError>

Source

pub fn write1( &mut self, mnemonic: Mnemonic, operand1: Operand, ) -> Result<usize, InstructionEncodingError>

Source

pub fn write2( &mut self, mnemonic: Mnemonic, operand1: Operand, operand2: Operand, ) -> Result<usize, InstructionEncodingError>

Examples found in repository?
examples/encode_basic.rs (line 11)
6fn main() {
7    let buffer = Cursor::new(Vec::new());
8    let mut writer = InstructionWriter::new(buffer, Mode::Protected); 
9    
10    let bytes_written = 
11        writer.write2(Mnemonic::MOV, Operand::Direct(Reg::EAX), Operand::Literal32(10)).unwrap() + // mov eax, 10
12        writer.write2(Mnemonic::MOV, Operand::Direct(Reg::EBX), Operand::Literal32(20)).unwrap() + // mov ebx, 20
13        writer.write2(Mnemonic::ADD, Operand::Direct(Reg::EAX), Operand::Direct(Reg::EBX)).unwrap(); // add eax, ebx
14
15    print!("Output ({} bytes): ", bytes_written);
16    for byte in writer.get_inner_writer_ref().get_ref().iter() {
17        print!("{:02X} ", byte);
18    }
19    println!("");
20}
Source

pub fn write3( &mut self, mnemonic: Mnemonic, operand1: Operand, operand2: Operand, operand3: Operand, ) -> Result<usize, InstructionEncodingError>

Source

pub fn write4( &mut self, mnemonic: Mnemonic, operand1: Operand, operand2: Operand, operand3: Operand, operand4: Operand, ) -> Result<usize, InstructionEncodingError>

Auto Trait Implementations§

§

impl<T> Freeze for InstructionWriter<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for InstructionWriter<T>
where T: RefUnwindSafe,

§

impl<T> Send for InstructionWriter<T>
where T: Send,

§

impl<T> Sync for InstructionWriter<T>
where T: Sync,

§

impl<T> Unpin for InstructionWriter<T>
where T: Unpin,

§

impl<T> UnwindSafe for InstructionWriter<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.