| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
fastest or smallest 'switch' statment
I use the following code a lot to switch between two values and was wondering what others do and if there is a faster, smaller or simply more elegant way someone has come up with.
lda #value1 ; default value: AR = value1
ldy switch ; get value of switch in YR
cpy #case1 ; compare switch
beq @cont
lda #value2 ; if switch != #case1 then AR = value2
@cont ... ; AR = value1/value2 depending on switch
|
|
... 14 posts hidden. Click here to view all posts.... |
| |
soci
Registered: Sep 2003 Posts: 480 |
Fungus: There was at least a '#' missing.
mod1 lda #value1
eor #value1 ^ value2
sta mod1 + 1
But this is not what he wants to do.
For $c0 and $d0 only a shorter table could do it fast.
ldy switch
lda table - $c0,y
table .fill $d0 - $c0, value1
.byte value2
|
| |
lft
Registered: Jul 2007 Posts: 369 |
Quoting iAN CooGa table, use y as index
ldy switch
lda table,y
table
byte value1, value2, etc
Sometimes this could be faster:
switch = * + 1
lda table
|
| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
Thanks for all the input. good stuff. I dont have $100 to spend on a table unfortunately.
I use it in a number of functions where the switch values are different (but all to switch between two states)... so it would lead to multiple tables.
The 'cmp' is probably the best in this use case, but it just felt so inelegant using it all the time ;-) |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
can you cite a real world example where you need to use this ? maybe it can be done algorithmically better. |
| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
In my game I have a number of objects (enemies, bullets, ...) and each has a specific identifier (0-255). In many of the object routines I check for a specific object or a specific state and take one of two actions or set one of two parameters.
Hope this helps explain it. Its always a balance of speed and size for a game. Tables can be obvious as can unrolled code or specific code for specific objects ... but with level graphics, music, etc. I find myself often in a compromise between the two.
E.g. the table lookup is faster, but cant do too many $100 tables. Specific code for each object is faster, but similar ends up eating up memory quickly.
Hope that gives some context. I am decently happy with what I mentioned originally in terms of compromise to switch between to values depending on a parameter ... but just thought I would seek some inspiration as well.
Thanks to everyone who chimed in! |
| |
soci
Registered: Sep 2003 Posts: 480 |
Quoting mhindsboThe 'cmp' is probably the best in this use case, but it just felt so inelegant using it all the time ;-)
Well, then use the one below for a change ;)
lda switch ; get value of switch in AR.
eor #case1 ; compare switch.
beq @cont
lda #value2^value1 ; if switch != #case1 then AR = value2
@cont eor #value1 ; default value: AR = value1
; AR = value1/value2 depending on switch
It's not faster or shorter, but it's only using the accumulator and does not destroy the carry. |
| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
nice ... I like it :-) |
| |
Bitbreaker
Registered: Oct 2002 Posts: 508 |
Depending on the values you wish as result, there's also the option to go like this:
cmp #switch
arr #$00
this would work if you need $80 and $00 as resulting values, so it would be interesting what the input and output values are, for some, there can be nice code constructs to generate the resulting values with a few mnemonics and without a branch.
Also, if the result would be just 2 values, the table approach woud just need 2 values strayed into the code somewhere, should be bearable in regards of memory footprint.
Another example that sets bit 4 depending on carry and toggling bit 5 on every call:
cmp #switch
and #$ef ;clear bit 4
adc #$20 ;toggle bit 5 and set bit 4 depending on carry -> adc #$20/21
ora #$0f ;set all lower bits again (might be omitted)
|
| |
lft
Registered: Jul 2007 Posts: 369 |
Quoting mhindsboIn my game I have a number of objects (enemies, bullets, ...) and each has a specific identifier (0-255). In many of the object routines I check for a specific object or a specific state and take one of two actions or set one of two parameters.
This will again depend on circumstances, but sometimes it is useful to encode information about the objects in a flag table. If you have 256 objects, and you need to switch one way or the other depending on whether an object is edible, held, a key, dangerous etc., then you might encode that as flags in a table. Some flags will be static, and some will change during gameplay. Then you have a one-page table that tracks eight such flags per object.
; object number in y
lda flags1,y
and #$40 ; is this a bullet?
beq ...
|
| |
Frantic
Registered: Mar 2003 Posts: 1648 |
http://codebase64.org/doku.php?id=base:dispatch_on_a_byte |
Previous - 1 | 2 | 3 - Next |