6502 Coding Tips

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
Movax12
Posts: 529
Joined: Sun Jan 02, 2011 11:50 am

6502 Coding Tips

Post by Movax12 »

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:

Code: Select all

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

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.
Post Reply