When you need a counter that counts modulo N, this can be implemented using an LSUB instruction. An LSUB subtracts two numbers and a “borrow”, and produces the result and a borrow. Supply as input parameters:
The LSUB instruction produces
This implements both the comparison and the computation of the wrapped counter, provided that the counter is between N and 2N-1.
An example program:
ldc r1, 0x9842 // Zero borrow, any even number will do
ldc r5, 10 // Value of N
ldc r3, 0 // The counter
...
add r3, r3, 3
lsub r0, r2, r3, r5, r1
bt r0, noWrap
or r3, r2, r2 // Copy r2 to r3; can be avoided by
noWrap: // judicious register renaming
...