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

12

u/choikwa Oct 28 '26

how about self disassembling disassembler

2

u/This-Assumption-5924 Oct 29 '26

Nope, I made assembler, not disassembler

5

u/AustinVelonaut Oct 28 '26

You might want to specify early on in your README whether AmmAsm is Intel-compatible or ATT-compatible syntax (or can handle both?) From the few examples it looks like Intel, but it should be documented.

4

u/This-Assumption-5924 Oct 28 '26

AmmAsm follows almost intel sytax, how ever it has key deferences which is mentioned in README, and you can see test programs in tests/, such as brainfuck interpreter, mandelbrot in AVX, etc.

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

2

u/kesarev Oct 31 '26

Very nice. How far we are from support of RISC-V assembly?

1

u/This-Assumption-5924 Oct 31 '26

What do you mean by "how far we are from supporting RISC V?"

My assembler is target x86-64 architecture only. I wrote encoders to encode every single instruction and its every operand types from scratch, no Nasm/Gas dependence, only pure C99.

1

u/Double-Setting5615 25d ago

Any plans to make it not only amd64?

2

u/hukupaku Oct 29 '26

How much Ai sir?

-1

u/This-Assumption-5924 Oct 29 '26

To be honest, yes, AmmAsm 1.0 source code was rewritte to assembly by claude code, however, aasm it self I am writing almost by my self, with some AI assistance in order to make big regression files to test my encoders.

I have many test programs, folder tests/ in repository consist of Brainfuck interpreter, Mandelbrot fractal, and else I/O sofware.

Writeing aasm it self and test programs for it, will definitely take a lot time. "Tests are many, AmmAsm is alone".

2

u/bearheart Nov 01 '26

These days, "I wrote a thing" almost always means "I told the global pyschotic plagiarism machine to write a thing for me".

0

u/This-Assumption-5924 29d ago

Maybe that make sense...

-1

u/Sorry_Difficulty_250 Oct 28 '26

Nice! Well done!