CARDIAC › Tips and tricks › Instructions are numbers
Add the case number to a JMP
Say you're writing a little calculator. Each card is a command from 0 to 4: add 1, take 1 away, double, times ten, or print the total.
You'd probably find out which command it is one question at a time: take 1 away and test, take 1 away and test. The commands at the front are cheap, but each one further down pays for all the questions before it.
cell contents notes
10 050 INP 50 cells 10-12: read commands until a negative one
11 150 CLA 50
12 339 TAC 39
13 700 SUB 00 cells 13-20: which command? one SUB and one TAC for each
14 323 TAC 23
15 700 SUB 00
16 327 TAC 27
17 700 SUB 00
18 331 TAC 31
19 700 SUB 00
20 335 TAC 35
21 551 OUT 51 cells 21-22: command 4: print the total
22 810 JMP 10
23 151 CLA 51 cells 23-26: command 0: add 1
24 200 ADD 00
25 651 STO 51
26 810 JMP 10
27 151 CLA 51 cells 27-30: command 1: take 1 away
28 700 SUB 00
29 651 STO 51
30 810 JMP 10
31 151 CLA 51 cells 31-34: command 2: double
32 251 ADD 51
33 651 STO 51
34 810 JMP 10
35 151 CLA 51 cells 35-38: command 3: times ten
36 410 SFT 10
37 651 STO 51
38 810 JMP 10
39 900 HRS 00
50 000 the command
51 000 the total
Step through the natural way in Cardigan
Instead, jump straight to the command's code. Lay the commands out ten cells apart, so command k starts at cell 20 + 10k. SFT 10 makes 10k, adding JMP 20 makes the jump, and storing it in the next cell runs it. It's the same addition that reads an entry in "Lay a table out in decimal", aimed at code instead of data, and the gaps between the commands hold the variables, just as they do there. When the cases don't fit ten cells apart, add k to a JMP aimed at a table of JMPs, one for each case. Brian L. Stuart's CARDIAC page shows the same addition for reaching element i of an array, with CLA and STO.
cell contents notes
10 025 INP 25 cells 10-12: read commands until a negative one
11 125 CLA 25
12 317 TAC 17
13 410 SFT 10 cells 13-16: command k starts at cell 20 + 10k, so add 10k to JMP 20 and run it
14 227 ADD 27
15 616 STO 16
16 800 JMP 00 JMP 20 + 10k, written by the cell before
17 900 HRS 00
20 126 CLA 26 cells 20-23: command 0: add 1
21 200 ADD 00
22 626 STO 26
23 810 JMP 10
25 000 cells 25-27, in the gap: the command, the total, and JMP 20
26 000
27 820 JMP 20
30 126 CLA 26 cells 30-33: command 1: take 1 away
31 700 SUB 00
32 626 STO 26
33 810 JMP 10
40 126 CLA 26 cells 40-43: command 2: double
41 226 ADD 26
42 626 STO 26
43 810 JMP 10
50 126 CLA 26 cells 50-53: command 3: times ten
51 410 SFT 10
52 626 STO 26
53 810 JMP 10
60 526 OUT 26 cells 60-61: command 4: print the total
61 810 JMP 10
Step through the CARDIAC way in Cardigan
Both print 39. Asking takes 32 cells and runs 74 instructions; jumping takes 29 cells and runs 68. With the jump, every command costs 11 instructions, or 9 for print, wherever it is in the list. With the questions it's anywhere from 9 to 15, and each command you add makes the ones after it dearer. One caution: the jump trusts the card. A command of 5 would land on cell 70, where there's nothing, so if the cards might be wrong, check the range first.
This is how compilers lower a dense switch statement. GCC and LLVM build a table of addresses and index it with the case value, after a range check that sends anything outside the cases to the default, which is the caution above. When the cases are spread out, they use a tree of comparisons instead, much like the first version.
From the tips in
cardiac-code on GitHub. All the tips.