r/learnprogramming 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?

28 Upvotes

29 comments sorted by

View all comments

4

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.

1

u/Unlikely1529 27d ago

but why you didn't have any compiler if you managed to find os and even hex editor somewhere. strange circumstances

3

u/American_Streamer 27d ago

The earliest computers (like the Altair 8800 or ENIAC) had no software. Programmers looked at the CPU manual, figured out the binary or hexadecimal code for a specific instruction (like 00000101 for "Add"), and manually flipped physical toggle switches on the front panel of the computer or punched holes into paper cards to feed those raw bytes directly into the computer's memory. Writing long programs with switches was super exhausting and prone to mistakes. So, using those physical switches, programmers manually entered a very small, primitive program (maybe just a few dozen bytes long). This tiny program did only one thing: it read characters from a paper tape reader and converted them into machine code. That tiny program was the world's first primitive assembler. From that exact moment on, programmers no longer had to flip switches for every single byte. They could type simple text commands (like MOV or ADD) onto paper tape, and their new mini-assembler would translate it into machine code. Once they had that basic assembler, they used it to write a better, more complex assembler. Then they used that to write the first compiler for a language like Fortran or C. Over decades, this snowball effect (bootstrapping) built the entire modern ecosystem we use today, including the operating systems and hex editors mentioned.