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 › Testing with one branch

Testing with one branch

CARDIAC can ask exactly one question: is the accumulator negative? This page is about that question, where it came from, how every other test is built out of it, and how processors and compilers ask their questions now.

TAC

TAC jumps to its address when the accumulator is negative, and otherwise carries on with the next cell. That's the whole instruction, and it's CARDIAC's only test. There's no jump on zero, no jump if equal and no compare. If you want to know whether a is less than b, you load a, subtract b and TAC on the difference.

TAC leaves two things alone that matter later. It doesn't write cell 99, as JMP does, so a routine that ends with the accumulator negative can return with TAC 99, as in return with TAC 99 when the accumulator is negative, and the Subroutines page has the rest of that story. And it doesn't change the accumulator, so whatever a subtraction left there is still there for the next instruction, and the next TAC, to use.

Where TAC came from

CARDIAC is from 1968, but its test is twenty years older. The Manchester Baby, which ran the world's first stored program on 21 June 1948, had seven instructions, and one of them was a test. CMP skipped the next instruction if the accumulator was negative, and that was all the deciding the Baby could do. Since the test only skipped, a branch was a CMP followed by a jump, which the skip stepped over.

The program it ran that day was Tom Kilburn's, and it found the highest proper factor of 218 by trying every number below it in turn. The Baby had no divide, so each try subtracted the trial factor until the accumulator went negative, and its CMP was what noticed. The run took 52 minutes and about 2.1 million instructions. There's a CARDIAC version further down.

EDSAC, at Cambridge, ran its first program on 6 May 1949, and had two conditional jumps instead of a skip. G jumped if the accumulator was less than zero, and E jumped if it was greater than or equal to zero. That's still one question, the sign, but EDSAC let you jump on either answer.

The IBM 704, from 1954, asked more. TMI transferred on minus and TPL on plus, and TZE transferred on zero and TNZ on anything else. It also had CAS, compare accumulator with storage, which compared the accumulator with a cell and skipped no instructions, one or two, for less, equal and greater. That's a three-way comparison in one instruction, which CARDIAC builds out of two TACs further down.

Why the sign came first

The sign is one bit, in one known place. The 704's manual says it in so many words: TMI and TPL look at "the sign bit of the AC". Asking whether the accumulator is zero means looking at every bit of it, and TZE looks at 37 of the 704's bits, everything but the sign. Asking whether one number is less than another needs a subtraction first, and somewhere to keep what it leaves, and on these machines that somewhere was the accumulator. Once you've subtracted, the sign of the difference is the answer, so a sign test plus a subtractor gives you every comparison, a step or two at a time. That makes the sign test the cheapest one to build that still makes all the others possible.

By CARDIAC's time, machines asked more than that. The IBM System/360, from 1964, set a two-bit condition code after most of its instructions, and its BRANCH ON CONDITION could branch on any mix of the code's four states. The PDP-8, from 1965, could skip on minus, zero or plus, and on combinations of them, so SMA SZA skipped when the accumulator was zero or negative. CARDIAC's TAC is closer to 1948 than to 1968. It likely copies the machines CARDIAC was modelled on, though we haven't found anything from its designers that says so. It's also the one test a teaching machine needs, because, as the rest of this page shows, every other test can be built from it.

Every test from one

The tips cover the everyday cases, each with a program and its measured cost. In short:

The two programs below put those together in bigger jobs.

Three answers from one subtraction

Say you want to compare a and b the way Rust's cmp does, and print -1 if a is less, 0 if they're equal and 1 if a is greater. The natural way asks two questions, each a subtraction and a TAC: is a - b negative, and is b - a negative? If neither is, they're equal.

cell  contents  notes
 10      040    INP 40  cells 10-11: a and b
 11      041    INP 41
 12      140    CLA 40  cells 12-14: a - b < 0 is less
 13      741    SUB 41
 14      320    TAC 20
 15      141    CLA 41  cells 15-17: b - a < 0 is greater
 16      740    SUB 40
 17      322    TAC 22
 18      542    OUT 42  cells 18-23: print 0 for equal, -1 for less and 1 for greater
 19      900    HRS 00
 20      543    OUT 43
 21      900    HRS 00
 22      500    OUT 00
 23      900    HRS 00
 40      000    a
 41      000    b
 42      000    0
 43     -001    -1

