CARDIAC › Programming basics
CARDIAC programming basics
What CARDIAC is made of, and how a program gets into it.
The machine in thirty seconds
- 100 memory cells, 00 to 99, each holding a signed three-digit number, -999 to 999.
- An accumulator of four digits and a sign.
- A program counter, the PC: the cell whose instruction runs next. The manual calls it "the bug".
- Ten instructions. The first digit of a word is the opcode and the other two are an address.
- The instruction clock, from Cardigan's help pane, puts the ten opcodes round a dial, joined across the middle.
Opcodes five apart do opposite jobs: INP and OUT, CLA and STO, ADD and SUB, TAC and JMP (a jump if, a jump
always), SFT and HRS.
| Code | Mnemonic | What it does |
| 0 | INP | reads the next input card into the cell |
| 1 | CLA | clears the accumulator and adds the cell |
| 2 | ADD | adds the cell to the accumulator |
| 3 | TAC | jumps to the cell if the accumulator is negative |
| 4 | SFT | SFT xy shifts the accumulator x places left, then y right |
| 5 | OUT | outputs the value in the cell |
| 6 | STO | stores the accumulator's last three digits in the cell |
| 7 | SUB | subtracts the cell from the accumulator |
| 8 | JMP | jumps to the cell, leaving the way back (8xx) in cell 99 |
| 9 | HRS | halts, with the PC left on the cell |
Cell 00 always holds 001. That is what lets a deck of cards load itself.
Understanding the loader
CARDIAC has one way to get anything into memory: the card reader. The only instruction that reads a card is
INP, and an INP needs a program to run it, so a program can't load itself without a
program already there. Cell 00 breaks the circle. It is wired to hold 001, which as an instruction is
INP 01: read a card into cell 01. Start the machine at 00, and the deck is arranged so that what it
reads becomes a loader.
Here is a whole deck, for a program of three cells that prints 42, and what each card does:
| Cards | What happens |
002 | Cell 00, INP 01, reads it into cell 01, which now holds INP 02. |
800 | Cell 01, INP 02, reads it into cell 02, which now holds JMP 00. The
loader is built: three instructions in cells 00 to 02. |
010 512 | INP 01 reads the address card, 010, into cell 01, which
becomes INP 10. That reads the next card, 512, into cell 10. JMP 00 goes back for the
next pair. |
011 900 | The same again: 900 goes into cell 11. |
012 042 | And 042 goes into cell 12. |
002 810 | INP 01 reads 002, so cell 01 is INP 02
again, and that reads 810 into cell 02, which is now JMP 10. The loader hands over to the program. |
The program is OUT 12, HRS 00 and the number 42, and it prints 42. Loading it took 15
instructions: three to build the loader, three for each cell (INP 01, INP into the cell,
JMP 00), and three to hand over. Every cell costs two cards, an address and its contents.
Two cells are off limits to a deck. Cells 01 and 02 are the loader's own while it runs, and every
JMP writes the way back into cell 99, so the loader's JMP 00 overwrites whatever a deck
put there.
The deck here is laid out as Brian Stuart's
CARDIAC page describes, ending with the pair
002 8xx so that the program starts by itself. The manual's own loading program ends instead with a blank card,
which stops the machine, and the operator starts the program.
Watch the loader in Cardigan
This opens the deck with Step through loading ticked. Step with F11 and watch
each card go into its cell.
Why Cardigan doesn't show the loader
A listing (.cq) has no cards, so there is nothing for a loader to read. Cardigan writes it
straight into memory, the short cut the manual itself takes when it writes a program into the cells by hand. The
examples and the tips open this way, which is why their programs are already in place when they open.
A deck (.cqb) goes through the loader, just as it would on CARDIAC. Cardigan runs the loader at
full speed and stops on the program's first instruction, because three instructions a cell are slow to watch and
the memory ends up the same. Tick Step through loading, under Run and Debug, to watch it instead.
The same box works for a listing. With it ticked, Cardigan puts the listing on cards first, exactly as
Export deck does, and the loader reads them in. The listing's lines say not loaded yet until their
cards arrive.
DATA cells: loading a table with the program
In a Cardigan listing, a cell that holds a number instead of an instruction is DATA. Here is the
same program as a listing:
10 512 OUT 12
11 900 HRS 00
12 042 DATA 42
Cardigan writes the DATA cell into memory with the rest. Export deck turns every cell of a
listing, instructions and DATA alike, into an address card and a contents card, between the
loader's cards and the pair that starts the program. That is the deck above, card for card.
So a table written as DATA cells arrives on the program's own deck. The loader puts each entry in
its cell, and the program never spends an instruction filling it. That is the tip
load tables with the program, not with INP.
Open the listing in Cardigan
Beyond the basics