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

CARDIAC can jump, but it can't return. This page is about what you do with that: where a routine keeps its return address, what that costs on a machine with a hundred cells, and how CARDIAC came to work this way.

JMP and TAC

CARDIAC has two jumps. JMP always jumps, and before it does, 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, a jump to the return address. That one side effect is all the help a subroutine gets: a routine whose last instruction is in cell 99 returns just by running into it.

TAC jumps only when the accumulator is negative, and it doesn't touch cell 99. Every loop and every test is built from it, and it has a second use: a routine that ends with the accumulator negative can return with TAC 99 and never has to save cell 99 at all, as in return with TAC 99 when the accumulator is negative. The Intel 8080 has a relative of it, RM, return if minus, which returns when the sign flag is set and otherwise carries on, and the Z80 kept it as RET M.

One return address, and no stack

Cell 99 holds one return address, and the next JMP overwrites it, which means the routine's own loops as well as its calls. Nor can a routine end with JMP 99, because that JMP writes its own return address into cell 99 before it gets there, and lands on a jump straight back to the cell after itself.

And there's no stack to keep it on. CARDIAC has no stack pointer, no push or pop, and no call that saves the return address for you. Any routine that jumps anywhere has to keep its return address somewhere else, and on a machine with a hundred cells, where it keeps it decides how much room the program has left.

Later machines kept the one fixed place and dropped the trouble. ARM's BL and MIPS's jal also leave the return address in one fixed place, a register called the link register, LR on ARM and $ra on MIPS, rather than on a stack. A leaf function, one that calls nothing else, returns straight through it, and only a function that makes calls of its own saves it first, which is the choice the rest of this page measures. The difference is that those registers hold an address and the return is an instruction that jumps to it, where CARDIAC writes the jump itself.

Why no Stack?!

It's easy to read CARDIAC's missing stack as a shortcut for the classroom. It's closer to say CARDIAC works the way most computers did when it was designed. The IBM System/360's BALR put the return address in a register, and the PDP-8's JMS put it in the routine's first word. Neither had a stack, and FORTRAN, the language most of their programs were written in, didn't need one, because it didn't allow recursion.

Machines like these are the likely models for CARDIAC's JMP. The industry knew a subroutine needed somewhere to keep its return address, and each machine picked its own place: a register on the 360, the routine's first word on the PDP-8, cell 99 on CARDIAC. What it hadn't settled on yet was the model programmers take for granted today, a stack for calls and their local variables and a heap for everything else.

The stack was already in use, by the languages that couldn't do without it. LISP, on the IBM 704, started every recursive function by calling a SAVE routine, which copied the registers it was about to reuse onto what John McCarthy's 1960 paper calls the public push-down list, and finished with UNSAVE, which put them back. Edsger Dijkstra's compiler for ALGOL 60 kept a stack the same year. Both are what the factorial further down this page does: a stack built out of ordinary memory, on a machine that doesn't know it's there. The Burroughs B5000 built a stack into the hardware in 1961, but it was the exception.

The change came just after CARDIAC. The PDP-11, in 1970, made one of its registers the stack pointer and wired its call and return, JSR and RTS, to it, and the Intel 8080 brought CALL and RET with a stack in memory to microprocessors in 1974. That's the "since about the 1970s" on Brian L. Stuart's CARDIAC page, and it's why most programmers now expect a stack before they've thought about it.

Microprocessors had a stack from the very first. The Intel 4004 ran its programs from ROM, and Stan Mazor, one of its designers, explained in 1995 that the minicomputer way, saving the return address at the top of the subroutine, was out, because nothing could be written into ROM. So the 4004 kept up to three return addresses on a small stack inside the chip, the 8008 kept seven, and the 8080 moved the stack out into memory, where it could be as deep as the memory allowed. CARDIAC would have had the same trouble, because its JMP writes the return address into memory, in cell 99.

One return address, no stack Stack in software Stack in hardware

  1. 1957 FORTRAN ships, with no recursion and so no stack
  2. 1960 LISP and ALGOL 60 build their stacks in software
  3. 1961 Burroughs B5000 puts a stack in hardware
  4. 1964 IBM System/360 keeps the return address in a register
  5. 1965 PDP-8 keeps the return address in the routine's first word
  6. 1968 CARDIAC keeps the return address in cell 99
  7. 1970 PDP-11 wires JSR and RTS to a stack pointer
  8. 1971 Intel 4004 keeps three return addresses on the chip
  9. 1972 Intel 8008 keeps seven
  10. 1974 Intel 8080 puts the stack in memory, with CALL and RET

