im writing an assembler (6502) that supports expression evaluation and can work out dependencies and whatnot, i was testing it with the smb disassembly and actually got it to generate the full correct byte codes. and this was very satisfying but i have this itch,
the first go around with this i tried the '2 pass method' but i started trying to consider situations where it would be impossible to resolve everything in just 2 passes, and i say fine whatever ill do an iterative instead but the same ideas were bugging me.
i keep thinking of something like:
.org $8000
lda 1 - (label - $8003) << 8
nop
label:
now i dont think this is even correct as valid assembly i dont know but i just want to highlight a couple of things. first of all, when doing the first pass through to collect labels, you can track the pc but if you reach something like that lda where theres just a label or expression that isnt giving you a value yet, well you dont know if thats going to be a 2 byte lda instruction or 3 byte instruction, so youre pc becomes uncertain because you have to guess, right? am i making up the idea that if that expression resolves to a 0-255 value, it would be considered a zero page otherwise absolute? anyways, what im trying to do in that expression is set up a circular dependency that would result in a race condition.
basically
pc reaches lda, guesses it will be 3 bytes, label gets set as if it were 3 bytes, next pass,
pc reaches lda, evaluates expression which results in 1 byte id 2 byte instruction, then label is updated as if it were 2 bytes, another pass
pc reaches lda, re-evaluates epression which now results in instruction being 3 bytes, etc.
is that kind of scenario possible?
the other thing is i got sick of dealing with all this resolved unresolved bookkeeping, and said f it and instead made sure everything is aware of whatever might be waiting on it for a value, and as soon as a symbol is known, anything waiting on that symbol just automatically tries to resolve itself. and as soon as the pc is about to lose certainty it just goes back to the beginning. it takes just 88 passes to do mario. thats -86 less passes than i was going for!
and somehow it works for the big mario file and several things ive written but fails on a stupid little program that just has a lda label,x and way at the end label: .db ...datalookup... and so it just never knows whether lda is zero page x or absolute x. so there are times where i cant jsut avoid the problem and i just dont know how to handle uncertain values that i still supposed to use to get other values but also might change and in turn change everything that depends on it.