"'Contrariwise,' continued Tweedledee, 'if it was so, it might be, and if
it were so, it would be: but as it isn't it ain't. That's logic.'" - Lewis Carroll,
'Through the Looking-Glass'
The AND Instruction
If you've done some electronics, or possibly some programming in c/c++, you'll know
roughly what the AND instruction does. It performs a binary AND operation on the a
register and one other register or value and stores the result in a. The general form is
and [value_to_AND_with]. But what is a 'binary AND operation?
It compares, bit by bit, the binary values of the a register and the other argument. If
both bits are 1 then the same bit in the result is a 1, otherwise it is a 0. For
example:
Example 1
| First Value |
%10010110 |
| Second Value |
%10101100 |
| Result |
%10000100 |
Example 2
| First Value |
%11110001 |
| Second Value |
%10100101 |
| Result |
%10100001 |
Example 3
| First Value |
%10101010 |
| Second Value |
%01010101 |
| Result |
%00000000 |
The OR Instruction
The OR instruction works in a similar way to the AND instruction, but rather than
giving a 1 only if both bits are 1 it will give out a 1 if either bit is 1 (or if both
are 1). The general form is or a,[value_to_AND_with]. For
example:
Example 1
| First Value |
%10010110 |
| Second Value |
%10101100 |
| Result |
%10110110 |
Example 2
| First Value |
%11110001 |
| Second Value |
%10100101 |
| Result |
%11110101 |
Example 3
| First Value |
%10101010 |
| Second Value |
%01010101 |
| Result |
%11111111 |
The XOR Instruction
The XOR instruction (also called 'Exclusive OR') is like the OR instruction, but the
result is only a 1 if either, but not both of the bits are 1. The general form
is xor [value_to_XOR_with]. For example:
Example 1
| First Value |
%10010110 |
| Second Value |
%10101100 |
| Result |
%00111010 |
Example 2
| First Value |
%11110001 |
| Second Value |
%10100101 |
| Result |
%01010100 |
Example 3
| First Value |
%10101010 |
| Second Value |
%01010101 |
| Result |
%11111111 |
The CPL Instruction
The CPL instruction is another logical instruction - it is sometimes called 'NOT' or
'One's Complement'. It takes the general form cpl without
any parameters - it is always performed on the a register. It goes through each of the
bits in a, converting 1s to 0s and 0s to 1s. For example:
Example 1
| Value |
%10110010 |
| Result |
%01001101 |
Example 2
| Value |
%10101010 |
| Result |
%01010101 |
Examples
OK, now for some examples. The comments should explain what happens (in case you
hadn't figured it out yet, anything after a semicolon (;) is ignored by the compiler
as it is a comment).
ld a,%11110000 ; a contains %11110000
ld b,%10101010 ; b contains %10101010
or b ; a contains %11111010 (a or b), b remains unchanged
ld a,$aa ; a contains $aa = %10101010
and %11001100 ; a contains %10001000 (a and %11001100)
ld a,%10011010 ; a contains %10011010
ld e,%10101010 ; e contains %10101010
xor e ; a contains %00110000 (a xor e)
ld a,%10101010 ; a contains %10101010
cpl ; a contains %01010101
One other interesting thing to note - xor a will always set
the a register to zero (think about it). In fact it is often used to do that as it
requires one byte less than ld a,0 and takes slightly less
time to run.