Skip to content

Latest commit

 

History

History
45 lines (37 loc) · 1.27 KB

README.md

File metadata and controls

45 lines (37 loc) · 1.27 KB

machina crates.io docs.rs

Manage and execute assembly at runtime.

Crossplatform?

Yap.

Why?

Emulators, JIT, dynarec, etc.

Install

To build the tests you need sam.
In Cargo.toml:

[dependencies]
machina = "*"

If you work with many instructions, you may want to enable optimization (only applied to Cache):

[dependencies.machina]
version = "*"
features = ["optimize"]

Example

extern crate machina;
#[macro_use]
extern crate sam; // just to keep it simple

use machina::cache::{Cache, STUB};
use machina::memory::Memory;

fn main() {
    let asm = format!("mov rax, {}", STUB); // fill in a dummy address
    let dummy = 64;
    let address = &dummy as *const _; // address of dummy

    let cache = Cache::new();
    let memory = Memory::new(1); // 1 page = 1024 bytes
    cache.insert("mov_rax_x".to_string(), sam!(x64 => &asm)); // create the bytes at compile time via sam

    memory.emit_bytes(cache.get_stub("mov_rax_x".to_string(), address as u64)) // get "mov_rax_x" and fill in a dynamic address
    let rax = memory.execute(); // returns rax
}