https://github.com/mariosieg/machinekit
[WIP] single C header x86-64 code generator < 300 lines
https://github.com/mariosieg/machinekit
Last synced: about 2 months ago
JSON representation
[WIP] single C header x86-64 code generator < 300 lines
- Host: GitHub
- URL: https://github.com/mariosieg/machinekit
- Owner: MarioSieg
- License: mit
- Created: 2023-02-07T17:21:40.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-07T17:24:46.000Z (over 3 years ago)
- Last Synced: 2025-03-22T19:23:01.015Z (about 1 year ago)
- Language: C
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MachineKit [WIP]
* Encode x86-64 machine language at runtime.
* Single header x86-64 code generator in under 300 lines of C99!
* Includes most common x86-64 instructions aswell as SSE and SSE2.
* AVX support is planned.
SSE instruction encoding example
1. Encode some instructions from C
```c
#include "X86_64.h"
int main() {
X64MScar buf[8192]; // create a buffer on the stack
memset(buf, XI_INT3, sizeof(buf)); // fill the buffer with the INT3 (breakpoint) instruction
X64MScar *p = buf; // create pointer as needle
// encode some SSE register-to-register instructions
X64_sse_rr(&p, XO_ADDPS, RID_XMM8, RID_XMM7);
X64_sse_rr(&p, XO_SUBSS, RID_XMM15, RID_XMM7);
X64_sse_rr(&p, XO_MOVSS, RID_XMM1, RID_XMM7);
X64_sse_rr(&p, XO_SUBSD, RID_XMM8, RID_XMM11);
X64_sse_rr(&p, XO_MOVAPS, RID_XMM8, RID_XMM7);
X64_sse_rr(&p, XO_MOVUPD, RID_XMM15, RID_XMM7);
X64_sse_rr(&p, XO_DIVPS, RID_XMM1, RID_XMM7);
X64_sse_rr(&p, XO_MINPD, RID_XMM8, RID_XMM11);
X64_machine_buf_dump_c(buf, p); // dump the machine code in our buffer into a C like string literal
return 0;
}
```
2. Machine code is generated
And can be then executed (requires mmap etc..) or printed as C string literal (X64_machine_buf_dump_c)
```c
const char *machine_code = { /* N = 35 */
"\x44\x0f\x58\xc7\xf3\x44\x0f\x5c"
"\xff\xf3\x0f\x10\xcf\xf2\x45\x0f"
"\x5c\xc3\x44\x0f\x28\xc7\x66\x44"
"\x0f\x10\xff\x0f\x5e\xcf\x66\x45"
"\x0f\x5d\xc3"
};
```
Disassembled machine code
Note: The operands are reversed because of AT&T syntax.
```asm
Address | Machine Language | Assembly
------------------------------------------------------------
0x0000000000000000: 44 0F 58 C7 addps %xmm7, %xmm8
0x0000000000000004: F3 44 0F 5C FF subss %xmm7, %xmm15
0x0000000000000009: F3 0F 10 CF movss %xmm7, %xmm1
0x000000000000000d: F2 45 0F 5C C3 subsd %xmm11, %xmm8
0x0000000000000012: 44 0F 28 C7 movaps %xmm7, %xmm8
0x0000000000000016: 66 44 0F 10 FF movupd %xmm7, %xmm15
0x000000000000001b: 0F 5E CF divps %xmm7, %xmm1
0x000000000000001e: 66 45 0F 5D C3 minpd %xmm11, %xmm8
```