6502 Assembly Language

Once upon a time, lots of software was written in assembly language: a very terse mnemonic code which is about half a step removed from the zeroes and ones used by the computer itself. These days, most programmers don’t bother much with assembly – it’s too much work for too little reward. Still, if you want to understand what’s really going on inside a microprocessor, you’ll want at least a basic understanding. And if you spend your work hours dealing with something like .Net or Javascript, it may be a fun mental exercise to get back to basics.

Back in my undergrad days, I did a little 68000 assembly programming, and a very little x86. That’s long enough ago that I’d basically be starting from zero, so I figured it’d be fun to go all the way back to the 8-bit days.

The MOS Technology 6502 was one of the first really affordable microprocessor chips, and it was very popular in its day. It (and its variants like the 6507 and the 6510) were used in the Apple I, II, and III, the Atari 2600 and 8-bit computers, the Commodore PET, Vic 20, 64, etc., and even the NES. If you know 6502 assembly, you can program a heck of a lot of vintage computer systems. And if you want to do that, you’re my kind of hacker.

Of course, dealing with low-end 70s tech like the 6502 has its advantages and disadvantages. The instruction set is compact and pretty straightforward – officially, there’s fewer than 60 different instructions. Of course, that means that a few useful functions got left out. For example, the 6502 knows how to add and subtract, but not multiply or divide.

Another major limitation is that the 6502 only knows how to deal with 8-bit values. That’s a number between 0 and 255. Even if it could multiply 16 by 16, it wouldn’t know how to represent the result.

Finally, there’s the question of registers. CPUs use registers to store values temporarily, and generally the more you have the easier your life will be. The 6502 has one general-purpose register – the accumulator – that you can perform operations on. If you want to add two numbers together, the accumulator will be the source of one of them and the destination of the result. There’s a pair of index registers which can also be helpful, but you’re limited in what you can do with them. And all these registers are one byte each.

Of course, all these problems are solvable. You can write a multiplication routine using the available operations, and you can even make it work on much larger numbers. The fun part is learning how.