Stackless

A routine that never calls itself only ever has one return address to keep, and it doesn't need a stack for that. 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. Stuart's page builds its routines this way too, before it gets to the stack.

The job for the first two programs is a small one: double a number and print it, in two places in a program.

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 it in Cardigan

It prints 6 and 8, in 14 cells and 19 instructions. The linkage is three cells, CLA 99, the STO and the return address, and two instructions a call. If the argument arrives in the accumulator, it has to be stored before CLA 99, which can cost one more.

This is the PDP-8's scheme, done by hand. 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.

The return address isn't the only address worth keeping in a cell. A routine is just the address of its first cell, so adding that address to JMP 00 makes a call you can store and pass around, as in call a routine through its address. That's a function pointer, which is how C's qsort takes its comparison, and how a C++ virtual call reaches the right function through the object's vtable.

What stackless gives up is recursion. A routine that calls itself overwrites its own copy, and the outer call loses its return address. The same goes for two routines that call each other, because the second call to the first one overwrites the copy the first call is still waiting on. The PDP-8's routines had the same limit.

But I really want a stack

Then build one. The scheme here is Stuart's, from his CARDIAC page. The routine pushes cell 99 as soon as it starts, and pops it into its last cell just before it runs into it. Stuart keeps the top of the stack as an address, adds it to STO 00 to push and to CLA 00 to pop, and lets the stack grow down. Here cell 50 keeps the STO itself, so a push only has to copy it and add 1, the stack grows up, and subtracting 500 turns the STO 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 it in Cardigan

It prints the same 6 and 8, in 28 cells and 41 instructions. Pushing runs 6 instructions and popping 7, so every call costs 13 before the routine does anything. The routine spends 14 cells on pushing, popping and its return address, and the top, the 500 and the stack take 3 more, one stack cell for every call that can be under way at once. The doubling itself takes 4. On x86, CALL pushes the return address and RET pops it, so the push and pop that cost 13 instructions here come built into the call and the return.

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.

Orange is what each version spends on the return address. In the stack version it's most of the program, 17 of its 28 cells, wrapped around 4 cells of doubling.

A stack pays for itself in recursion. Here's factorial, where n! is n times (n - 1)!, and 0! is 1. Each call pushes its return address and its n, calls itself with n - 1, and pops both before it multiplies and returns. 0! pushes nothing and makes no JMP, so cell 99 still holds its return address, and the stackless return is enough there.

cell  contents  notes
 10      089    INP 89  cells 10-15: print n!, for n up to 6; fact takes n in the accumulator
 11      189    CLA 89
 12      820    JMP 20
 13      689    STO 89
 14      589    OUT 89
 15      900    HRS 00
 20      665    STO 65  cells 20-22: fact(n); 0! is 1
 21      700    SUB 00
 22      361    TAC 61
 23      170    CLA 70  cells 23-32: push the return address and n; cell 70 is the STO to the stack's top
 24      630    STO 30
 25      200    ADD 00
 26      632    STO 32
 27      200    ADD 00
 28      670    STO 70
 29      199    CLA 99
 30      600    STO 00  STO to the top, written by cell 24
 31      165    CLA 65
 32      600    STO 00  STO to the next, written by cell 26
 33      700    SUB 00  cells 33-35: fact(n - 1), which overwrites cell 99, but the return address is on the stack
 34      820    JMP 20
 35      666    STO 66
 36      170    CLA 70  cells 36-46: pop n and the return address; CLA is STO minus 500
 37      700    SUB 00
 38      700    SUB 00
 39      670    STO 70
 40      769    SUB 69
 41      644    STO 44
 42      200    ADD 00
 43      646    STO 46
 44      100    CLA 00  the return address, read from the stack
 45      660    STO 60
 46      100    CLA 00  n, read from the stack
 47      700    SUB 00  cells 47-59: n times (n - 1)!: add it n - 1 more times
 48      667    STO 67
 49      166    CLA 66
 50      668    STO 68
 51      167    CLA 67
 52      700    SUB 00
 53      359    TAC 59
 54      667    STO 67
 55      168    CLA 68
 56      266    ADD 66
 57      668    STO 68
 58      851    JMP 51
 59      168    CLA 68
 60      000    the return address, popped into place
 61      199    CLA 99  cells 61-64: 0! is 1; nothing was pushed, and cell 99 still holds the return address
 62      664    STO 64
 63      100    CLA 00
 64      000    the return address, copied from cell 99
 65      000    n
 66      000    (n - 1)!
 67      000    the count
 68      000    the product
 69      500
 70      671    STO 71
 71      000    cells 71-82: the stack, two cells for each call

