CARDIAC › Tips and tricks › Instructions are numbers
SFT 22 reads the address out of an instruction
Say you want to walk through a table in cells 40 to 43 and print each entry.
CARDIAC has no indirect addressing, so walking a table means rewriting the address in one of your own instructions. The usual way also keeps a counter, builds the instruction from it each time round, and checks the counter against the end.
cell contents notes
10 130 CLA 30 cells 10-13: build OUT 40 + i from the counter
11 232 ADD 32
12 613 STO 13
13 500 OUT 00 OUT 40 + i, written by the cell before
14 130 CLA 30 cells 14-19: count, and stop at 4
15 200 ADD 00
16 630 STO 30
17 731 SUB 31
18 310 TAC 10
19 900 HRS 00
30 000 the counter, kept in step with the instruction
31 004
32 540 OUT 40
40 007
41 003
42 009
43 001
Step through the natural way in Cardigan
But the instruction already knows where it's pointing, so let it be the pointer. Add 1 to the instruction itself, and when you want to know where it's got to, load it and do SFT 22, which drops the opcode and leaves the address. OUT 41 is 541; shift 0541 two places left and two right and you've got 0041. There's no counter to keep in step.
cell contents notes
10 540 OUT 40 cells 10-17: print cells 40-43; cell 10 is the pointer
11 110 CLA 10
12 200 ADD 00
13 610 STO 10
14 422 SFT 22 drop the opcode: just the address
15 730 SUB 30
16 310 TAC 10
17 900 HRS 00
30 044 one past the end
40 007
41 003
42 009
43 001
Step through the CARDIAC way in Cardigan
Both print 7, 3, 9 and 1. Keeping a counter takes 17 cells and runs 37 instructions; using the instruction as the pointer takes 13 cells and runs 29.
From the tips in
cardiac-code on GitHub. All the tips.