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 › Subroutines

Return with TAC 99 when the accumulator is negative

Say a routine adds a reading to a running total, but a negative reading means there was nothing to read, and it should be skipped.

Done as in the last tip, the routine copies cell 99 first thing and returns through the copy, and a skipped reading jumps straight to the copy.

cell  contents  notes
 10      040    INP 40  cells 10-18: four readings, each handed to the routine; then print the total
 11      820    JMP 20
 12      040    INP 40
 13      820    JMP 20
 14      040    INP 40
 15      820    JMP 20
 16      040    INP 40
 17      820    JMP 20
 18      541    OUT 41
 19      900    HRS 00
 20      199    CLA 99  cells 20-26: copy the return address first, then add the reading unless it is negative
 21      626    STO 26
 22      140    CLA 40
 23      326    TAC 26  negative: skip it and go back
 24      241    ADD 41
 25      641    STO 41
 26      000    the return address, copied from cell 99
 40      000    the reading
 41      000    the total

Step through the natural way in Cardigan

TAC doesn't write cell 99; only JMP does. So when the accumulator is negative, TAC 99 goes straight back to the caller, and here the accumulator is negative exactly when the reading should be skipped. The test that skips it is the return. The copy moves down to the one path that still needs it, and a skipped reading never pays for it.

cell  contents  notes
 10      040    INP 40  cells 10-18: four readings, each handed to the routine; then print the total
 11      820    JMP 20
 12      040    INP 40
 13      820    JMP 20
 14      040    INP 40
 15      820    JMP 20
 16      040    INP 40
 17      820    JMP 20
 18      541    OUT 41
 19      900    HRS 00
 20      140    CLA 40  cells 20-21: a negative reading returns at once; TAC leaves cell 99 alone
 21      399    TAC 99
 22      241    ADD 41  cells 22-26: otherwise add it, and only now copy the return address
 23      641    STO 41
 24      199    CLA 99
 25      626    STO 26
 26      000    the return address, copied from cell 99
 40      000    the reading
 41      000    the total

Step through the CARDIAC way in Cardigan

Both add up 5 and 7 and print 12, and both take 19 cells. Copying first runs 34 instructions, and returning with TAC runs 30, two fewer for every reading skipped. A routine that ends negative on every path never has to copy cell 99 at all, which is why some programs go as far as storing their data as negative numbers. Cell 99 as the return address is the subroutine technique Brian L. Stuart's CARDIAC page shows, and TAC 99 is a small variation on it.

The Intel 8080 has this as one instruction. RM, return if minus, returns when the sign flag is set and otherwise carries on, and the Z80 kept it as RET M.

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