Step through it in Cardigan

It prints 120 for 5, in 58 cells and 273 instructions, and 720 for 6 in 349. Here the push and pop run 21 instructions a call, because n goes on the stack too, and each call takes two cells of stack, so 6!, the biggest factorial that fits in a cell, needs twelve.

001122334455667788990000101020304050607080910INP11CLA12JMP13STO14OUT15HRS1617181920STO21SUB22TAC23CLA24STO25ADD26STO27ADD28STO29CLA30STO31CLA32STO33SUB34JMP35STO36CLA37SUB38SUB39STO40SUB41STO42ADD43STO44CLA45STO46CLA47SUB48STO49CLA50STO51CLA52SUB53TAC54STO55CLA56ADD57STO58JMP59CLA60back61CLA62STO63CLA64back65n66(n-1)!67count68prod6950070top71172173274275376377478479580581682683848586878889909192939495969798998xx

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

The stack is cells 71 to 82, two cells for each call, and 6! makes six calls that push. The 16 free cells above it would hold eight more.

On CARDIAC the stack's depth is a budget of cells, set before the program runs. Every level is two cells the program can't use for anything else, and nothing stops a call deeper than the budget from writing past the end. Here the budget is easy, because 7! wouldn't fit in a cell anyway.

Modern systems have the same budget with bigger numbers. Each thread gets a stack of a fixed size, often between one and eight megabytes, and recursion that goes too deep runs off the end of it. That's a stack overflow, and most systems leave an unmapped guard page past the end, so the program stops instead of writing over whatever lies beyond.

Unless the call comes last

Some recursion doesn't need the stack at all. When a routine's call of itself is the last thing it does, the outer call has nothing left to do when the inner one returns but pop its own return address and return too. So don't call. Put the new arguments where the routine reads them and jump back to its start, just past the copy of cell 99, and the return address the first call copied is still in its cell.

Here's the greatest common divisor, where gcd(a, b) is gcd(b, a mod b), and gcd(a, 0) is a. Written with the stack, the way the recursion reads, each call pushes its return address. Nothing else is needed after the call, so nothing else is pushed. The mod is the loop from subtract until it goes negative, then add one back.

cell  contents  notes
 10      050    INP 50  cells 10-14: print gcd(a, b), for a and b from 0 to 999; gcd leaves its answer in a
 11      051    INP 51
 12      820    JMP 20
 13      550    OUT 50
 14      900    HRS 00
 20      153    CLA 53  cells 20-25: push cell 99; cell 53 is the STO to the stack's top
 21      625    STO 25
 22      200    ADD 00
 23      653    STO 53
 24      199    CLA 99
 25      600    STO 00  STO to the top, written by cell 21
 26      151    CLA 51  cells 26-28: gcd(a, 0) is a
 27      700    SUB 00
 28      340    TAC 40
 29      150    CLA 50  cells 29-33: a mod b: subtract b until it goes negative, then add one back
 30      751    SUB 51
 31      333    TAC 33
 32      830    JMP 30
 33      251    ADD 51
 34      652    STO 52  cells 34-39: gcd(b, a mod b), the last thing it does
 35      151    CLA 51
 36      650    STO 50
 37      152    CLA 52
 38      651    STO 51
 39      820    JMP 20
 40      153    CLA 53  cells 40-47: pop the return address into cell 47 and run it; CLA is STO minus 500
 41      700    SUB 00
 42      653    STO 53
 43      754    SUB 54
 44      645    STO 45
 45      100    CLA 00  the return address, read from the stack
 46      647    STO 47
 47      000    the return address, popped into place
 50      000    a
 51      000    b
 52      000    a mod b
 53      655    STO 55
 54      500
 55      000    cells 55-70: the stack, one cell for each call; 610 and 987 make 16

