Search code examples
pythonpython-3.xassembly6502

How Do I Handle Forward References with IF Pseudo Ops in an assembler


I am writing a 6502 assembler in Python, and have no idea how to handle this:

It's a two pass assembler, so it figures out symbols in the first pass, then fills them in in the second. Standard stuff. But! I want it to have an ".if" pseudo op, where it will conditionally execute code based on a condition.

So what do I do if the .if pseudo op is testing a forward referenced symbol that has not yet been defined in the first pass?? Without it, I can't expand the code properly to figure out the location of labels to determine symbols!

I was thinking maybe I could simulatenously generate multiple versions of the symbol table based on whether or not the if statement finds Truth, then deciding which version to use in the second pass, but that sounds like a terrible hit to my performance.


Solution

  • This isn't really an answer to your question, more an explanation of why you can admit defeat with honour intact.

    Consider the following:

    .org $400
    label1: nop
    .if label2 - label1 == 1
            nop
    .endif
    label2: nop
    

    What's the final assembly? If the bit inside the .if is excluded than the condition is true and the bit inside the .if should be included. Conversely, if the bit inside the .if is included the condition is false and the .if should be excluded.

    I think it's perfectly reasonable to restrict the .if from using labels that are defined further down the code.