Counter machine
A counter machine is an abstract machine used in a formal logic and theoretical computer science to model computation. It is the most primitive of the four types of register machines. A counter machine comprises a set of one or more unbounded registers, each of which can hold a single non-negative integer, and a list of arithmetic and control instructions for the machine to follow. The counter machine is typically used in the process of designing parallel algorithms in relation to the mutual exclusion principle. When used in this manner, the counter machine is used to model the discrete time-steps of a computational system in relation to memory accesses. By modeling computations in relation to the memory accesses for each respective computational step, parallel algorithms may be designed in such a matter to avoid interlocking, the simultaneous writing operation by two threads to the same memory address.
Basic features
For a given counter machine model the instruction set is tiny—from just one to six or seven instructions. Most models contain a few arithmetic operations and at least one conditional operation. Three base models, each using three instructions, are drawn from the following collection.- CLR : CLeaR register r.
- INC : INCrement the contents of register r.
- DEC : DECrement the contents of register r.
- CPY : CoPY the contents of register rj to register rk leaving the contents of rj intact.
- JZ : IF register r contains Zero THEN Jump to instruction z ELSE continue in sequence.
- JE : IF the contents of register rj Equals the contents of register rk THEN Jump to instruction z ELSE continue in sequence.
Using the instructions mentioned above, various authors have discussed certain counter machines:
- set 1:,, Lambek )
- set 2:,, Peter as interpreted by Shepherdson-Sturgis ; Minsky ; Schönhage )
- set 3:,, Minsky )
Alternative names, alternative models
The counter machine models go by a number of different names that may help to distinguish them by their peculiarities. In the following the instruction "JZDEC " is a compound instruction that tests to see if a register r is empty; if so then jump to instruction Iz, else if not then DECrement the contents of r:- Shepherdson-Sturgis machine, because these authors formally stated their model in an easily accessible exposition. Uses instruction set augmented with additional convenience instructions :
- :
- Minsky machine, because Marvin Minsky formalized the model. Usually uses instruction set, but instruction-execution is not default-sequential so the additional parameter 'z' appears to specify the next instruction after INC and as the alternative in JZDEC:
- :
- Program machine, Program computer, the names Minsky gave the model because, like a computer its instructions proceed sequentially unless a conditional jump is successful. Uses instruction set but may be augmented similar to the Shepherson-Sturgis model. JZDEC is often split apart:
- :
- Abacus machine, the name Lambek gave to his simplification of the Melzak model, and what Boolos-Burgess-Jeffrey calls it. Uses instruction set but with an additional parameter z to specify the next instruction in the manner of the Minsky model;
- :
- Lambek machine, an alternative name Boolos-Burgess-Jeffrey gave to the abacus machine. Uses instruction set with an additional parameter to specify the next instruction:
- :
- Successor machine, because it uses the 'successor operation' of, and closely resembles, the Peano axioms. Used as a base for the successor RAM model. Uses instruction set by e.g. Schönhage as a base for his RAM0 and RAM1 models that lead to his pointer machine SMM model, also discussed briefly in van Emde Boas :
- :
- Elgot-Robinson model, used to define their RASP model. This model requires one empty register at the start.
- :
- Other counter machines: Minsky demonstrates how to build the three base models from the superset of available instructions described in the lead paragraph of this article. The Melzak model is quite different from the above because it includes 'add' and 'subtract' rather than 'increment' and 'decrement'. The proofs of Minsky that a single register will suffice for Turing equivalence requires the two instructions to encode and decode the Gödel number in the register that represents the computation. Minsky shows that if two or more registers are available then the simpler INC, DEC etc. are adequate.
Formal definition
- Labeled unbounded integer-valued registers: a finite set of registers r0 ... rn each of which can hold any single non-negative integer. The registers do their own arithmetic; there may or may not be one or more special registers e.g. "accumulator".
- A state register that stores/identifies the current instruction to be executed. This register is finite and separate from the registers above; thus the counter machine model is an example of the Harvard architecture
- List of labelled, sequential instructions: A finite list of instructions I0 ... Im. The program store is not in the same physical "space" as the registers. Usually, but not always, like computer programs the instructions are listed in sequential order; unless a jump is successful, the default sequence continues in numerical order. Each of the instructions in the list is from a small set, but this set does not include indirection. Historically most models drew their instructions from this set:
Example: COPY the count from register #2 to #3
- CLR : Clear the contents of register rj to zero.
- J : Unconditionally jump to instruction Iz.
- CPY : Copy the contents of source register rs to destination register rd. Afterward rs will contain its original count.
Instruction | Effect on register "j" | Effect on Instruction Counter Register ICR | Summary |
INC | +1 → j | +1 → IC | Increment contents of register j; next instruction |
DEC | -1 → j | +1 → IC | Decrement contents of register j; next instruction |
JZ | IF = 0 THEN Iz → IC ELSE +1 → IC | If contents of register j=0 then instruction z else next instruction | |
HALT |
Initial conditions
Initially, register #2 contains "2". Registers #0, #1 and #3 are empty. Register #0 remains unchanged throughout calculations because it is used for the unconditional jump. Register #1 is a scratch pad. The program begins with instruction 1.Final conditions
The program HALTs with the contents of register #2 at its original count and the contents of register #3 equal to the original contents of register #2, i.e.,Program high-level description
The program COPY has two parts. In the first part the program moves the contents of source register #2 to both scratch-pad #1 and to destination register #3; thus #1 and #3 will be copies of one another and of the original count in #2, but #2 is cleared in the process of decrementing it to zero. Unconditional jumps J are done by tests of register #0, which always contains the number 0:In the second the part the program moves the contents of scratch-pad #1 back to #2, clearing the scratch-pad #1 in the process:
Program
The program, highlighted in yellow, is shown written left-to-right in the upper right.A "run" of the program is shown below. Time runs down the page. The instructions are in yellow, the registers in blue. The program is flipped 90 degrees, with the instruction numbers along the top, the instruction mnemonics under the addresses, and the instruction parameters under the mnemonics :
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ← Instruction number | |||||||||||||||
JZ | DEC | INC | INC | JZ | JZ | DEC | INC | JZ | H | ← Instruction | |||||||||||||||
2 | 2 | 3 | 1 | 0 | 1 | 1 | 2 | 0 | ← Register number | ||||||||||||||||
6 | 1 | 10 | 6 | ← Jump-to instruction number | |||||||||||||||||||||
step | IC | Inst # | reg # | J-addr | reg0 | reg1 | reg2 | reg3 | reg4 | IC | |||||||||||||||
start | 0 | 0 | 2 | 0 | 0 | 1 | move to #1 and #3: | ||||||||||||||||||
1 | 1 | JZ | 2 | 6 | 0 | 0 | 2 | 0 | 0 | 1→2 | JZ | Jump fails: register #2 contains 2 | |||||||||||||
2 | 2 | DEC | 2 | 0 | 0 | 0 | 2→1 | 0 | 0 | 2→3 | DEC | Decrement register #2 from 2 to 1 | |||||||||||||
3 | 3 | INC | 3 | 0 | 0 | 0 | 1 | 0→1 | 0 | 3→4 | INC | Increment register #3 from 0 to 1 | |||||||||||||
4 | 4 | INC | 1 | 0 | 0 | 0→1 | 1 | 1 | 0 | 4→5 | INC | Increment register #1 from 0 to 1 | |||||||||||||
5 | 5 | JZ | 0 | 1 | 0 | 1 | 1 | 1 | 0 | 5→1 | JZ | U-Jump: register #0 is empty | |||||||||||||
6 | 1 | JZ | 2 | 6 | 0 | 1 | 1 | 1 | 0 | 1→2 | JZ | Jump fails: register #2 contains 1 | |||||||||||||
7 | 2 | DEC | 2 | 0 | 0 | 1 | 1→0 | 1 | 0 | 2→3 | DEC | Decrement register #2 from 1 to 0 | |||||||||||||
8 | 3 | INC | 3 | 0 | 0 | 1 | 0 | 1→2 | 0 | 3→4 | INC | Increment register #3 from 1 to 2 | |||||||||||||
9 | 4 | INC | 1 | 0 | 0 | 1→2 | 0 | 2 | 0 | 4→5 | INC | Increment register #1 from 1 to 2 | |||||||||||||
10 | 5 | JZ | 0 | 1 | 0 | 2 | 0 | 2 | 0 | 5→1 | JZ | U-Jump: register #0 is empty | |||||||||||||
11 | 1 | JZ | 2 | 6 | 0 | 2 | 0 | 2 | 0 | 1→6 | JZ | Jump !: register #2 is empty | |||||||||||||
move to 2: | |||||||||||||||||||||||||
12 | 6 | JZ | 1 | 10 | 0 | 2 | 0 | 2 | 0 | 6→7 | JZ | Jump fails: register #1 contains 2 | |||||||||||||
13 | 7 | DEC | 1 | 0 | 0 | 2→1 | 0 | 2 | 0 | 7→8 | DEC | Decrement register #1 from 2 to 1 | |||||||||||||
14 | 8 | INC | 2 | 0 | 0 | 1 | 0→1 | 2 | 0 | 8→9 | INC | Increment register #2 from 0 to 1 | |||||||||||||
15 | 9 | JZ | 0 | 6 | 0 | 1 | 1 | 2 | 0 | 9→6 | JZ | U-Jump: register #0 is empty | |||||||||||||
16 | 6 | JZ | 1 | 10 | 0 | 1 | 1 | 2 | 0 | 6→7 | JZ | Jump fails: register #1 contains 1 | |||||||||||||
17 | 7 | DEC | 1 | 0 | 0 | 1→0 | 1 | 2 | 0 | 7→8 | DEC | Decrement register #1 from 1 to 0 | |||||||||||||
18 | 8 | INC | 2 | 0 | 0 | 0 | 1→2 | 2 | 0 | 8→9 | INC | Increment register #2 from 1 to 2 | |||||||||||||
19 | 9 | JZ | 0 | 6 | 0 | 0 | 2 | 2 | 0 | 9→6 | JZ | U-Jump: register #0 is empty | |||||||||||||
20 | 6 | JZ | 1 | 10 | 0 | 0 | 2 | 2 | 0 | 6→10 | JZ | Jump !: register #1 is empty | |||||||||||||
21 | 10 | H | 0 | 0 | 0 | 0 | 2 | 2 | 0 | 10→10 | H | HALT |
The partial recursive functions: building "convenience instructions" using recursion
The example above demonstrates how the first basic instructions can create three more instructions—unconditional jump J, CLR, CPY. In a sense CPY used both CLR and J plus the base set. If register #3 had had contents initially, the sum of contents of #2 and #3 would have ended up in #3. So to be fully accurate CPY program should have preceded its moves with CLR and CLR.However, we do see that ADD would have been possible, easily. And in fact the following is summary of how the primitive recursive functions such as ADD, MULtiply and EXPonent can come about.
- Beginning instruction set:
- Define unconditional "Jump J " in terms of JZ given that r0 contains 0.
- Define "CLeaR in terms of the above:
- Define "CoPY " while preserving contents of rj in terms of the above:
- Define "ADD ", , by use of the above:
- Define " MULtiply " , in terms of the above:
- Define "EXPonential " in terms of the above,
But what about full Turing equivalence? We need to add the sixth operator—the μ operator—to obtain the full equivalence, capable of creating the total- and partial- recursive functions:
- Zero function
- Successor function
- Identity function
- Composition function
- Primitive recursion
- μ operator
Problems with the counter machine model
Unbounded capacities of registers versus bounded capacities of state-machine instructions: How will the machine create constants larger than the capacity of its finite state machine?Unbounded numbers of registers versus bounded numbers of state-machine instructions: How will the machine access registers with address-numbers beyond the reach/capability of its finite state machine?
The fully reduced models are cumbersome:
Shepherdson and Sturgis are unapologetic about their 6-instruction set. They have made their choice based on "ease of programming... rather than economy".
Shepherdson and Sturgis' instructions :
- * INCREMENT ; +1 → r
- * DECREMENT ; -1 → r
- * CLEAR ; 0 → r
- * COPY ; → rd
- * JUMP-UNCONDITIONAL to instruction Iz
- * JUMP IF =0 to instruction Iz
Two-counter machines are Turing equivalent (with a caveat)
For every Turing machine, there is a 2CM that simulates it, given that the 2CM's input and output are properly encoded. This is proved in Minsky's book, and an alternative proof is sketched below in three steps. First, a Turing machine can be simulated by a finite-state machine equipped with two stacks. Then, two stacks can be simulated by four counters. Finally, four counters can be simulated by two counters.The two counters machine use the set of instruction.
Step 1: A Turing machine can be simulated by two stacks.
A Turing machine consists of an FSM and an infinite tape, initially filled with zeros, upon which the machine can write ones and zeros. At any time, the read/write head of the machine points to one cell on the tape. This tape can be conceptually cut in half at that point. Each half of the tape can be treated as a stack, where the top is the cell nearest the read/write head, and the bottom is some distance away from the head, with all zeros on the tape beyond the bottom. Accordingly, a Turing machine can be simulated by an FSM plus two stacks. Moving the head left or right is equivalent to popping a bit from one stack and pushing it onto the other. Writing is equivalent to changing the bit before pushing it.Step 2: A stack can be simulated by two counters.
A stack containing zeros and ones can be simulated by two counters when the bits on the stack are thought of as representing a binary number. Pushing a zero onto the stack is equivalent to doubling the number. Pushing a one is equivalent to doubling and adding 1. Popping is equivalent to dividing by 2, where the remainder is the bit that was popped. Two counters can simulate this stack, in which one of the counters holds a number whose binary representation represents the bits on the stack, and the other counter is used as a scratchpad. To double the number in the first counter, the FSM can initialize the second counter to zero, then repeatedly decrement the first counter once and increment the second counter twice. This continues until the first counter reaches zero. At that point, the second counter will hold the doubled number. Halving is performed by decrementing one counter twice and increment the other once, and repeating until the first counter reaches zero. The remainder can be determined by whether it reached zero after an even or an odd number of steps, where the parity of the number of steps is encoded in the state of the FSM.Step 3: Four counters can be simulated by two counters.
As before, one of the counters is used as scratchpad. The other holds an integer whose prime factorization is 2a3b5c7d. The exponents a, b, c, and d can be thought of as four virtual counters that are being packed into a single real counter. If the real counter is set to zero then incremented once, that is equivalent to setting all the virtual counters to zero. If the real counter is doubled, that is equivalent to incrementing a, and if it's halved, that's equivalent to decrementing a. By a similar procedure, it can be multiplied or divided by 3, which is equivalent to incrementing or decrementing b. Similarly, c and d can be incremented or decremented. To check if a virtual counter such as c is equal to zero, just divide the real counter by 5, see what the remainder is, then multiply by 5 and add back the remainder. That leaves the real counter unchanged. The remainder will have been nonzero if and only if c was zero.As a result, an FSM with two counters can simulate four counters, which are in turn simulating two stacks, which are simulating a Turing machine. Therefore, an FSM plus two counters is at least as powerful as a Turing machine. A Turing machine can easily simulate an FSM with two counters, therefore the two machines have equivalent power.
The caveat: *If* its counters are initialised to ''N'' and 0, then a 2CM cannot calculate 2''N''
This result, together with a list of other functions of N that are not calculable by a two-counter machine — when initialised with N in one counter and 0 in the other — such as N2, sqrt, log2, etc., appears in a paper by Schroeppel. The result is not surprising, because the two-counter machine model was proved to be universal only when the argument N is appropriately encoded to simulate a Turing machine whose initial tape contains N encoded in unary; moreover, the output of the two-counter machine will be similarly encoded. This phenomenon is typical of very small bases of computation whose universality is proved only by simulation.The proof is preceded by some interesting theorems:
- "Theorem: A three-counter machine can simulate a Turing machine"
- "Theorem: A 3CM can compute any partial recursive function of one variable. It starts with the argument in a counter, and leaves the answer in a counter."
- "Theorem: A counter machine can be simulated by a 2CM , provided an obscure coding is accepted for the input and output"
- "Theorem: Any counter machine can be simulated by a 2CM, provided an obscure coding is accepted for the input and output."
- * "Corollary: the halting problem for 2CMs is unsolvable.
- * "Corollary: A 2CM can compute any partial recursive function of one argument, provided the input is coded as 2N and the output is coded as 2answer."
- "Theorem: There is no two counter machine that calculates 2N ."
A practical example of calculation by counting
The Friden EC-130 calculator had no adder logic as such. Its logic was highly serial, doing arithmetic by counting. Internally, decimal digits were radix-1 — for instance, a six was represented by six consecutive pulses within the time slot allocated for that digit. Each time slot carried one digit, least significant first. Carries set a flip-flop which caused one count to be added to the digit in the next time slot.Adding was done by an up-counter, while subtracting was done by a down-counter, with a similar scheme for dealing with borrows.
The time slot scheme defined six registers of 13 decimal digits, each with a sign bit.
Multiplication and division were by done basically by repeated addition and subtraction. The square root version, the EC-132, effectively subtracted consecutive odd integers, each decrement requiring two consecutive subtractions. After the first, the minuend was incremented by one before the second subtraction.