Class: Module

Inherits:
Object show all
Defined in:
lib/wilson.rb

Instance Method Summary collapse

Instance Method Details

#defasm(name, *args, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wilson.rb', line 35

def defasm name, *args, &block
  asm = Wilson::MachineCodeX86.new

  asm.ebp.push
  asm.ebp.mov asm.esp

  asm.esi.push
  asm.edi.push

  size = asm.stream.size

  asm.instance_eval(&block)

  if asm.stream.size == size  # return nil
    warn "returning nil for #{self}##{name}"
    asm.eax.mov 4
  end

  asm.edi.pop
  asm.esi.pop

  asm.leave
  asm.ret

  code = asm.stream.pack("C*")

  if $DEBUG then
    path = "#{name}.obj"
    File.open path, "wb" do |f|
      f.write code
    end

    p name
    puts code.unpack("C*").map { |n| "%02X" % n }.join(' ')
    system "ndisasm -u #{path}"

    File.unlink path
  end

  ptr  = code.to_ptr
  ASM << ptr
  Ruby.rb_define_method self, name.to_s, ptr, 0
end