11

Having taken a course on compilers and making a rudimentary one by myself, i still have this lingering doubt about the first compiler.

From a high to low level, i see code running in lets say C or C++ which gets converted to the respective assembly language equivalent by it's compiler (lets say gcc). This code is platform dependent (lets say i'm on intel x86 architecture).

Now comes the question, how does a hardware run assembly?

I remember from my computer organization class that each and every assembly statement gets converted to a specific format (depending on the processor) for example, a statement like mov ax,bx gets converted to it's opcode let say 0110 101010 101000. Assuming the assembler parses every statement in my assembly language program and converts it into a machine code, then how was the first assembler written?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user720694
  • 2,035
  • 6
  • 35
  • 57
  • Technology, especially computers, is inherently iterative; we use tools to make better tools, over and over again. The first "assemblers" were card punches; operators took handwritten or typed instructions and used punches to both translate the commands/registers/etc to their binary opcodes and to persist those commands in a repeatable, machine-readable form. Before that, programmable computers had to be programmed by "hardcoding" the electrical paths using a switchboard. – KeithS Aug 17 '12 at 14:19
  • 1
    Writing machine code by hand isn't really a problem as such. Just tedious. So the task was automated - by writing an assembler in machine code. – harold Aug 17 '12 at 15:03
  • What is the main question? How was the first assembler written? That on programmers: http://programmers.stackexchange.com/questions/129123/were-the-first-assemblers-written-in-machine-code Compiler bootstrapping: http://stackoverflow.com/questions/1653649/how-was-the-first-compiler-written – Ciro Santilli OurBigBook.com Nov 07 '15 at 20:32

3 Answers3

12

Actually I think you understand. First off the title question, how does hardware run assembly. Hardware runs on machine code or machine instructions or whatever term. As you have correctly described assembly is a representative of that machine code, and not always but is close to a one to one relationship one asm instruction to one machine instruction. These being bits, ones and zeros, the hardware can now perform the actions that the bits describe.

Now how is the first assembler written? With pencil and paper. You typically write down the instruction in some sort of pseudo assembly as you may not have completely defined the language, and then you write down the bits based on the encoding, the same thing an assembler would do. Then using some mechanism you feed those bits into the computer and tell it to run.

Eventually, naturally, this becomes tedious for larger programs so you decide to write a larger program that parses a language that is easier to write, then repeat that with more complicated languages and programs.

Even today depending on the team and how they do things and the individual engineer testing the instruction decoder, etc. Writing machine code by hand still happens. Eventually the assembler is created and you switch to that and sometimes there is a higher level compiler and you switch to that for the bulk of the coding, but in the chip development world you still are very aware of and from time to time will modify the bits of an instruction at the machine code level.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Thanks. Combining your explanation with the links posted in the answer by Gaius below helped me in creating a good mental image of the whole process. Much appreciated. – user720694 Aug 18 '12 at 14:19
  • http://en.wikipedia.org/wiki/Computer_programming_in_the_punch_card_era my father would bring home boxes of punch cards when in college. these were using fortran in http://en.wikipedia.org/wiki/Extended_Binary_Coded_Decimal_Interchange_Code EBCDIC is similar to the ASCII we use today (alphabet, etc represented in bit patterns). I dont think these cards were machine code, only programs that you needed an assembler or compiler for (which you needed paper tape, etc to boot the machine). – old_timer Aug 18 '12 at 14:41
  • so not only did you need the card reader software loaded but also the compiler/assembler. – old_timer Aug 18 '12 at 14:42
  • Note, there was a literal "bit bucket" where chads from the paper would fall in under the punch machine (which was like a typewriter). – old_timer Aug 18 '12 at 14:48
  • Pencil and paper. That brings back memories of writing inline machine code in old versions of Turbo Pascal. You took a piece of paper, wrote the assembly, and manually translated that to machine code understandable to Turbo's `inline` statement. Somehow using pencil and paper was more comfortable that doing it directly on the computer. – ninjalj Aug 19 '12 at 10:55
  • In your answer you have said "Hardware runs on machine code". It is actually the other way round. Machine code runs on hardware. –  Nov 05 '20 at 10:42
  • 1
    @ShashankVM a car runs on gas, the OP said hardware run on assembly, the answer is no hardware runs on machine code based on the op's language but better to say hardware runs machine code or machine code runs on hardware. – old_timer Nov 05 '20 at 14:24
5

It was "toggled in" on the front panel or read in from paper tape. You could work out the binary and either set the switches or make the holes by hand, converting opcodes in your head. Legend has it that Seymour Cray entered the entire first Cray OS this way.

Gaius
  • 2,556
  • 1
  • 24
  • 43
2

"Hardware", mostly the CPU and memory, is pretty much a finite state machine. Its states/outputs and inputs are roughly the contents of the registers and memory.

Your program compiled down to the machine codes that the FSM "understands" drives the FSM from state to state. That's how "hardware runs assembly".

Unless the first assembler/compiler is cross-compiled (developed and compiled on a different platform), the first one is written and translated to machine codes manually and then those are typed in.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180