SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

CARDIAC › Tips and tricks › Comparisons

Ask the cheap question first

Say you want to check that a value is a single digit, 0 to 9.

The careful way is to check both ends: is it at least 0, and is it below 10?

cell  contents  notes
 10      030    INP 30  cells 10-18: a digit is at least 0 and below 10: two tests
 11      130    CLA 30
 12      315    TAC 15
 13      731    SUB 31
 14      317    TAC 17
 15      532    OUT 32
 16      900    HRS 00
 17      500    OUT 00  a single digit: print 1
 18      900    HRS 00
 30      000    a count of cards
 31      010
 32      000

Step through the natural way in Cardigan

But look at where the value came from. If it's counting cards, it can't be negative, so the first question has already been answered for you and only "below 10?" is left. It's worth getting into the habit of ordering your tests so that what you already know answers the later ones.

cell  contents  notes
 10      030    INP 30  cells 10-17: x counts cards, so it is never negative; one test is enough
 11      130    CLA 30
 12      731    SUB 31
 13      316    TAC 16
 14      532    OUT 32
 15      900    HRS 00
 16      500    OUT 00  a single digit: print 1
 17      900    HRS 00
 30      000    a count of cards
 31      010
 32      000

Step through the CARDIAC way in Cardigan

Both print 1 for 4. Asking both takes 12 cells and runs 7 instructions; asking one takes 11 cells and runs 6.

From the tips in cardiac-code on GitHub. All the tips.