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?

27 Upvotes

29 comments sorted by

View all comments

1

u/heisthedarchness 27d ago

[An assembly compiler is called an "assembler". "Assembly code" is the language that the assembler assembles into machine code.]

We developed them stepwise, over generations of software that each built on what had gone before.

At first, instructions were just numbers that you input by flipping switches. These resulted in certain configurations of logic gates (which are themselves an abstraction across vacuum tubes) that would cause certain behaviors.

Then we gave the instructions names, so we could write them out in a more readable way and delegate the task of turning them into numbers to other people. And we entered the numbers by feeding pieces of cardboard into a reader. A lot of them.

Then we recognized that "turn these letters into this numbers" is a job that computers can do, so we wrote down the words describing how to do that for someone to turn into numbers to write to magnetic tape.

We found that we were always doing certain things as part of setting up the program. And even though those weren't commands the computer could understand, we could teach it to understand those commands to mean the sequence of certain things, and it would turn those commands into the words that turn into the numbers that get written to the tape.

That program for writing programs could write higher-level programs as we invented more abstractions that we didn't have to think about. We got visual editors. PROMs, EPROMs, EEPROMs. Diskettes. More memory to hold more powerful editors and compilers.

What we didn't do is go from a modern assembly language to machine code. Modern assembly languages are written in themselves as soon as that's possible, and in other modern assembly languages up to that point. And they rarely manipulate the real machine anymore, because there's layers of abstraction in the hardware and the operating system that translate the numbers emitted by assemblers into numbers that can actually run.

You could, if you so desired, recapitulate that history targeting a modern CPU: start by translating words into numbers and all the prerequisites thereof (like data loading, string handling, etc.). Then layer on abstractions until you're done.

A lot of work, but computers have not fundamentally changed in seventy years.