Step through it in Cardigan

Written as a jump, the call is JMP 22, and the routine is a loop with the stackless return around it.

cell  contents  notes
 10      050    INP 50  cells 10-14: print gcd(a, b), for a and b from 0 to 999; gcd leaves its answer in a
 11      051    INP 51
 12      820    JMP 20
 13      550    OUT 50
 14      900    HRS 00
 20      199    CLA 99  cells 20-21: copy cell 99 into the routine's last cell
 21      636    STO 36
 22      151    CLA 51  cells 22-24: gcd(a, 0) is a
 23      700    SUB 00
 24      336    TAC 36
 25      150    CLA 50  cells 25-29: a mod b: subtract b until it goes negative, then add one back
 26      751    SUB 51
 27      329    TAC 29
 28      826    JMP 26
 29      251    ADD 51
 30      652    STO 52  cells 30-34: gcd(b, a mod b), the last thing it does, as a jump back to cell 22
 31      151    CLA 51
 32      650    STO 50
 33      152    CLA 52
 34      651    STO 51
 35      822    JMP 22
 36      000    the return address, copied from cell 99
 50      000    a
 51      000    b
 52      000    a mod b

Step through it in Cardigan

Both print 21 for 252 and 105. With the stack it takes 39 cells, and 15 more for the stack to grow into, and runs 121 instructions; as a jump it takes 25 cells and runs 68. For 610 and 987, the deepest pair below 1000, it's 472 instructions against 251, and all 16 cells of the stack are in use. Every call with the stack costs 14 instructions, 13 to push and pop and one to return, and once the answer is found, the pops do nothing but hand the return address down the chain. With the jump, the depth costs nothing.

With the stack

001122334455667788990000101020304050607080910INP11INP12JMP13OUT14HRS151617181920CLA21STO22ADD23STO24CLA25STO26CLA27SUB28TAC29CLA30SUB31TAC32JMP33ADD34STO35CLA36STO37CLA38STO39JMP40CLA41SUB42STO43SUB44STO45CLA46STO47back484950a51b52mod53top54500551562573584595606617628639641065116612671368146915701671727374757677787980818283848586878889909192939495969798998xx

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

610 and 987 need all 16 cells of stack. 32 of the 54 cells it uses are linkage.

As a jump

001122334455667788990000101020304050607080910INP11INP12JMP13OUT14HRS151617181920CLA21STO22CLA23SUB24TAC25CLA26SUB27TAC28JMP29ADD30STO31CLA32STO33CLA34STO35JMP36back3738394041424344454647484950a51b52mod53545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798998xx

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

3 of its 25 cells are linkage.

Compilers call this tail-call elimination, and GCC and LLVM do it when they optimize. Scheme goes further and requires it. Its standard says an implementation must be properly tail-recursive, so a loop can be written as a function that calls itself last and never runs out of stack.

Two routines that call each other only as the last thing each does can be written the same way, as one routine. Say is_even(n) is is_odd(n - 1), and is_odd(n) is is_even(n - 1). They keep one return address between them, each start copies cell 99 into it when it's called from outside, and each call of the other is a jump past the other's copy.

Stackless or a 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

How rust-cardiac decides

rust-cardiac makes this choice for you. Every function is stackless, with fixed cells for its locals and its return address. A function that calls itself as the last thing it does becomes a loop, and functions that call one another as the last thing each does become one function. Only a function that calls itself anywhere else gets a stack of its own, in the memory the program leaves over, and only the locals it still reads after the call are pushed, as n is in the factorial. A call past the end of that stack panics with stack overflow instead of writing over the program. Recursion through two or more functions, other than at their ends, is refused with RCE0013, and the compiler says to write it as a loop.

Fixed cells for every local and for the return address is how Fortran worked for a long time. The language didn't allow recursion until Fortran 90 added the RECURSIVE keyword, so compilers were free to give every local variable a fixed address, just as rust-cardiac does.

Sources

Know a trick like this one? Send it to nerd@strangequery.com and it might end up here.

The short version of this tip, with the others in all the tips.