18

Possible Duplicate:
How was the first compiler written?

This question has always been bothering me. To compile a program, you need a compiler, which is also a type of program, so what compiled the compiler? Somebody told me that the first compilers were written in assembly or machine code. But thinking about that, that is still not the complete story. After all, how does the machine code go from the hard drive to RAM to the CPU without an operating system and drivers? The drivers had to have been programmed somehow.

I know that the very early computers had switches and allowed you to flip the switch to indicate bits. I am wondering how the leap was made from switches to a way to get the CPU to read machine code without needing a computer program to tell it to do so.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Not a "duplicate" as such, but [my answer to an earlier question](http://stackoverflow.com/questions/494372/how-can-the-linux-kernel-compile-itself/494394#494394) is responsive at some level. – dmckee --- ex-moderator kitten Apr 19 '11 at 01:58
  • 1
    Anyone who ever used front panel switches to input a program on a computer that didn't have a hard disk or compiler knows that this question is not an exact duplicate of questions about compilers. – Windows programmer May 15 '12 at 06:55
  • Hard drive? Operating system? Drivers? There were no such things. "a way to get the CPU to read machine code without needing a computer program to tell it to do so." --- this makes no sense; the CPU reads and executes the instruction in memory pointed to by the program counter, over and over again. – Jim Balter Jun 03 '22 at 06:50
  • "Possible Duplicate: How was the first compiler written?" -- this is *obviously* not a duplicate. It seems that the folks who closed this question didn't bother to read either it or the duplicate. – Jim Balter Jun 03 '22 at 06:53

3 Answers3

19

The short answer: the first programs were meticulously written in raw machine code, and everything was built up from there.

The idea is called bootstrapping. Suppose that you have a bare machine with a processor, some flash memory, and a hard disk. Typically, the processor is configured on power-up to load a simple operating system called the bootloader from a fixed location in non-volatile memory (for example, CMOS or flash). This OS is extraordinarily simple and has just enough functionality to point the computer to the spot on disk where the real OS lives. This OS can then turn on more and more devices and load more and more complicated programs, until eventually the whole OS is up and running.

But what is this bootloader written in? Originally, these were written in raw machine code and hardcoded into the machine. The programs it would run would be written in machine code as well, which would be unbelievably slow and tedious to work with. Eventually, someone wrote the first simple assembler in machine code. Once you have this assembler, you can start writing programs in assembly, including the assembler itself. In fact, once you have a simple assembler, you never need to write machine code again. You can just keep writing the assembler in assembly!

From this point you can build more complex programming languages by first writing the compiler using existing tools (the assembler, for example) to get just enough functionality available so that the compiler can do basic programming. You then use that compiler to write a compiler for the programming language itself, and use the same trick to build off of your previous work to get something bigger and cooler. This technique is still used today - most compilers are written in the language they compile to.

To summarize, everything had to be done by hand at some awful point in the past, but thanks to the hard work of the people who did this we can build off of what's already there.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 11
    "written in raw machine code"? Using what? A text editor? A magnetized needle writing on a disk? That part is a bit vague... – user541686 Jan 23 '11 at 07:45
  • 9
    Raw machine code was written with pen and paper. Pencil and paper might be considered better, but erasers cost more than scratching out mistakes and rewriting them. Front panel switches were flipped as necessary to enter bits (bytes) into memory. Eventually someone took a journalist's teletype machine and connected it to a computer, so streams of bytes could be saved on paper tape and read back in again later. Someone else took a data warehouse's punched card machines and connected them to a computer. Someone else did the same with magnetic tapes. Disk drives came later. – Windows programmer May 15 '12 at 07:02
  • @user541686 A text editor is a program, so it couldn't have been used to write the first program. And the first computers didn't have disks. How programs and other information was entered into the first machines depends on the machine, and isn't really relevant to the question here. – Jim Balter Jun 03 '22 at 06:31
7

In the early days of the microcomputer industry, you had to laboriously enter machine code directly using toggle switches. A classic example is the Altair 8800 (shown below though possibly a replica):

enter image description here

With this, you would set the binary switches for the address and/or data bits, then use one of the command toggle switches to either:

  • set the current address to whatever is indicated by the address switches; or
  • write the data from the data switches to the current/next address.

The Altair, after RESET, had the current address set to zero and, since this is where the CPU will start executing code, that was usually sufficient. Otherwise, you could set the address switches and toggle EXAMINE to set the current address to something else. That address selection process was also needed in the case where you had entered your program incorrectly and needed to patch it (rather than doing it all again from scratch).

Once the address was what you wanted (EXAMINE would show it on the address LEDs along with the current content on the data LEDs), you would toggle the data switches and do DEPOSIT to put that data into the current address (DEPOSIT-NEXT was identical but it first incremented the current address before depositing, useful for sequential entry).

The whole process can be found on sites like this one, with the basic idea being:

  • Write the program out by hand, in assembly language.
  • Hand-assemble that into binary code, using whatever manuals you have for the CPU, giving you a series of address/data pairs.
  • Use the EXAMINE/DEPOSIT/DEPOSIT-NEXT process to enter each byte into memory, being very careful that the LEDs correctly indicate the byte you just deposited.

That second bullet point is probably the hardest bit since you need to adjust for jumping to instructions that may not have yet been assembled, so you're basically basically a slow, biological, multi-pass assembler.

For an example, consider the following (very simple) 8080 code, gleaned from online sources:

0000    c3 00 00    jmp 0000 ; Infinite loop.

Note those data bytes c3 00 00 are hexadecimal but people often used octal since the Altair switches were grouped into three rather than four; that would give 303 000 000.

Hence, you would, for that specific program:

  • Toggle RESET, giving a current address of zero.
  • Enter binary data switches 101 000 101 for octal 303, the JMP opcode.
  • Toggle the DEPOSIT switch, putting that byte into memory location 0, and showing that address and the deposited data on the LEDs for checking.
  • Enter binary data switches 000 000 000 for octal 000, the first byte of the address to jump to.
  • Toggle the DEPOSIT-NEXT switch, advancing the current address to 1, depositing the byte in that address and again showing the address and data on the LEDs.
  • Toggle the DEPOSIT-NEXT switch again, advancing the current address to 2, depositing the byte in that address and again showing the address and data on the LEDs. There was no need to change the data switches as they were identical to the previous byte.

At that point, the program is in memory, and you can toggle RESET then RUN to see it in action. Though, if you wanted to see the LEDs change as it ran, you'd probably continuously toggle SINGLE-STEP instead.


The other thing to keep in mind is that there's no requirement that a program be written on the platform it's intended to run on. Cross-assemblers (or compilers) running on another platform can be used to bypass the laborious hand-assembling process described above.

You still have to get the bytes into the target platform but there were solutions for that as well. Paper tape readers could be added to Altair so, provided the cross-assembler system could produce the tapes, only a small loader program would be needed to load and run it.

For example, in the early embedded days (before "embedded" meant huge honkin' Linux boxes), I was part of a team developing operating system, drivers, and application code for a 6809-based device. The actual development tools were all hosted on an early UNIX system, and these were burnt onto EPROMS for insertion into the devices. No development at all took place on those devices, as they were very tailored toward their one real task.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • There were no "microcomputers" at the dawn of the computer age. And the first computers used stepping switches, not toggle switches. "the early Altair machines" were made several decades after the first programs were written. – Jim Balter Jun 03 '22 at 07:01
  • The links broke, do you have another version? – asheroto Mar 09 '23 at 10:02
  • 1
    @asheroto: I couldn't find equivalent sites so I rejigged the answer to bring it up to date and found a similar site. I also brought in quite a bit of information in case *that* site also disappears at some point. – paxdiablo Mar 09 '23 at 12:14
0

Once upon a time to use a computer you entered machine code in binary. People got tired of doing that, so they made a program (with machine code) that would read assembly. After a while they realized that writing in assembly sucked, so they used assembly to make high level languages such as FORTRAN.

To get the full story, enroll in college and take some CS or COMPE classes.

regality
  • 6,496
  • 6
  • 29
  • 26
  • 11
    The last sentence is true but quite disappointing for an answer IMHO... – user541686 Jan 23 '11 at 07:45
  • ...Or watch some YouTube videos for free on the subject, and save yourself a lot of time, headaches and money. I'd search for something like "How were the first computers programmed?" for some great videos on the subject. – HartleySan Mar 29 '22 at 13:21
  • @user541686 Not really. SO works well as a repository for answers to "how to" questions about programming. It's not a substitute for an education. – Jim Balter Jun 03 '22 at 06:43