Tales of the Rampant Coyote

Adventures in Indie Gaming!

Programming a Computer with Nothing

Posted by Rampant Coyote on December 7, 2016

I don’t know why this irritates me, but it does. Several times over the last few weeks, I’ve seen memes like the one on the right, generally from programmers who think it’s cute.  As if it really was a mystery. But… if you are not a programmer, or you are a programmer who actually doesn’t know this but doesn’t want the embarrassment of having to admit it, here’s how it works:

You create a programming language with… another programming language. Another compiler / tool. They’ve got tools nowadays that can accept a new programming language in a standardized format, and will convert it to object code ready to be compiled to whatever machine you want to put it on.

Oh, but how do you build the compiler? With another programming language. 🙂

This was a required class to get my Computer Science degree back in the day. Admittedly, it was a tough course. You were strongly advised not to take the compiler course (where you developed your own language, and built a compiler for it) and the operating system course (where you built your own operating system, from more-or-less scratch) in the same semester.

Then you just put that code into a format that the new computer with a way for it to self-boot it (meaning load it up and begin executing instructions) from memory or storage. You just need a way to put a program there in the first place. The usual approach is two write code on an existing machine, and then target the compiler to compile for the new computer, and put that code on a bootable medium (like a CD-ROM, or an SD card).

Or you don’t even use a compiler, and you write it all in straight machine code. In the olden days, they had machines that were kind of like electric typewriters that would allow you to create punch-cards that a computer could read and execute. You can even write code for a machine that doesn’t even exist yet, and compile it for that target machine. In fact, if you go even further back into the 1800s, Lady Ada Lovelace wrote programs for Babbage’s Analytical Engine which was never actually produced. She was the world’s first programmer, and the machine she developed for was permanently vaporware. Sad, huh?

If you aren’t a programmer, and you have no idea what a “compiler” is or what “machine code” means, here’s what you need to know:

Computers understand numbers. Now, we always say it’s “ones and zeros” because that’s how numbers are represented in the computer. But don’t get bogged down by those bits. Just think of it as numbers. A… code. Every processor is different, and understands different sets of codes. Maybe on one machine, the value of “1” means to jump from the part of code you are currently executing to a whole different place. Maybe the value of “2” means to add two numbers together and store them somewhere.  The processor also has some registers, which designate its state and serve as temporary holding areas for values.

The position of these numbers is important, too. A number three might be an instruction, the literal value of three, or an address (the third byte of memory), or even an offset to another address stored in a register. Or… well, it depends on the processor.  To avoid confusion, we poor humans use symbols and keywords make all that slightly more readable. So if the instruction “1” means to jump to a new location in memory and resume execution, and we wanted to execute at the memory location 1, we’d probably write it as “JMP 1” instead of “1 1”. But if we’re looking at the raw values in memory, they’ll be the latter.

Now, back in the old days, you might write complete commercial software at that level.  You still can, but it’s a slow, hard way to go about it. It gets very complicated to do even a very simple operation.

Instead, we write things in a higher-level language that is a lot more readable for humans, and encapsulates a lot of complexity in a simple format. So we might write an instruction that reads, “Healthbar.SetValue(100)”, which might actually represents hundreds of instructions of machine code. We use a compiler to convert that higher-level code into machine code.

So… I hope that clears things up. There are C++ compilers actually written in… C++.  But it’s not really brain-twisting. You use compiler A to build compiler B, and compiler B may be for a totally different system. As long as compiler A understands how to convert stuff down to machine code for the new system, it’ll all work fine.

Now… what if you have no tools like that and have to do it all by hand / from scratch / etc.?  Well, the earliest PCs were that way… the rudimentary Altair interface used a bunch of switches and lights for its UI. You could program it that way. I wouldn’t want to write Skyrim that way, but it works.

I hope you feel enlightened.

Filed Under: Programming - Comments: 2 Comments to Read

  • CdrJameson said,

    Did have a fun time back in the early days of the internet trying to get a version of gcc.
    I needed it, because I didn’t have a C compiler.
    You could download it from several places – in C source form.
    This was somewhat frustrating.
    Eventually I found a binary on a coverdisc from somewhere.

  • Darius said,

    I never took a compiler class, but I did take a class where we used a circuit design/simulation package to build a simple 4 or 8 bit, I don’t remember which, computer from the logic gates up.
    Then we programmed it using switches like the Altair, built a simple bios, and wrote simple programs for it. It was really cool.
    It wasn’t as in-depth as a compiler class would have been, but still gave a good idea of what it would take to build a programming language.