Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/dvvcz/dasm
- Owner: DvvCz
- License: gpl-3.0
- Created: 2024-08-31T22:01:38.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2024-09-09T02:50:03.000Z (3 months ago)
- Last Synced: 2024-09-30T05:45:12.141Z (3 months ago)
- Topics: amd64, assembler, code-generator, rust, x86, x86-64
- Language: Rust
- Homepage:
- Size: 46.9 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
dasm
A tiny, zero dependency assembler that currently supports x86 and amd64.## 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 1let 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.