Friday, September 25, 2009

The second computer language I learned was 6502 machine code. Eventually, I got an assembler.

The assembler would make two passes. It would determine the values of all the labels in the first pass, and then generate the code on the second pass. Sometimes, the assembler would fail with a "phase error". That meant that the value of a label determined in the first pass didn't match what it was supposed to be in the second pass. I think that was because operations on memory from 0000-00ff, the "zero page", were 2 bytes, while operations on the rest of memory took 3 bytes.

So, "LDA LABEL" would assemble to different instructions, depending on whether LABEL was in zero page. If LABEL was 00C0, it would assemble to A5 C0, but if LABEL was 04C0, it would assemble to AD C0 04. I think the phase error was caused when, during the first pass, if LABEL had yet to be determined, would decide that an instruction like "LDA LABEL" would take 3 bytes, but then, if LABEL turned out to be in the zero page, so the second pass would assemble those instruction into 2 bytes, causing subsequent labels to be off by one. One simple fix would be to always assemble such instructions into the 3 byte operation.

No comments:

Post a Comment