There is a 6502 asm tricks thread here but I am curious if anyone has any other simple, straighforward, but maybe overlooked coding techniques that maybe aren't considered quite hacks/tricks for experienced 6502 coders, but useful to share with those that are a bit newer to 6502 asm.
For example, a 16 bit compare:
The cmp instruction always sets the carry first, so for the second part of the compare you have to use sbc to not set the carry.
For example, a 16 bit compare:
Code:
foo .res 2 ; 16 bit variables
bar .res 2
lda foo
cmp bar ; carry clear if low byte of foo < bar, set if foo >= bar
lda foo + 1
sbc bar + 1 ; subtract high byte bar, or in effect subtract one more if foo < bar
bcc label ; if all 16 bits foo < bar goto label
bar .res 2
lda foo
cmp bar ; carry clear if low byte of foo < bar, set if foo >= bar
lda foo + 1
sbc bar + 1 ; subtract high byte bar, or in effect subtract one more if foo < bar
bcc label ; if all 16 bits foo < bar goto label
The cmp instruction always sets the carry first, so for the second part of the compare you have to use sbc to not set the carry.