ORA: A Special Use in Branch Testing
Posted on
By Kodiak
In a recent post, I talked about some special uses for the
EOR instruction, particularly with regard to saving CPU time on addition and subtraction in 6502
assembly coding on the Commodore 64.
Now it's the turn of ORA, which can also be used for adding within certain conditions on 6502,
as per
this Codebase piece on Combining Bits / Substitute Logical Operations.
Changing track from maths, a nice little hack you can apply (and which is used occasionally in
Parallaxian)
is where you start with something prosaic like this:
| LDA ZPPLANEBLOWMODE | ; Disable effect if plane is exploding | |
| BNE QUIT | ||
| LDA ZPTAILSLIDE | ; Disable effect if plane is tail-sliding | |
| BNE QUIT | ||
| LDA ZPTURNSTATUS | ; Disable effect if plane is turning | |
| BNE QUIT | ||
| LDA ZPRESPAWN | ; Disable effect if plane is respawning | |
| BNE QUIT | ||
| ; EFFECT | ... | |
| ... | ||
| ... | ||
| QUIT | RTS |
The above assumes, in all instances of the variables being tested, that 01 = relevant condition is active and 00 = relevant condition is inactive (i.e. turned off).
Note that if any single one - or more - or all - of those conditions is / are active, the effect will not be performed; in other words,
all of those conditions must = 00 for the effect to be executed.
You could, in plain English, say this:
"If any one or more of ZPPLANEBLOWMODE or ZPTAILSLIDE or ZPTURNSTATUS or ZPRESPAWN is turned on, then we do not perform the effect."
So we can convert that sentence into code thus:
| LDA ZPPLANEBLOWMODE | ; Disable effect if plane is exploding | |
| ORA ZPTAILSLIDE | ; Disable effect if plane is tail-sliding | |
| ORA ZPTURNSTATUS | ; Disable effect if plane is turning | |
| ORA ZPRESPAWN | ; Disable effect if plane is respawning | |
| BNE QUIT | ||
| ; EFFECT | ... | |
| ... | ||
| ... | ||
| QUIT | RTS |
By the same token, you could use AND everywhere instead of ORA and finish with a BEQ to the exit location, but the key aim is always the same:
to save some needless branch-testing cycles and RAM by using ORA or AND in this fashion.
____
Similar post:
Illegal Opcode SAX/AXS: A Practical Use
____
If you value my work and want to support me, a small donation via PayPal would be nice (and thanks if you do!)