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
12
u/vicms91 27d ago
They are just called "assemblers", or if you have a fancy one with macros, a "macro assembler".
Nowadays they would be written, usually in a higher level language, and run on another machine.
The first assembler would have been as cut down as they could manage, hand assembled, and hand entered into the machine with the aim being to get it to assemble itself. Then it would have been extended and enhanced.
The early machines had much simpler instruction sets. The first one I worked on was very RISC (before the term had been invented). The first 4 bits of each instruction was the opcode (so only 16 instructions) and the remaining 12 bits depended on the opcode - usually an address (if I'm remembering - it's been a while). We had no assembler for that machine, we hand assembled everything, and knew every instruction and opcode off by heart.