It’s been a good few months for the 8 bit versions of Prince of Persia. Last October saw the release of a C64 version. Yesterday the original source code for the Apple II version was uploaded to github! Read about how Jason Scott recovered that source code from 20 year old disks (similar to what I did recently!)
The game was originally written in assembler so the source code was already out there. How? Machine code is the language a machine understands and assembler is a human representation of that machine code. For example, the machine code “A9 00 8D 20 D0” is actually this more readable assembler: (that inserts the value 0 into the memory location $D020)
The assembler code released yesterday goes one step further. It uses labels, variables and comments. See BOOT.S as a good example. Variables are defined at the top and labels are used throughout making it a lot easier to deal with moving and adding code around. Look for the text “skewtbl” where you’ll find a simple loop that reads in data from memory and inserts it into 2 registers.
:0 ldy sector
:rdsect jsr $005c
:1 dec sector
skewtbl hex 00,0d,0b,09,07,05,03,01
sectaddr hex 00,09,00,00,00,00,00,00
Jordan Mechner puts it more poetically:
Non-programming analogy: Video game source code is a bit like the sheet music to a piano sonata that’s already been performed and recorded. One might reasonably ask: If you have the recording, what do you need the sheet music for?
You don’t, if all you want is to listen and enjoy the music. But to a pianist performing the piece, or a composer who wants to study it or arrange it for different instruments, the original score is valuable.
Props to this Slashdot post for the extra links. Also worth a look is the development diary of the C64 version and there are videos showing how the game was made back in 1985!