A small x86 DSL for C++
Find a file
Tamvana Makuluni 9b1c186c76 switch to fixed-width integer types
The original version of this code used `size_t` for most integer types,
which was:
    1) incorrect (I neglected to use `uintptr_t`) when converting
       pointers to integers
    2) specific to the compilation target, not the assembly target.

This version uses `uint64_t` (which is the same as both `size_t` and
`uintptr_t` on x64) to match the assembly target.
2025-01-21 10:20:29 -08:00
.clang-format initial commit 2025-01-15 13:34:52 -08:00
.gitignore initial commit 2025-01-15 13:34:52 -08:00
asm.hxx switch to fixed-width integer types 2025-01-21 10:20:29 -08:00
LICENSE initial commit 2025-01-15 13:34:52 -08:00
README.md initial commit 2025-01-15 13:34:52 -08:00
test.cxx switch to fixed-width integer types 2025-01-21 10:20:29 -08:00

asm.hxx --- A single-header C++ partial x64 assembly DSL

This is a small DSL for creating bytecode literals in C++. Example:

#include "asm.hxx"
#include <stdio>

// Any type can be used as a placeholder in the generated code.
struct A {};

int main(int argc, char *argv) {
    using namespace asm_;
    constexpr auto code =
        Bytecode
	        << MOV(R8,GetVar<A,U64>)
	        << CALL(R8);
	  // placeholders may be set at runtime
	  // the code between them will be assembled at compile time.
	  //
	  // Output "\x49\xb8\x90\x49\x38\x20\x00\x00\x00\x00\x41\xff\xd0":
	  std::cout << (code << SetVar<A>(0x20384990)) << std::endl;
	  // Output "49 b8 27 70 a5 d3 ea 00 00 00 41 ff d0":
	  std::cout << (code << SetVar<A>(0xead3a57027)).human_readable() << std::endl;
}

As of now, the only commands covered are:

  • NOP
  • RET
  • CALL
  • MOV
  • PUSH
  • POP

I might add more in the future; the framework is there, but for now I just focused on what I needed to work. If you stumble onto this and want it extended, let me know what you need, and I'll add it.