Part One!
Don’t miss next issue, subscribe on page 16!
Part One!
Don’t miss next issue, subscribe on page 16!
OUR EXPERT David Bolton worked as a games developer back in the ’80s, programming in both Z80 and 6502 assembly language on ZX Spectrum, MSX, Amstrad CPC-64 and CBM-64/CBM-16 home computers.
QUIICK TIP
If you get a ‘Symbol Lookup error’ when you run as65, you may have to uninstall the Dotnet Snap and install Dotnet using Apt.This command removes it: sudo snap remove – purge dotnetsdk . Then use the Apt command in the Quick Tip opposite.
QUIICK TIP
If you get a ‘Symbol Lookup error’ when you run as65, you may have to uninstall the Dotnet Snap and install Dotnet using Apt.This command removes it: sudo snap remove – purge dotnetsdk . Then use the Apt command in the Quick Tip opposite.
This article shows you how to create a 6502 assembler, which we’re calling as65. It won’t be quite as powerful as the xa65 assembler used in earlier articles (www.floodgap.com/retrotech/ xa – see LXF313) but it’ll be our own!
To get started, here are the steps it will go through when assembling: 1. Read source file and options. If there are any include files specified, read those in. 2. Do the first pass, evaluate labels. 3. If there were no errors in the first pass, do the second pass. Emit the machine code to disk. We use two passes because of page-zero support. If we didn’t have page zero, all addresses would be 16-bit. Accessing page-zero means instructions are one byte shorter and run slightly faster.
The problem is when a label is referred to before it is defined. If the memory location accessed is in page zero, the instruction will be two bytes long and this will affect the location of labels that follow.
The first pass builds up a dictionary of labels. By the time it has done the first pass, it knows the value of every label and can then generate correct code on the second pass.
Rules to follow
If you follow these rules, it will be less problematic.
1. Declare any zero-page memory locations before you use them. Think of these as the variables that your program will use.
2. Labels used for subroutines or branching forward can be called before they are declared. After pass one is done, the length of all instructions will have been determined.
You might ask why are we even using page zero, because instructions that access zero-page locations are two bytes long not three, and execute in three clock cycles not four.