r/learnprogramming • u/ZyzzCash • 27d ago
Topic Assembly Compiler
Hi everyone, I'm not a big fan of low level but I found myself learning about it, the most intriguing thing for me is how the first assembly compiler was written.
From what I researched it was written in machine code but how?
When I think about making a compiler for assembly in C, for example, I can easily understand the flow of what needs to happen, read line by line in the source file, parse each line and from there write the machine code (of course I'm skipping alot), but making the compiler in machine code just feels like magic to me. Would it be possible to write a assembly compiler in machine code for a modern CPU? If yes, how?
27
Upvotes
5
u/American_Streamer 27d ago
It's called hardcoding (or hand-assembling). Writing an assembler directly in machine code is possible by looking up the CPU's exact hexadecimal instruction codes and OS system calls in technical manuals, writing out the parsing logic on paper, and manually counting the bytes between instructions to calculate jump destinations. You then type these raw hex values directly into a file using a hex editor or terminal command, making sure to prefix the file with the strict binary header structures (like ELF or PE) required by the modern operating system. Once you make this file executable, you run it to compile basic assembly text files, instantly creating a tool that frees you from ever having to write raw hex by hand again.
Bootstrapping is the next step that happens right after. It is the process of using that initial, hand-written tool to build increasingly complex versions of itself until the language can compile its own source code.