Struct Instruction

Source
pub struct Instruction {
    pub mnemonic: Mnemonic,
    pub operand1: Option<Operand>,
    pub operand2: Option<Operand>,
    pub operand3: Option<Operand>,
    pub operand4: Option<Operand>,
    pub lock: bool,
    pub rounding_mode: Option<RoundingMode>,
    pub merge_mode: Option<MergeMode>,
    pub sae: bool,
    pub mask: Option<MaskReg>,
    pub broadcast: Option<BroadcastMode>,
}

Fields§

§mnemonic: Mnemonic§operand1: Option<Operand>§operand2: Option<Operand>§operand3: Option<Operand>§operand4: Option<Operand>§lock: bool§rounding_mode: Option<RoundingMode>§merge_mode: Option<MergeMode>§sae: bool§mask: Option<MaskReg>§broadcast: Option<BroadcastMode>

Implementations§

Source§

impl Instruction

Source

pub fn new0(mnemonic: Mnemonic) -> Instruction

Examples found in repository?
examples/encode_function.rs (line 15)
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}
Source

pub fn new1(mnemonic: Mnemonic, operand1: Operand) -> Instruction

Examples found in repository?
examples/encode_function.rs (line 11)
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}
Source

pub fn new2( mnemonic: Mnemonic, operand1: Operand, operand2: Operand, ) -> Instruction

Examples found in repository?
examples/encode_real.rs (line 15)
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 12)
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 16)
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 11)
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 new3( mnemonic: Mnemonic, operand1: Operand, operand2: Operand, operand3: Operand, ) -> Instruction

Source

pub fn new4( mnemonic: Mnemonic, operand1: Operand, operand2: Operand, operand3: Operand, operand4: Operand, ) -> Instruction

Source

pub fn operands(&self) -> [&Option<Operand>; 4]

Source

pub fn encode<W>( &self, writer: &mut W, mode: Mode, ) -> Result<usize, InstructionEncodingError>
where W: Write,

Trait Implementations§

Source§

impl Clone for Instruction

Source§

fn clone(&self) -> Instruction

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Instruction

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Instruction

Source§

fn default() -> Instruction

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Instruction

Source§

fn eq(&self, other: &Instruction) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Instruction

Source§

impl Eq for Instruction

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.