Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dvvcz/dasm

Tiny dynamic x86/amd64 assembly library
https://github.com/dvvcz/dasm

amd64 assembler code-generator rust x86 x86-64

Last synced: 2 months ago
JSON representation

Tiny dynamic x86/amd64 assembly library

Awesome Lists containing this project

README

        

dasm


A tiny, zero dependency assembler that currently supports x86 and amd64.



Crates.io Version


Docs.rs Link


Test Status

## Note

Support for every single instruction is **NOT** planned.
Nor will there be any passes for optimization.

This has a simple goal of assembling instructions for compilers.
If you want a fully featured library, please refer to [`Iced`](https://github.com/icedland/iced)

## How it works

Code generation doesn't have to be hard. This just provides explicit functions for generating instructions.
No abstractions for the sake of safety or optimization which add complexity. If you just want to write assembly, this is for you.

**This library has tiered abstraction levels.**

### Raw

This gives you access to the "raw" bytes from an instruction given untagged numeric arguments.
Despite how it sounds, it is pretty nice to use on its own.

**Example**

```rust
let rax = 0;
let rsi = 6; // Argument 2
let rdi = 7; // Argument 1

let asm = [
&dasm::tier::raw::amd64::mov_r64_r64(rax, rdi) as &[u8],
&dasm::tier::raw::amd64::add_r64_r64(rax, rsi),
&dasm::tier::raw::amd64::ret()
].concat();

// A helper for making memory executable is included.
let mmapped = dasm::mmap::Mmap::exec(&asm)
.expect("Failed to mmap");

// Simply cast the bytes to the function you just made.
let adder: extern "C" fn(x: u64, y: u64) -> u64 = unsafe { std::mem::transmute(mmapped.as_ptr()) };
assert_eq!(adder(5, 200), 205);
```

There's also an example showcasing a tiny AOT compiled lisp at [`examples/tinylisp`](https://github.com/DvvCz/dasm/tree/master/examples/tinylisp).

## Other Tiers

At the moment, other tiers are not implemented as I plan out how these abstractions would go.

Hopefully they'd involve abstracting away overloads with tagged enums and cross architecture compatibility.

## Why

I felt the need for an assembler.

LLVM was out of the question due to its size and difficult integration to Rust.

I struggled to work with Cranelift due to its complexity for the sake of safety and performance.

But sometimes, you don't care about safety or performance. You just want a simple solution to get it done.

That's what `dasm` is supposed to be.