Step through it in Cardigan

But TAC leaves the accumulator alone, so when a - b isn't negative it's still sitting there for a second question. It's 0 or more, and it's 0 exactly when a and b are equal, which is the SUB 00 test. Take 1 more away and TAC again: if that's negative, they were equal. One subtraction answers both questions.

cell  contents  notes
 10      040    INP 40  cells 10-11: a and b
 11      041    INP 41
 12      140    CLA 40  cells 12-16: one subtraction, two TACs; a - b < 0 is less, then a - b - 1 < 0 is equal
 13      741    SUB 41
 14      319    TAC 19
 15      700    SUB 00
 16      321    TAC 21
 17      500    OUT 00  cells 17-22: print 1 for greater, -1 for less and 0 for equal
 18      900    HRS 00
 19      543    OUT 43
 20      900    HRS 00
 21      542    OUT 42
 22      900    HRS 00
 40      000    a
 41      000    b
 42      000    0
 43     -001    -1

Step through it in Cardigan

Both print -1 for 3 and 7, 0 for 7 and 7, and 1 for 7 and 3. Two subtractions take 18 cells, and run 7 instructions when a is less and 10 otherwise. One subtraction takes 17 cells and runs 7 and 9. It's a small saving, but it's the same idea as condition codes, one comparison read by more than one branch, and CARDIAC gets it for free because the accumulator is where the comparison's result lives.

Two subtractions

001122334455667788990000101020304050607080910INP11INP12CLA13SUB14TAC15CLA16SUB17TAC18OUT19HRS20OUT21HRS22OUT23HRS2425262728293031323334353637383940a41b42043-14445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

Reading, printing and setting upThe testsVariables and constantsCell 00, the constant 1Free

The comparison is 6 of its 18 cells.

One subtraction

001122334455667788990000101020304050607080910INP11INP12CLA13SUB14TAC15SUB16TAC17OUT18HRS19OUT20HRS21OUT22HRS232425262728293031323334353637383940a41b42043-14445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

Reading, printing and setting upThe testsVariables and constantsCell 00, the constant 1Free

The comparison is 5 of its 17 cells.

The first program, on CARDIAC

Kilburn's highest factor fits on CARDIAC if the number does, so take n up to 999 and try every b from n - 1 down, stopping at the first that divides n. Here it is the way the Baby did it. Subtract b until the accumulator goes negative, which is the loop from subtract until it goes negative, then add one back. Then add one b back for the remainder, and test whether the remainder is 0 the way Kilburn's program did, by negating it: 0 - r is negative unless r is 0. Kilburn's loop was a subtraction, a CMP and a jump back that the CMP skipped once the accumulator went negative. On CARDIAC it's the same three instructions, with TAC jumping out and JMP going round.

cell  contents  notes
 10      040    INP 40  cells 10-13: n, and the first b, n - 1
 11      140    CLA 40
 12      700    SUB 00
 13      641    STO 41
 14      140    CLA 40  cells 14-17: subtract b until negative; TAC leaves the loop, JMP goes round
 15      741    SUB 41
 16      318    TAC 18
 17      815    JMP 15
 18      241    ADD 41  cells 18-22: add b back for the remainder r; 0 - r < 0 unless r is 0
 19      642    STO 42
 20      143    CLA 43
 21      742    SUB 42
 22      325    TAC 25
 23      541    OUT 41  cells 23-24: r is 0, so b divides n
 24      900    HRS 00
 25      141    CLA 41  cells 25-28: try the next b down
 26      700    SUB 00
 27      641    STO 41
 28      814    JMP 14
 40      000    n
 41      000    b
 42      000    r
 43      000    0

Step through it in Cardigan

Now turn the loop around, as in put the threshold at 0. Start from -n and add b until the accumulator stops being negative. While it's still negative, TAC is what goes round, so the JMP is gone, and the loop is two instructions where it was three. What's left when it stops is 0 if b divides n and somewhere from 1 to b - 1 if it doesn't, so the remainder test is just SUB 00 and TAC.

