There are different ways to handle this. But to start, note that there's an automatic, implied, built-in compare-to-0 in every instructions that loads or affects A, X, or Y; so there's no need for CMP #0 after LDA. Also, if it's a chain of comparisons against successive numbers, you can increment or decrement the number each time (instead of re-loading it) and take advantage of the automatic, implied compare-to-0 in that instruction. Below, I used DEC A (decrement accumulator), or DEA; but if you have only the NMOS 6502 and not the CMOS, you won't have that instruction, but you could load the number initially into X or Y instead and then use DEX or DEY.
Code: Select all
MyFunction:
lda #MY_COLUMN
bne 1$
; your code
1$: DEC A
bne 2$
; your code
2$: DEC A
bne 3$
; your code
3$: DEC A
BNE MyFunctionDone
; your code
MyFunctionDone:
(You may need to change the label system to work with whatever assembler you're using. Also, I don't know what you have at the end of each "your code" section, regarding where it goes when it's done.) I have a bunch of these in section 20 ("
Tips For Programming the 65(c)02") of the 6502 primer.
Another thing you can do for successive numbers is use a table. This runs faster and saves memory if there are quite a few choices. If you can't confine the numbers to even numbers, like if you really need 0, 1, 2, and 3, then load it, double it with ASL, transfer it to X, and use it for indexing in a table of addresses. (The doubling is because each address in the table takes two bytes.) What you do need to be careful of in this case is that if the input is ever something unexpected, you will index outside the table and probably get garbage as an address to jump to, and you'll get a crash!
I like to use structure macros, per my article at
http://wilsonminesco.com/StructureMacros/ . A CASE structure would be good for a situation where the numbers are somewhat random, like if you wanted to have special handling for certain characters like <CR>, <LF>, and <BS>, and let all the others get handled a default way. So you could have for example:
Code: Select all
CASE ACCUM ; Test the accumulator against the following cases.
CASE_OF $0A ; If it has the linefeed character,
<actions> ; execute these instructions,
<actions>
END_OF ; then jump to the first instruction after END_CASE.
CASE_OF $0D ; If it has the carriage-return character,
<actions> ; execute these instructions,
<actions>
END_OF ; then jump to the first instruction after END_CASE.
CASE_OF $08 ; If it has the backspace character,
<actions> ; execute these instructions,
<actions>
END_OF ; then jump to the first instruction after END_CASE.
<actions> ; If the character is anything else, do these actions
<actions> ; to feed it to the display as display data.
END_CASE
As you might expect, what it assembles is a series of CMP #__, BNE instructions, plus the jumps to the first instruction after END_CASE.