CARDIAC › Tips and tricks › Comparisons
Park the number you subtract; keep the other in the accumulator
Say you've worked out two values, a = x + y and b = 10z, and want to know whether a is below b.
The tidy way is to work out both, store both, then load b back and subtract a.
cell contents notes
10 040 INP 40 cells 10-15: a = x + y, stored
11 041 INP 41
12 042 INP 42
13 140 CLA 40
14 241 ADD 41
15 643 STO 43
16 142 CLA 42 cells 16-18: b = 10z, stored
17 410 SFT 10
18 644 STO 44
19 144 CLA 44 cells 19-26: load b again and test b - a - 1
20 743 SUB 43
21 700 SUB 00
22 325 TAC 25
23 500 OUT 00 a < b: print 1
24 900 HRS 00
25 545 OUT 45
26 900 HRS 00
40 000
41 000
42 000
43 000
44 000
45 000
Step through the natural way in Cardigan
Here's the thing: CARDIAC can only subtract a cell, so only the number you subtract has to be stored. Store a, work out b, and while b is still sitting in the accumulator, subtract a straight away. For "a < b" you test b - a - 1 against zero, and the 1 comes free from cell 00.
cell contents notes
10 040 INP 40 cells 10-15: a = x + y, stored, because it is the one subtracted
11 041 INP 41
12 042 INP 42
13 140 CLA 40
14 241 ADD 41
15 643 STO 43
16 142 CLA 42 cells 16-24: b = 10z stays in the accumulator; test b - a - 1
17 410 SFT 10
18 743 SUB 43
19 700 SUB 00
20 323 TAC 23
21 500 OUT 00 a < b: print 1
22 900 HRS 00
23 544 OUT 44
24 900 HRS 00
40 000
41 000
42 000
43 000
44 000
Step through the CARDIAC way in Cardigan
Both compare 19 with 20 and print 1. Storing both takes 23 cells and runs 15 instructions; parking only a takes 20 cells and runs 13.
From the tips in
cardiac-code on GitHub. All the tips.