Class: Evoasm::X64::Instruction

Inherits:
FFI::Pointer show all
Defined in:
lib/evoasm/x64/instruction.rb

Overview

Represents an x86-64 instruction

Defined Under Namespace

Classes: Parameter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FFI::Pointer

#to_ptr

Instance Attribute Details

#nameSymbol (readonly)

Returns the instruction's name

Returns:

  • (Symbol)

    the instruction's name



16
17
18
# File 'lib/evoasm/x64/instruction.rb', line 16

def name
  @name
end

Instance Method Details

#basic?Bool

Returns whether this instruction is encodable with the basic encoder

Returns:

  • (Bool)


63
64
65
# File 'lib/evoasm/x64/instruction.rb', line 63

def basic?
  Libevoasm.x64_inst_is_basic(self)
end

#encode(parameters, buffer = nil, basic: false) ⇒ void

This method returns an undefined value.

Encodes the instruciton with the given parameters

Parameters:

  • parameters (X64::Parameters)

    parameters

  • buffer (Buffer) (defaults to: nil)

    the buffer to emit to

  • basic (Bool)

    whether the basic encoder should be used



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/evoasm/x64/instruction.rb', line 72

def encode(parameters, buffer = nil, basic: false)
  if basic && !basic?
    raise ArgumentError, 'instruction does not support basic mode'
  end

  buf_ref = Libevoasm.buf_ref_alloc

  if buffer
    Libevoasm.buf_to_buf_ref buffer, buf_ref
  else
    data = FFI::MemoryPointer.new :uint8, 32
    len_ptr = FFI::MemoryPointer.new :size_t, 1
    Libevoasm.buf_ref_init buf_ref, data, len_ptr
  end

  parameters = Parameters.for(parameters, basic: basic)

  success =
    if basic
      Libevoasm.x64_inst_enc_basic self, parameters, buf_ref
    else
      Libevoasm.x64_inst_enc self, parameters, buf_ref
    end

  Libevoasm.buf_ref_free buf_ref

  if success
    unless buffer
      len = len_ptr.read_size_t
      data.read_string len
    end
  else
    raise Error.last
  end
end

#mnemonicString

Gives the preferred instruction mnemonic

Returns:

  • (String)

    the mnemonics

See Also:



33
34
35
# File 'lib/evoasm/x64/instruction.rb', line 33

def mnemonic
  mnemonics.first
end

#mnemonicsArray<String>

Gives a list of instruction mnemonics

Returns:

  • (Array<String>)

    mnemonics



26
27
28
# File 'lib/evoasm/x64/instruction.rb', line 26

def mnemonics
  Libevoasm.x64_inst_get_mnem(self).split('/')
end

#operand(index) ⇒ Operand

Gives the operand at the specified index

Returns:



39
40
41
# File 'lib/evoasm/x64/instruction.rb', line 39

def operand(index)
  Operand.new Libevoasm.x64_inst_get_operand(self, index), self
end

#operandsArray<Operand>

Gives the instruction's operands

Returns:

  • (Array<Operand>)

    the operands



45
46
47
48
49
50
# File 'lib/evoasm/x64/instruction.rb', line 45

def operands
  n_operands = Libevoasm.x64_inst_get_n_operands self
  Array.new(n_operands) do |index|
    operand index
  end
end

#parametersArray<Instruction::Parameter>

Gives this instruction's parameters

Returns:



54
55
56
57
58
59
# File 'lib/evoasm/x64/instruction.rb', line 54

def parameters
  n_params = Libevoasm.x64_inst_get_n_params self
  Array.new(n_params) do |param_index|
    Parameter.new Libevoasm.x64_inst_get_param(self, param_index)
  end
end