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

Keep a flag as 0 and -1, not 1 and 0

Say you want a yes-or-no flag that you set in one place and test in another.

Everyone reaches for 1 and 0. The catch is that TAC can't tell them apart, since neither one is negative, so every test needs a SUB 00 first to turn them into 0 and -1.

cell  contents  notes
 10      030    INP 30  cells 10-17: the flag is 1 or 0; testing it needs a SUB first
 11      130    CLA 30
 12      700    SUB 00
 13      316    TAC 16
 14      531    OUT 31  set: print 1
 15      900    HRS 00
 16      532    OUT 32  clear: print 0
 17      900    HRS 00
 30      000    the flag
 31      001
 32      000

Step through the natural way in Cardigan

So store them that way to begin with: 0 for yes, -1 for no. Now each test is just CLA and TAC. It isn't quite free, because you need a cell holding -1 somewhere to set the flag to no, but that pays for itself from the second test on.

cell  contents  notes
 10      030    INP 30  cells 10-16: the flag is 0 or -1; reading it is CLA and TAC
 11      130    CLA 30
 12      315    TAC 15
 13      500    OUT 00  set: print 1
 14      900    HRS 00
 15      531    OUT 31  clear: print 0
 16      900    HRS 00
 30      000    the flag
 31      000

Step through the CARDIAC way in Cardigan

Both print 1 for yes. The 1-and-0 version takes 11 cells and runs 6 instructions; the 0-and-minus-1 version takes 9 cells and runs 5. That's one of each saved, every single time you test the flag.

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