AND - The Bitwise AND Command
The AND command is a bitwise operation that AND’s the value held in the accumulator with the specified value (argument) and resaves the new value into the accumulator.
"AND"ing a bit turns it off. The value you specify in the command is known as a mask. Bits in the argument that are off will become off in the result. Bits that are on in the argument will maintain state in the result.
This makes the command useful for either turning a single bit off or turning off all but one bit.
Sometimes it is useful to store binary states in a single byte and use bitwise operations like this to do targeted edits. One example would be tracking partition states in a ballroom.
You can preform an advanced version of this command using the X register to load from an array.
This command is the opposite of the OR command.
ACC |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
1 |
MASK |
0 |
1 |
1 |
0 |
1 |
1 |
0 |
0 |
RESULT |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
Examples
Example 1
{
LDA #0xA6 //Load the accumulator with a value of 0xA6
AND #0x0F //AND it with 0x0F turning off bits 5-8
STA ~1 //Store the new value of 0x06 into ~1
}
Example 2
Using AND to mask out all but one dipswitch.
{
LDA ^17,0 //Load the first 8 dry contacts
AND #0x04 //AND it with 0x04 and mask out all but input 3
BRZ Switch3Released //If released branch, if not assume pressed
...
}
Example 3
This excerpt stores the state of a partition into a single byte tracking all of them.
{
...
Partition1:
LDA PresetByte //get preset byte
CMP #0 //preset 1
BRZ Part1P1
CMP #3 //preset 4
BRZ Part1P4
Null
Part1P1: //Partition open
LDA RoomState
OR #0x01 //Set the bit to on
STA RoomState
BRA SendMessage
Part1P4: //Partition closed
LDA RoomState
AND #0xFE //Set the bit to off
STA RoomState
BRA SendMessage
...
}
Example 4
ANDing with a value in an array using the X register.
This task snippet would be used to turn a single bit off or on using an array.
OR_Array: Send2(0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80)
AND_Array: Send2(0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F)
{
Start(0x1c,PartitionArea,x,0x6b,x,x,x) //Start on incoming messages from partitions
copy @0,~0,7 //Copy to bytes
LDA ChannelByte //Load the channel number
ADD #5 //Add 5 to offset for the array
SWAP //Swap to X reg
LDA PresetByte //Load the preset number
BNE PartitionClose //Is it P1(Open)? If not assume closed
LDA RoomState //Load the previous partition states
OR OR_Array,x //Or the value in the array offset by the partition number(ch)
STA RoomState //Restore the new state
BRA SendMessage //Go send the new join messages
PartitionClose:
LDA RoomState //Load the previous partition states
AND AND_Array,x //AND the value in the array offset by the partition number(ch)
STA RoomState //Restore the new state
SendMessage: //Go send the new join messages
...
}