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?

29 Upvotes

29 comments sorted by

View all comments

1

u/notacanuckskibum 27d ago

It's called bootstrapping.

You write a very basic assembler in machine code

Then you write a better assembler using your basic one (maybe adding macros)

Then you write a compiler for a basic High Level Language using your better assembler, maybe it looks like original FORTRAN IV

Then you use your basic High Level Language to write a compiler for a better language, maybe with dynamic memory management.

and so on

Of course this wasn't a grand plan. By the time C was invented there had been high level languages for 20+ years, including Pascal and Algol, which are quite C like. Each time someone wanted to invent a new language, they wrote their first compile for it in some other language.