cell  contents  notes
 10      040    INP 40  cells 10-16: n, -n, and the first b, n - 1
 11      142    CLA 42
 12      740    SUB 40
 13      643    STO 43
 14      140    CLA 40
 15      700    SUB 00
 16      641    STO 41
 17      143    CLA 43  cells 17-19: from -n, add b until it reaches 0; TAC is what goes round
 18      241    ADD 41
 19      318    TAC 18
 20      700    SUB 00  cells 20-21: what's left is 0 only if b divides n; then minus 1 is negative
 21      326    TAC 26
 22      141    CLA 41  cells 22-25: try the next b down
 23      700    SUB 00
 24      641    STO 41
 25      817    JMP 17
 26      541    OUT 41  cells 26-27: b divides n
 27      900    HRS 00
 40      000    n
 41      000    b
 42      000    0
 43      000    -n

Step through it in Cardigan

Both print 256 for 512, the largest power of two that fits, in a nod to Kilburn's 218. Kilburn's way takes 23 cells and runs 3,845 instructions; counting up from -n takes 22 cells and runs 2,821. For 999 it's 10,496 against 7,665, and 997, which is prime, tries every b down to 1 and runs 33,074 against 23,047. The loop runs about n / b times for every b, so it's where the time goes, and the JMP it no longer has saves more than a quarter of the run.

Kilburn's way

001122334455667788990000101020304050607080910INP11CLA12SUB13STO14CLA15SUB16TAC17JMP18ADD19STO20CLA21SUB22TAC23OUT24HRS25CLA26SUB27STO28JMP293031323334353637383940n41b42r4304445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

Reading, printing and setting upThe loop that dividesThe testsVariables and constantsCell 00, the constant 1Free

Of its 23 cells, the loop is 4 and the test for a remainder of 0 is 5.

Up from -n

001122334455667788990000101020304050607080910INP11CLA12SUB13STO14CLA15SUB16STO17CLA18ADD19TAC20SUB21TAC22CLA23SUB24STO25JMP26OUT27HRS28293031323334353637383940n41b42043-n4445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

Reading, printing and setting upThe loop that dividesThe testsVariables and constantsCell 00, the constant 1Free

Of its 22 cells, the loop is 3 and the test for a remainder of 0 is 2.

From skips to flags

Machines went on asking more questions with each test, and asking them of something other than the accumulator.

The PDP-8's skips, from 1965, already showed one direction. They combine in one instruction, so SMA, skip on minus, and SZA, skip on zero, together skip if either is true. SPA SNA, skip on plus and skip on nonzero, skips only when the accumulator is above zero. One instruction asks whichever combination you need, though it still only skips, as the Baby's CMP did, and needs a jump after it to go anywhere else. Its ISZ, increment and skip if zero, is the loop counter from put the threshold at 0, built in.

The PDP-11, in 1970, made the answer a thing of its own. Four bits of its processor status register are condition codes: N if the result was negative, Z if it was zero, C if there was a carry out of the top bit, and V if there was an overflow. Moving or adding a number sets them, so there's often nothing extra to do before a test, and CMP is a subtraction that sets them and keeps nothing else. A family of branches reads them: BEQ and BNE on Z, BMI and BPL on N, and BLT, BGE, BGT and BLE, which combine N, V and Z for signed numbers. BHI and BLOS do the same with C and Z for unsigned ones, a distinction CARDIAC never needs, since its numbers all have a sign.

x86 has the same arrangement. The Intel 8086, from 1978, has a sign flag, a zero flag, an overflow flag and a carry flag, among others, and its CMP subtracts the second operand from the first and sets the flags as a SUB would. Its conditional jumps read them: JE when ZF is set, JS when SF is, JL when SF and OF differ, JB when CF is set, and so on. x86 processors still work this way, and Intel's current manual lists each conditional jump with the flags it tests.

RISC designs took a third way. MIPS, whose first processor, the R2000, came out in 1986, doesn't branch on flags. Its BEQ and BNE compare two registers and branch if they're equal or not, and its other branches compare one register with zero. RISC-V compares two registers in the branch itself, for equal, not equal, less than and greater than or equal, signed or unsigned. Its manual explains why. A compare-and-branch fits a pipeline neatly, needs no extra state for flags, and is seen earlier, so it can be predicted earlier. It also says that a test against zero isn't the cheap thing it was, since in current chips its circuit delay is almost that of a full comparison.

