17

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
  • 286,368
  • 41
  • 520
  • 731
  • 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

17

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
  • 345,949
  • 98
  • 857
  • 1,030
  • 10
    "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
  • 8
    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
6

In the early days of the microcomputer industry, we had to laboriously enter machine code directly using toggle switches. This was very similar to the way the first non-hardcoded-program computers were done.

See here for details on the early Altair machines. Basically, you set the binary switches for the address and data then used a command switch to write that to memory. Yes, one byte at a time. We were "real men" back then :-)

From that same site is the detailed process used to enter a sample program.

You should also keep in mind that you don't have to compile programs for machine X actually on machine X. Once machines reached a certain level of sophistication (where, for example, machine Y could run without toggling in programs), you could use cross-assemblers and cross-compilers to actually create the machine language for machine X.

Then you just needed a way to get the binary image of that program into machine X. It wasn't always easy but it was a darn sight easier than toggling switches.

paxdiablo
  • 814,905
  • 225
  • 1,535
  • 1,899
  • 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
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,428
  • 6
  • 28
  • 26
  • 9
    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