I wonder something about the simple switch trick on the 6502. As simple switches can be slightly annoying to code in assembly, a trick is to use the CMP instruction to skip what would be the "else" statement in C :
For example, let's do a piece of code which eturns 01 if the thing to test is positive, and $02 else. The "clean" way to do it is :
However, doing it in a trickier way in order to save 2 bytes and one label (labels are annoying to define), the trick way is to do that.
Now what I have been wondering if that the cmp instruction does a dummy read to somewhere. There is nothign bad about it, exept if the read accidentally $2002 or $2007, or one of their numerous mirrors (for example reading $38a7 would also read $2007). In that case the results could be catastrophic, as the screen would be gabraged.
Since the next instruction (to skip) is always a two byte instruction, the one performing this trick has to be very carefull about the argument of the two byte instruction in question. If the argument is included between $20 and $3f, he has to become carefull. Something as innocent as sta $20 for example would in fact read $2085 (mirror $2005) which normally have no effect, but I'd still avoid reading it. Luckily, no two byte instruction has $7 or $e as their last nyble of their opcode, making reading accidentally $2007 technically impossible (maybe I missed one tough). But something like ldx $20 would read $20a2 (so $2002) and this could possibly affect the PPU during rendering.
Am I correct to worry about such things or am I imaginating things.
For example, let's do a piece of code which eturns 01 if the thing to test is positive, and $02 else. The "clean" way to do it is :
Code:
lda something_to_test
bmi _else
lda #$01
jmp _endif
_else
lda #$02
_endif
rts
bmi _else
lda #$01
jmp _endif
_else
lda #$02
_endif
rts
However, doing it in a trickier way in order to save 2 bytes and one label (labels are annoying to define), the trick way is to do that.
Code:
lda something_to_test
bmi _else
lda #$01
.db $cd (cmp $xxxx opcode)
_else
lda #$02
rts
bmi _else
lda #$01
.db $cd (cmp $xxxx opcode)
_else
lda #$02
rts
Now what I have been wondering if that the cmp instruction does a dummy read to somewhere. There is nothign bad about it, exept if the read accidentally $2002 or $2007, or one of their numerous mirrors (for example reading $38a7 would also read $2007). In that case the results could be catastrophic, as the screen would be gabraged.
Since the next instruction (to skip) is always a two byte instruction, the one performing this trick has to be very carefull about the argument of the two byte instruction in question. If the argument is included between $20 and $3f, he has to become carefull. Something as innocent as sta $20 for example would in fact read $2085 (mirror $2005) which normally have no effect, but I'd still avoid reading it. Luckily, no two byte instruction has $7 or $e as their last nyble of their opcode, making reading accidentally $2007 technically impossible (maybe I missed one tough). But something like ldx $20 would read $20a2 (so $2002) and this could possibly affect the PPU during rendering.
Am I correct to worry about such things or am I imaginating things.