8-Bit : Learning from the Sources
Before the advent of 'Open Source', Linus Torvalds learned by reading the assembly language code for his computers. We can do the same today for popular 8-bit systems.
In The Virtues of the 8-Bit Era : Eight Iconic Processor Designs we saw how, in the 1970s and early 1980s, source code for some popular systems of the era was widely shared:
I’ve deliberately put Open Source here in inverted commas. Software of the era wasn’t open source in the modern sense. It was sometimes ‘source available’ though in the sense that software writers published their source code. I still have a copy of the book ‘Sargon : A Computer Chess Program’ which listed a chess program, written in 8080 assembly, in full.
And if the software writers didn't publish their own source then sometimes it would be disassembled and made widely available.
It’s worth putting the timing of this into context. Richard Stallman announced the GNU project, to develop a ‘free’ Unix-like operating system, in 27 September 1983. Development of software for the GNU project started the following year.
One of Stallman’s aims was to enable computer users to study the source code of the software that they were using. Yet, by 1983, users could already study the code for their own systems, either by ‘PEEK'ing (the BASIC command to read a byte from memory) around the Read Only Memory that contained the operating system, or in some cases by buying a book with an annotated listing.
At around the same time that Stallman announced the GNU project, a teenager in Finland was exploring the code that came with his new home computer. Having outgrown the 6502 based Commodore VIC-20, he’d moved on to the Motorola 68008 (a cheaper 8-bit data-bus version of the 68000) powered Sinclair QL:
… I found some bugs in the operating system—or at least a discrepancy between what the documentation said the operating system would do and what it actually did. I found it because something I had written didn’t work.
My code is always, um, perfect. So I knew it had to be something else, and I went in and disassembled the operating system.
You could buy books that contain partial listings of the operating system. That helps. You also need a disassembler, a tool that takes the machine language and turns it into assembly language.
That’s important because when you only have a machine language version, it’s difficult to follow the instructions. You find that an instruction will jump to a numerical address, which makes it very hard to read. A good disassembler will make up names for the numbers and also allow you to specify names. It also can be used to help you identify particular instruction sequences. I had my own disassembler that I could use to create reasonably nice listings. When something didn’t work, I could go in and tell it to find the listing from a particular spot, and I could see everything that the operating system was going to do. Sometimes I used the disassembler not because something was buggy but because I was trying to understand what it was supposed to do.
That teenager was Linus Torvalds. He would progress to an Intel 80386 based Personal Computer, and write his own operating system kernel, which became known as Linux. In the origin story of GNU/Linux, GNU has its roots at MIT and the world of minicomputers, but Linux really starts with Torvalds PEEKing around in his home computers. And Torvalds’s journey was helped by reading disassembly of the operating system in his QL. I’m certain that Torvalds wasn't alone in learning in this way.
This post follows in the steps of Torvalds and gathers together some resources for anyone who wants to learn about assembly language and hardware from the 8-bit era. We’ll focus on two home computers using the two most popular architectures of the era, the MOS Technology 6502 and the Zilog Z80.
(Sadly, I’ve not been able to track down the books, with partial disassembly of the QL’s operating system, that Torvalds was referring to. Suggestions welcome!)
This begs the question, why would anyone do this today? There are many millions of lines of source code, in hundreds of programming languages, available to read on sites like GitHub. The source code for GNU/Linux itself is freely available, if you want to understand how a modern operating system works.
But:
If you’ve enjoyed the ‘ground-up’ approach to learning how computers work, exemplified by the book Code by Charle Petzold, then looking at real assembly language code for 8-bit systems is a natural next step.
If you want to learn the basics of assembly language, then 8-bit architectures are much easier to learn than modern instruction set architectures. The modern x86 ISA has thousands of instructions compared to just 56 for the 6502.
If you don’t want to devote a lot of time to learning assembly but want a basic understanding of how a microprocessor works, then 8-bit assembly language is an easy way to start.
It’s instructive to see how complex operations can be implemented using the simple instructions that were available in these designs. None other than Donald Knuth has used (his own) assembly languages, MIX and MMIX, as tools to teach algorithms in his famous The Art of Computer Programming books.
It can be a lot of fun to hack on 8-bit systems!
I also like to think that it’s healthy to have a reminder that underlying any complex software ‘stack’ - even the sophisticated machine learning models running on the latest GPUs - there is always just a sequence of machine instructions.
Before we get started looking at some 8-bit source code, we probably need guides to 6502 and Z80 assembly language. So we’ll start with two outstandingly clear books on assembly language programming for the two architectures.
The rest of this edition is for paid subscribers. If you value The Chip Letter,then please consider becoming a paid subscriber. You’ll get additional weekly content, learn more and help keep this newsletter going!