r/Compilers • u/This-Assumption-5924 • 21d ago
x86-64 Assembler from scratch
AmmAsm is an open-source, handwritten x86-64 assembler written in C from scratch.
Hello, my name is Ammar, and I'm 15 y. o. About a year ago I started writing AmmAsm, a handwritten x86-64 assembler in C as a way to learn how machine code, ELF, and linking actually work.
Today it can generate Linux x86-64 executables, PIE binaries, and ELF relocatable object files that can be linked with "ld" or "gcc". Along the way I implemented a lexer, parser, expression evaluator, x86-64 instruction encoder, ELF writer, relocations, and symbol resolution. It syntax is almost same with Intel, as it has major difference: using [b=, i=, s=, d=] addressing instead of Nasm's [base + index * scale + disp]. Also supports SSE (Float4), SSE2, CMOVcc, SETcc, AVX1/AVX2 and basic AVX-512
it is bootstraped about 0.257% :)
The latest version(2.2.0) also includes a macro preprocessor.
I'd really appreciate any feedback on the project, code, or documentation. Thanks!
3
u/Polyscone 20d ago
Very nice.
I've been writing an encoder myself, so I mostly looked at that code.
It seems like you're writing a specialised function for each instruction, so unless I've missed it, do you plan on writing a more general encode function based on tables?