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

Keep a number too big for a cell in two

Say you're adding up cards, and the total can go past 999.

The usual loop keeps the total in one cell. That's fine until the total passes 999. The accumulator holds four digits, but STO keeps only the last three, so 500, 600 and 700 add up to 800.

cell  contents  notes
 10      030    INP 30  cells 10-17: add up cards until a negative one, in one cell
 11      130    CLA 30
 12      316    TAC 16
 13      231    ADD 31
 14      631    STO 31  STO keeps only the last three digits
 15      810    JMP 10
 16      531    OUT 31
 17      900    HRS 00
 30      000
 31      000    the total

Step through the natural way in Cardigan

So keep the total in two cells, one for the last three digits and one for the thousands. The accumulator can hold the last three digits plus a card, which is at most 1998, and two instructions split it. STO keeps the last three digits, and SFT 03 drops them and leaves the thousands, 0 or 1, to carry into the other cell. Print the thousands and then the rest, so 1 and 800 means 1,800, and 2 and 50 means 2,050. Two cells go up to 999,999.

cell  contents  notes
 10      030    INP 30  cells 10-20: add up cards until a negative one, in two cells
 11      130    CLA 30
 12      319    TAC 19
 13      231    ADD 31  up to 1998, which the accumulator can hold
 14      631    STO 31  STO keeps the last three digits
 15      403    SFT 03  and SFT 03 leaves the thousands: the carry
 16      232    ADD 32
 17      632    STO 32
 18      810    JMP 10
 19      532    OUT 32  cells 19-21: print the thousands, then the rest
 20      531    OUT 31
 21      900    HRS 00
 30      000
 31      000    the total's last three digits
 32      000    the total's thousands

Step through the CARDIAC way in Cardigan

One cell

001122334455667788990000101020304050607080910INP11CLA12TAC13ADD14STO15JMP16OUT17HRS18192021222324252627282930card31tot3233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

CodeThe totalVariables and constantsCell 00, always 001Free

10 cells. The total gets one, so it can't pass 999.

Two cells

001122334455667788990000101020304050607080910INP11CLA12TAC13ADD14STO15SFT16ADD17STO18JMP19OUT20OUT21HRS222324252627282930card31low321000s33343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

CodeThe total: its last three digits and its thousandsVariables and constantsCell 00, always 001Free

15 cells. The total gets two, so it goes up to 999,999.

The one-cell version prints 800, which is wrong, in 10 cells and 23 instructions. The two-cell version prints 1 and 800, which is right, in 15 cells and 33 instructions.

Binary machines do this with a carry flag, and it's called multi-precision arithmetic. On the 8-bit 6502, adding two 16-bit numbers is one ADC on the low bytes and another on the high bytes, and the second ADC adds in the carry the first one left behind. Libraries like GMP do the same on 64-bit words to work with numbers thousands of digits long.

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