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)
LDA #00 STA $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 lda skewtbl,y sta $3d lda sectaddr,y beq :1 sta $27 :rdsect jsr $005c :1 dec sector bne :0 lda SLOT jmp $900 skewtbl hex 00,0d,0b,09,07,05,03,01 hex 0e,0c,0a,08,06,04,02,0f sectaddr hex 00,09,00,00,00,00,00,00 hex 30,31,32,33,34,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.