Logical operations: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) |
|||
Line 60: | Line 60: | ||
FALSE |
FALSE |
||
</pre> |
</pre> |
||
=={{header|6502 Assembly}}== |
|||
There are no built-in boolean types; however, supporting the concept in software is trivial. Typically, the zero flag or the carry flag can act as a boolean, with zero being false and nonzero being true. |
|||
<lang 6502asm>LDA myBoolean |
|||
BNE isTrue |
|||
;code that would execute if myBoolean is false, goes here. |
|||
RTS |
|||
isTrue: |
|||
;code that would execute if myBoolean is true, goes here. |
|||
RTS </lang> |
|||
===Branches Based On Equality to Zero=== |
|||
A logical AND can easily be implemented as a nested if. Here, we'll be executing the following pseudocode. For this example, all variables are one byte in size. |
|||
<lang C>if(myValue == 3 && myOtherValue == 5){ |
|||
myResult = true; |
|||
}</lang> |
|||
<lang 6502asm>LDA myValue |
|||
CMP #3 |
|||
BNE .skip |
|||
;if we got to here, "myValue == 3" evaluated to true. |
|||
LDA myOtherValue |
|||
CMP #5 |
|||
BNE .skip |
|||
;if we got to here, both "myValue == 3" and "myOtherValue" == 5 evaluated to true. |
|||
STA myResult ;any nonzero value is considered TRUE, so we've stored 5 into myResult. |
|||
.skip:</lang> |
|||
A logical OR is somewhat similar. |
|||
<lang C>if(myValue == 3 || myOtherValue == 5){ |
|||
myResult = true; |
|||
}</lang> |
|||
<lang 6502asm>LDA myValue |
|||
CMP #3 |
|||
BEQ .doTheThing |
|||
;if not equal, check myOtherValue |
|||
LDA myOtherValue |
|||
CMP #5 |
|||
BNE .skip |
|||
;if we got to here, either "myValue == 3" or "myOtherValue" == 5 evaluated to true. |
|||
.doTheThing: |
|||
STA myResult ;any nonzero value is considered TRUE, so we've stored 5 into myResult. |
|||
.skip:</lang> |
|||
Logical NOT is the easiest of all; just use the opposite branch condition. |
|||
===Using Bit Shifts=== |
|||
Chances are, however, on an 8-bit computer like the 6502, rather than using an entire byte to represent a single variable, you're going to store up to 8 related booleans in a single byte. Variables such as these are often called "bit flags" and is very common for parameters that are passed to/from external hardware, such as joysticks, video display processors, or sound cards. Each bit typically represents a different yet related variable. For example, reading a one-button joystick returns 5 bits, one for the "fire button" and the 4 directions. |
|||
<i>Side note: For joysticks, it's actually more common for 0 to represent pressed and 1 to represent not pressed, but that's out of the scope of this task.</i> |
|||
For testing multiple bits, a simple <code>BNE</code> or <code>BEQ</code> won't cut it, as this doesn't tell you WHICH bits are 0 or 1, only that a 1 exists/doesn't exist somewhere in the byte (which, if you need that info specifically, can be a nice shortcut.) |
|||
In this example, we'll be testing the bottom 2 bits of the 8-bit variable "Flags", and we want to test if both bits are 1. |
|||
<lang 6502asm>LDA flags |
|||
LSR ;test the rightmost bit. |
|||
BCC .skip |
|||
LSR ;test the bit just to the left of the one we tested prior. |
|||
BCC .skip |
|||
;your code for what happens when both of the bottom 2 bits are 1, goes here. |
|||
.skip:</lang> |
|||
===Using BIT=== |
|||
If we're testing the top 2 bits of a byte (usually referred to as bit 7 or 6) then there's a special method we can use. The BIT instruction sets the N flag to bit 7 of the tested byte, and the V flag to bit 6 of the tested byte. |
|||
<lang 6502asm>BIT myBitFlags |
|||
BMI .Bit7Set |
|||
BVS .Bit6Set</lang> |
|||
For this reason, it's a good strategy when designing a bit flags variable to put the bits you'll be testing the most in bit 7 or 6 so that you spend less time checking them. |
|||
=={{header|ACL2}}== |
=={{header|ACL2}}== |