SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

CARDIAC › Tips and tricks › Subroutines

Subroutines and recursion

Say you need the same small job, doubling a number and printing it, in two places in your program.

CARDIAC has no call instruction, but JMP does half the work. It writes 8 and the address of the cell after it into cell 99, so JMP 20 in cell 11 leaves 812 there, which is JMP 12, the return address. The trouble is keeping it, because the next JMP overwrites cell 99, and you can't simply JMP 99 to return, because that JMP would write its own return address into cell 99 before it got there.

ARM and MIPS still call this way. ARM's BL leaves the return address in a register called the link register, LR, and MIPS's jal leaves it in $ra, with no stack involved. A leaf function, one that calls nothing else, returns straight through it, and a function that makes calls of its own saves it first, which is the choice this tip is about.

The stack is expensive

Ask most programmers where a return address belongs, and they'll say on a stack. So the routine pushes cell 99 as soon as it starts, and pops it into its last cell just before it runs into it. The stack is Brian L. Stuart's, from his CARDIAC page: he keeps the top of the stack as an address, and adds it to STO 00 to push or to CLA 00 to pop. Here cell 50 keeps the STO itself, so a push only has to copy it and add 1, and subtracting 500 turns it into the CLA that reads the top back.

cell  contents  notes
 10      040    INP 40  cells 10-14: two calls; each JMP leaves its return address in cell 99
 11      820    JMP 20
 12      040    INP 40
 13      820    JMP 20
 14      900    HRS 00
 20      150    CLA 50  cells 20-25: push cell 99; cell 50 is the STO to the stack's top
 21      625    STO 25
 22      200    ADD 00
 23      650    STO 50
 24      199    CLA 99
 25      600    STO 00  STO to the top, written by cell 21
 26      140    CLA 40  cells 26-29: double x and print it
 27      240    ADD 40
 28      641    STO 41
 29      541    OUT 41
 30      150    CLA 50  cells 30-37: pop the return address into cell 37 and run it; CLA is STO minus 500
 31      700    SUB 00
 32      650    STO 50
 33      751    SUB 51
 34      635    STO 35
 35      100    CLA 00  the return address, read from the stack
 36      637    STO 37
 37      000    the return address, popped into place
 40      000    x
 41      000
 50      652    STO 52
 51      500
 52      000    the stack

Step through the natural way in Cardigan

It works, but look at the price on a machine with a hundred cells. Pushing runs 6 instructions and popping 7, so every call costs 13 instructions before the routine does anything. The routine spends 14 cells on pushing, popping and its return address, and the pointer, the 500 and the stack take 3 more, one stack cell for every call that can be under way at once.

A more economical way

A routine that never calls itself only ever has one return address to keep, and it doesn't need a stack for that. The stackless way is to copy cell 99 into the routine's own last cell as soon as it starts, and let the routine run into it at the end. The copy comes first because any JMP inside the routine, in a loop or a call to another routine, overwrites cell 99, and by then the copy is safe. Routines can call each other this way, since each one keeps its own return address in its own cell. It still costs something: three cells in every routine, two for CLA 99 and STO and one to hold the return address, and two instructions every call. If the routine's argument arrives in the accumulator, it has to be stored before CLA 99, which can cost one more. Brian Stuart's page builds its routines this way too, before it gets to the stack.

cell  contents  notes
 10      040    INP 40  cells 10-14: two calls; each JMP leaves its return address in cell 99
 11      820    JMP 20
 12      040    INP 40
 13      820    JMP 20
 14      900    HRS 00
 20      199    CLA 99  cells 20-26: copy cell 99 into the routine's last cell, then double x and print it
 21      626    STO 26
 22      140    CLA 40
 23      240    ADD 40
 24      641    STO 41
 25      541    OUT 41
 26      000    the return address, copied from cell 99
 40      000    x
 41      000

Step through the CARDIAC way in Cardigan

With the stack

001122334455667788990000101020304050607080910INP11JMP12INP13JMP14HRS151617181920CLA21STO22ADD23STO24CLA25STO26CLA27ADD28STO29OUT30CLA31SUB32STO33SUB34STO35CLA36STO37back383940x412x424344454647484950top5150052153545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798998xx

Main programThe routine's own workLinkage: push, pop and the return addressThe stack, its top and 500VariablesCells 00 and 99Free

17 of its 28 cells are linkage.

Stackless

001122334455667788990000101020304050607080910INP11JMP12INP13JMP14HRS151617181920CLA21STO22CLA23ADD24STO25OUT26back2728293031323334353637383940x412x424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798998xx

Main programThe routine's own workLinkage: CLA 99, STO and the return addressVariablesCells 00 and 99Free

3 of its 14 cells are linkage.

Both print 6 and 8. The stack takes 28 cells and runs 41 instructions; stackless takes 14 cells and runs 19. That's 17 cells of linkage against 3, and 13 instructions a call against 2.

The PDP-8 built the stackless way into its call instruction. JMS stores the return address in the first word of the routine and starts it at the second, and the routine returns with an indirect jump through that word, so its routines couldn't call themselves without help either.

What stackless gives up is recursion. A routine that calls itself overwrites its own copy, and the outer call loses its return address, and two routines that call each other do the same to each other. The exception is a call that's the last thing the routine does. There's nothing left to come back for, so the call can be a jump back to the routine's start, and the return address it copied first is still in its cell. Compilers call this tail-call elimination, and Scheme's standard requires it, so a loop can be written as a function that calls itself last without ever running out of stack.

So the question to ask of every routine is whether it can be called again before it has returned, other than as the last thing it does. If it can't, stackless does the job in a fraction of the cells and instructions. If it can, nothing short of a stack will do.

IfReach for
A routine calls itself anywhere but at the endA stack
Two routines call each other anywhere but at their endsA stack
A routine calls itself as the last thing it doesNo stack: jump back to its start
Two routines call each other only at their endsNo stack: merge them into one routine, and jump
Routines call other routines, but never back up the chainStackless: copy cell 99
A routine runs inside a loop and every instruction countsStackless: 2 instructions a call, not 13
Cells are tightStackless: 3 cells a routine, not 14 plus the stack
A routine only returns when the accumulator is negativeStackless with no copy at all: return with TAC 99, the next tip

The long version, with maps of memory, recursion and calls that come last: subroutines. rust-cardiac makes this choice for you. Functions are stackless, with fixed cells for their locals and their return address, and only a function that calls itself somewhere other than at its end gets a stack of its own. A call of itself as the last thing it does becomes a loop. Recursion through two or more functions is refused unless every call is at the end, and the compiler says to write it as a loop.

From the tips in cardiac-code on GitHub. All the tips.