r/Compilers • • Oct 28 '26

I built a self-assembling x86-64 assembler from scratch

Post image

I've been working on AmmAsm, a handwritten x86-64 assembler, and recently achieved a milestone I wanted to document.

AmmAsm 1.0 was originally written in in C.

With AmmAsm 3.0.2, I managed to rewritte AmmAsm 1.0 code base to assembly x86-64, assemble it with verion 3.0.2, link, and run it again.

The repository now includes the original assembly source, test files, and execution evidence.

What makes this interesting to me is the connection between the versions: the newer assembler can process the assembly-language implementation of its own earliest version.

This isn't a claim that AmmAsm can compile itself from its current C source. Rather, it's a way to preserve and bring back an earlier stage of the project's development.

Repository: https://github.com/LinuxCoder13/AmmAsm

I'm curious how others here approach preserving old implementations and testing assemblers against real-world assembly code.

118 Upvotes

16 comments sorted by

View all comments

2

u/[deleted] Oct 28 '26

AmmAsm 1.0 was originally written in in C.

The repository now includes the original assembly source,

So which was written first?! The aasm.asm file says it is a self-hosted of 'aasm.c', although I can't find any such file. So not a port of the C version shown in your link?

In any case, it's a little confusing, because the usual pattern is to write things in assembly then port to a HLL (if there is a reason to use assembly at all).

Was the aim to test a subtantial ASM program? One approach is to use a C compiler to generate smallish programs and work from there. Although I appreciate that the output from programs such as gcc can be daunting. You can however extract functions from it into their own file, tweak as needed, assemble that, and link against a test framework.

I'm curious how others here approach preserving old implementations and testing assemblers against real-world assembly code.

'Real-world assembly' meaning hand-written ASM, or can it be the output of a compiler? That's where most ASM code comes from.

I maintain a private HLL, which can generate ASM as an option. This uses my specific dialect of x64, and it is processed by my assembler, another project.

That can be tested on bigger programs which the compiler generates. It can sort of assemble itself, but it's rather contrived (here, 'mm' is my HLL compiler, and 'aa' is the assembler; aa.m is the lead source file of the assembler):

c:\ax>mm aa                        # build the assembler executable
Compiling aa.m to aa.exe

c:\ax>mm -a aa                     # create also an ASM version
Compiling aa.m to aa.asm

c:\ax>aa aa -o:aa2                 # assemble the ASM assembler version
Assembling aa.asm to aa2.exe       # (Don't overwrite running aa.exe)

c:\ax>aa2 aa                       # try out AA-assembled version
Assembling aa.asm to aa.exe

(The .asm file here is 45Kloc and represents the entire program. This assembler only needs to support the subset used by my HLL, or that can be written via inline assembly, so it can cut corners.)

2

u/This-Assumption-5924 Oct 28 '26

Soo, Basically AmmAsm 1.0 was writen in C, then when I reached by developing AmmAsm verion 3.0.2, I rewrote original AmmAsm 1.0 code base to assembly language, with syntax fit able for my assembler. Yea, I know it sound confusing but pipe line is AmmAsm 1.0 in C -> development -> AmmAsm 3.0.2 -> compile AmmAsm 1.0 code base which is re written in assembly, with AmmAsm 3.0.2 -> AmmAsm 1.0 in assembly.

So it is not 100% self hosting, but it is bootstrape, or self-hosting for version 1.0

By the way, I used GCC in order to include libc to AmmAsm 1.0

0

u/[deleted] Oct 29 '26

[deleted] — view deleted comment

2

u/This-Assumption-5924 Oct 29 '26

However, AmmAsm 1.0 is bootstraped, 3.0.2 isn't