Lorne,
You should think of the IF statement as IF..ENDIF. The use of "THEN" is optional and you should just forget about it.
Your program is missing an ENDIF and could not possibly compile.
This is probably what you had in mind:
CLRIO CBP 'First Clear CBP
IF DM[535] > 0
FOR N = 0 TO 7
IF TESTBIT (DM[541], N)
IF TESTBIT(DM[537],N) =0
SETIO CBP
ENDIF
ENDIF
NEXT
ENDIF
I would suggest for "readability" that you indent your code so that the IF lines up with the ENDIF, FOR lines up with NEXT and the statements between then are indented. This is not required by TBASIC, but it helps to make it easier to see what is going on.
You could simplify your code a bit by using the logical "AND" to test for a "1" in DM[541] and a "0" in DM[537]:
CLRIO CBP 'First Clear CBP
IF DM[535] > 0
FOR N = 0 TO 7
IF (TESTBIT (DM[541], N) = 1) AND (TESTBIT(DM[537],N) = 0)
SETIO CBP
ENDIF
NEXT
ENDIF
You could probably simplify your code even more by eliminating the FOR/NEXT loop. You look at all of the bit patterns in parallel by using the bit-wise and operator, "&". The "~" is the bit-wise NOT operator. I know that this won't make much sense, yet. But, someday it might:
CLRIO CBP 'First Clear CBP (assume that the tests will fail)
IF DM[535] > 0
' Test for a "1" in each bit position in DM[541] against a "0" in
' the corresponding bit position in DM[537].
'
' TBASIC does most of its operations with 32-bit math and it will
' sign-extend the contents of the 16-bit DM[] registers to 32-bit variables
' and perform the math as if the variables are 32-bits wide.
'
' But you are only interested in the least significant 8-bits of the DM[]
' registers. The fix is to perform a bit-wise and operation and preserve
' the least significant 8-bits and force to 0 all of the rest of the bits that
' either you don't care about or never existed (sign-extension).
'
IF ((DM[541] & (~DM[537])) & &hff)
SETIO CBP
ENDIF
ENDIF
Best regards
Gary D*ckinson