You can test any bit in any address simply by loading a register and choosing the right instruction code. This facility is fairly easy to understand and can be used even in very short machine code (MC) subroutines.
For example, if you have a block of data with both positive and negative values less than 128, you can use the instruction BIT 7,A after loading A with a value. If A is positive, the Z Flag will be set to 1; if A is negative, the Z flag will be reset to 0. Then a Test-and-Branch (T&B) can give the value to the correct area for processing.
How does this instruction work? The 7th bit in A, either 0 or 1, “+” or “-“, is “inverted” and put into the Z bit of the Flags register. If your T&B is JR Z and the 7th bit of A is 0, then Z will be 0 and control will not jump. Remember that a Flag is “true” if equal to 1, or “set”.
Another example: suppose you have a block of data with odd and even values less than 256, and you want the addresses of the odd values. One solution is to set up a loop with HL. (Odd values will have a 1 in bit 0; even values will have a 0 in bit 0.)
The possibilities are endless, so here are some general rules:
1. Select the proper instruction. If the address is important, use BIT n,(HL). If the value is important, use BIT n,register.
2. First, load the proper register (or HL pair) with the value (or address), then test the bit.
3. Follow the bit test with a Test-and-Branch instruction with two possibilities:
A: Bit test fails–continue search loop (Ex. JR Z ;Z=0)
B: Bit test succeeds–Branch to processing area (Z=1) (or, A: JR NZ for branch and B: Continue search loop.)
4. If your data block is used more than once per USR-call, then add a “trigger” value into the last data address for a “value” Test-and-Branch to an area for resetting the address-pointer.
Next month: JUMPING AROUND