Prediction is the last change. Processors now guess which way a branch will go, and RISC-V's manual tells programmers to assume that a branch backward will be predicted taken and one forward won't be, at least the first time. A CARDIAC loop with its TAC at the bottom, jumping back while the count is negative, is exactly the branch that guess favours.

The sign Sign and zero Flags, read by a branch Compare and branch in one

  1. 1948 Manchester Baby skips an instruction if the accumulator is negative
  2. 1949 EDSAC jumps if the accumulator is negative, or if it isn't
  3. 1954 IBM 704 transfers on minus, plus and zero, and CAS compares three ways
  4. 1964 IBM System/360 branches on a two-bit condition code
  5. 1965 PDP-8 skips on minus, zero and plus, and on combinations of them
  6. 1968 CARDIAC jumps if the accumulator is negative
  7. 1970 PDP-11 keeps N, Z, V and C, and branches on them
  8. 1978 Intel 8086 sets flags with CMP, and reads them with conditional jumps
  9. 1986 MIPS R2000 compares two registers in the branch

Comparisons today

On a machine with flags, a compiler turns a comparison into an instruction that sets them and a jump that reads them. Here's what clang makes of a small function for x86-64, at -O2:

void pick(int a, int b) { if (a < b) f(); else g(); }

pick:
        cmp     edi, esi
        jge     g
        jmp     f

The CMP is a < b's subtraction, and JGE jumps to the else when a isn't less. The compiler turned the test around, so the jump goes to the else and the then comes right after it. TAC can only jump on "less", but x86 can jump on either answer. The calls became jumps because they're the last thing the function does, which is the tail call from the Subroutines page. A three-way comparison doesn't branch at all:

int8_t cmp3(int16_t a, int16_t b) { return (a > b) - (a < b); }

cmp3:
        cmp     di, si
        setl    cl
        setg    al
        sub     al, cl
        ret

One CMP, and its flags read twice, by SETL and SETG, which each turn a condition into a 0 or a 1. That's the one-subtraction comparison above, with flags where CARDIAC has the accumulator.

How rust-cardiac does it

rust-cardiac has only TAC to work with, so it builds every comparison the way this page does. In a normal build, a comparison that decides an if is never stored as a bool; it becomes the subtraction and the TAC. x < y loads x, subtracts y and TACs to the then. x <= c against a constant subtracts c + 1 instead, so it's still one test, and x > c is the same test with its branches swapped. x == c is the chain from the three-way comparison: subtract c, TAC away if it's below, subtract 1 and TAC if it's equal. When x can't be negative, x == 0 is SUB 00 and TAC alone. A match on consecutive values subtracts the lowest once and then spends a SUB 00 and a TAC on each arm.

For two variables it asks both questions. Here's a.cmp(&b), in what cargo cardiac builds by default:

pub fn main() {
    let a = inp();
    let b = inp();
    out(a.cmp(&b) as i16);
}
Start at 03.

03  017   main_bb0  INP   a   inp()
04  018             INP   b   inp()
05  117             CLA   a
06  718             SUB   b   if a < b
07  313             TAC   main_bb5
08  118             CLA   b
09  717             SUB   a   if a > b
10  315             TAC   main_bb6
11  520             OUT   k0  out(...)
12  900             HRS   00  return from main
13  519   main_bb5  OUT   kn1
14  900             HRS   00
15  500   main_bb6  OUT   00
16  900             HRS   00
17  000   a         DATA  000
18  000   b         DATA  000
19  -001  kn1       DATA  -001
20  000   k0        DATA  000

That's the two-subtraction program from earlier, instruction for instruction, in 18 cells, and it runs the same 7 and 10 instructions. The manual's operators section puts == and != among the things that cost more than they look, for this reason, and suggests testing with < and >= where the values allow, and keeping a value's sign meaningful. The compiler does that last part itself where it can. An Option<i16> whose value it can prove is never negative keeps None as -1, as in let negative mean "none", so is_none() is one TAC (see types). And the cardiac crate's Flag is a bool kept as 0 and -1, as in keep a flag as 0 and -1, so each test of it is a TAC and nothing else (see the runtime).

Sources

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

The short versions are the comparison tips, with the others in all the tips.