CARDIAC › Tips and tricks › Instructions are numbers
Lay a table out in decimal: the address is the digits
Say you've got a table with three columns, and you want the entry at row P, column R.
You'd probably pack the rows tight, so the entry is at 3P + R. Times 3 is two additions, and a wider table needs more.
cell contents notes
10 046 INP 46 cells 10-21: the entry for row P, column R is in cell 40 + 3P + R
11 047 INP 47
12 146 CLA 46
13 246 ADD 46 3P: two adds, and one more for every column a row has
14 246 ADD 46
15 247 ADD 47
16 249 ADD 49 add CLA 40
17 618 STO 18
18 100 CLA 00 CLA 40 + 3P + R, written by the cell before
19 648 STO 48
20 548 OUT 48
21 900 HRS 00
40 101 cells 40-45: rows 0 and 1, packed tight
41 102
42 103
43 201
44 202
45 203
46 000
47 000
48 000
49 140 CLA 40
Step through the natural way in Cardigan
Instead, start every row ten cells apart, so the entry is at 10P + R. Times 10 is one shift, however wide your rows are. And the cells left over between rows aren't wasted: that's where your variables go. Add the CLA instruction to the address, run it, and you've done the lookup. The manual's Nim programs keep their tables exactly this way.
cell contents notes
10 043 INP 43 cells 10-20: the entry for row P, column R is in cell 40 + 10P + R
11 044 INP 44
12 143 CLA 43
13 410 SFT 10 10P: one shift, however wide the rows
14 244 ADD 44
15 246 ADD 46 add CLA 40
16 617 STO 17
17 100 CLA 00 CLA 40 + 10P + R, written by the cell before
18 645 STO 45
19 545 OUT 45
20 900 HRS 00
40 101 cells 40-52: rows 0 and 1, ten cells apart
41 102
42 103
43 000 cells 43-49 between the rows hold the variables: P
44 000 R
45 000 the entry
46 140 CLA 40
50 201
51 202
52 203
Step through the CARDIAC way in Cardigan
Both look up row 1, column 1 and print 202. The packed table takes 22 cells and runs 12 instructions; the decimal one takes 21 cells and runs 11, and the gap grows with every column you add.
From the tips in
cardiac-code on GitHub. All the tips.