if the accumulator is 4, the subtraction will "succeed" (i.e. there will be no underflow) and the carry will be set.
No underflow? So water will not flow under the subtraction and that sets the carry? (trying to get you to teach me more about "underflow" - im interested)
tried to make this easier to read... for me at least... just added colors
Tokumaru covered CMP, but here's a little more explanation on what AND and the other bitwise operators (hey, why not?) do, and why what Dwedit suggested works:
AND (like ORA and EOR) at a basic level gives an output of 1 bit from 2 input bits. Order doesn't matter in any of them, so there are three cases.
1. Both bits are
0.
2. Both bits are
13. 1 bit is
1, and the other is
0.
AND gives an output of
1 if both input bits were
1. (if input bit 1 is
1 [true] AND input bit 2 is
1 [true], the result is
true.) See how it got its name?

So, AND will only return
1 in case
2, otherwise it gives
0.
ORA gives an output of
1 if either input bit is one. (If input bit 1 is
1 [true] OR input bit 2 is
1[true], the result is
true.) See how it got its name?
So ORA will return
1 in every case except case
1. It will return
0 in case
1.
EOR (Exclusive OR) gives an output of
1 if EXACTLY one of the input bits is
1. (If input 1 and ONLY input is
1 [true], the result is
true. If input 2 and ONLY input 2 is
1[true], the result is
true)
So EOR will return
1 only in case
3, otherwise it gives
0.
Now, at a basic level they work one bit at a time. The instructions work a byte at a time with corresponding bits in a byte.
Code:
#%00000000
^^^^^^^^
||||||||
BIT#: 76543210
#%00000000
They check and return the result of bits 7 in each byte, and while that check is happening what's in bits 6-0 doesn't matter. Then it does bits 6, and while that check is happening what's in bits 7 and 5-0 doesn't matter. Etc.
AND is useful for checking the status of a specific bit, because by definition of AND, a bit with a
0 in either byte will return
0.
So if you AND with a number that has 7 bits as
0, the result of those seve bits will be
0. That means the bit you left
1 in the AND determines whether the accumulator is
0, or
non-0.
Another use for AND is clearing a bit (setting it to
0). #%01111111 will clear the leftmost bit without changing the others. The
0 is guaranteed to clear the high bit because the 0 means both bits CAN'T be
1, and the 7 ones guarantee the other bits won't change.
EOR is useful for toggling a bit (
0->
1 or
1->
0). This is because a 0 in an EOR is guaranteed to not change the bit it is being EOR'd with, while a 1 in an EOR is guaranteed TO change the bit it is being EOR'd with.
ORA is useful for setting a bit to 1. This is a because a 1 is guaranteed to give a result of
1, while a 0 will never change the bit you are ORA'ing with.
The logic surrounding your cases is interesting.
: ) There are shortcuts or cycles that can be saved, right? I have tried to think of one, but I can't think of anything...