Hi!
I recently started a fun little project: making a 6502 assembler in python.
The assembler itself isn't the real goal though, it's really all about testing my knowledge of the processor.
As of now it works pretty much the way it's supposed to. However like pretty much everything regarding programming, just because it works doesn't mean it's good.
I feel like my implementation of the assembler is akward.
Of course I could just take a look at the source code of an already existing assembler, but then it would be cheating!
I'd rather ask for some advice here.
So here's how the assembler works for now.
The instruction set is stored in a dictionnary. The key is regex string and the value is a tuple containing the opcode of the instruction and its number of operands.
Here's an example:
Code:
"^STA \$([0-9A-F]{2})$" : (133,1)
It corresponds to the STA ZPG instruction.
And here's how the 6502 code is assembled:
- First the program is loaded from an external file. Everything is converted to uppercase. Extra tabs, spaces and empty lines are removed, and each line (i.e. instruction) is stored in a list.
- Two passes are then realized. The first one converts binary and decimal constants to hexadecimal. The labels' addresses are stored in a list and the labels are removed.
- During pass 2, labels used in instructions are replaced by their offset in branching instructions and by their absolute address in other instructions.
- Finally, each instruction is converted to its corresponding opcode and operands
As stated before, I have no idea if I'm doing this the right way.
If you could help me that'd wonderful!
Thanks!