Yesterday, I was updating some code of my PET 2001 emulation and had a hard time recovering the secondary address for the LOAD command. While the original value, stored in the zeropage, had been overwritten by some subsequent routines, eventually, I found it could be recovered from the X-register! And this, after the flow of control had traversed quite a number of instructions!
Actually, this isn’t a rare case. If you have a closer look at the BASIC routines, the Y-register is heavily used, as is the stack, but the X-register rarely so. It’s more used as a store, some zeropage addresses even enjoy a more frequent use. And then, when you think that a value has been ultimately lost, Commodore BASIC produces that value out of thin air, like a magician, right out of the sleeve that is the X-register.
Provided that the 6502 features just 3 usable registers (AC, X, Y), this appears to be a rather peculiar use of a that rare and crucial resource like a processor registers. Is there a prehistory to this (as in other processors)? How does this compare to other flavors of MS BASIC, like z80 code (which I’m not familiar with), is there similar to be observed?
And here’s the full story, including relevant bits of the ROM listing: