[stella] JMP(indirect) vs. CMP/BNE/JMP

Subject: [stella] JMP(indirect) vs. CMP/BNE/JMP
From: "Piero Cavina" <p.cavina@xxxxxxxxxxxxx>
Date: Sat, 16 Aug 1997 00:10:40 +0200
Here's a thing about 6502 assembly I've learned writing my game... some of
you might find it interesting.

Suppose you've to write a subroutine that executes a piece of code
according to an index value - the kind of thing that you would do with a
CASE SELECT-type of statement in an high level language or maybe with
something else in C (I don't know C, I'm still at 6502 assembly level, ok?
;)

Here's the first, obvious, solution, the same I had first put in my
program.

CHOOSE1:
 LDA VALUE
 CMP #1
 BNE NOT1
 JMP DO1
NOT1:
 CMP #2
 BNE NOT2
 JMP DO2
NOT2:
 CMP #3
 ........

DO1:
 ........
 RTS

DO2:
 ........
 RTS

and so on.

Later, when I was running out of ROM memory and I had to optimize the code,
I found a better way of doing that. So I've used JMP (indirect) for the
first time, wee! :)

CHOOSE2:
 LDA VALUE
 ASL
 TAX
 LDA JMPTABLE,X
 STA TEMP
 LDA JMPTABLE+1,X
 STA TEMP+1
 JMP (TEMP)
 ........

DO1:
 ........
 RTS

DO2:
 ........
 RTS

JMPTABLE:
 WORD DO1
 WORD DO2
 ........

Please note these numbers:
Let's say that n is the number of possible choices, then the first solution
takes 2+8*n bytes of ROM on the 2600, while the second 17+3*n bytes.
Hence the JMP() approach is better that the first in terms of memory when
17+3*n<2+8*n, that is when n>3... My n was 12 so I saved 45 bytes!

Exercise for the reader: which program is faster? ;)

Ciao,
 P.




--
Archives updated once/day at http://www.biglist.com/lists/stella/archives/
Unsubscribing and other info at http://www.biglist.com/lists/stella/stella.html

Current Thread