keystone

Bindings to the Keystone Assembler.

Readme

Keystone.nim

Nim bindings for the Keystone assembler.

# Create engine safely
let engine = newX86Engine()

defer:
  engine.close()

# Emit to tuple
let enc = engine.assemble("add eax, eax ; ret")

assert enc.buf == @[ 0x01, 0xC0, 0xC3 ]
assert enc.size == 3
assert enc.statementsCount == 2

# Emit using dot operator
let enc = engine.add("eax", "eax")

# Emit to buffer now
var buf = newSeqOfCap[byte](0)

engine.assemble("add eax, eax", buf)
engine.ret(buf)

assert buf == @[ 0x01, 0xC0, 0xC3 ]

# No silent errors
try:
  engine.assemble("add 42")
expect KeystoneError:
  echo "Error encountered: ", getCurrentExceptionMsg()

Thanks to macros, emitting any assembly code is very easy.

let engine = newX86Engine()

# To a new buffer
let buf = assembly engine:
  add eax, eax
  ret

# To an existing buffer
assembly engine, buf:
  add eax, eax
  ret
Need help? Read Nimble
Author:
6A
No tagged versions available
License: MIT

Project website Hosted docs