Definition
A register machine is a computer, real or virtual, whose instructions name the storage locations they work on. The locations are called registers: a small, fixed set of numbered slots that each hold one value. An instruction such as “add register 2 to register 1” says where its inputs are and where its result goes. The counterpart is the stack machine, whose arithmetic instructions name no locations: they take their inputs from the top of a pile of values, called the operand stack, and put the result back on top.
The idea
Every computation needs a place for intermediate results. To compute , a machine must hold the 7 somewhere before it multiplies. There are two classic answers. A register machine gives the holding places numbers, and each instruction says which ones it uses. A stack machine keeps the values in a pile, and each instruction works on the top of the pile.
Picture a workbench with a few labeled trays, and next to it a spike on which paper slips are stacked. With the trays, an order reads “add tray 2 into tray 1”. A value stays in its tray until something overwrites it, so it can be used again. With the spike, the order is just “add”: the top two slips come off, and one slip with their sum goes on. The orders are shorter, but a value is gone once it has been used, unless it was copied first. The picture is accurate for how an instruction finds its operands, the values it works on. It says nothing about speed, which depends on the engine that runs the instructions.
eBPF §2.1 (extended Berkeley Packet Filter), an instruction set for small programs that run inside a host program, is a register machine with eleven registers, R0 to R10. WebAssembly §19.5.6, an instruction set for programs that run inside a host such as a web browser, is a stack machine.
How it works
An arithmetic instruction of a register machine names its operation and its registers. In eBPF, the example used here, it has three parts: the operation, the register that receives the result, and the source of the other input, which is a register or a constant written inside the instruction. The receiving register is also the first input, so r1 += r2 means “add R2 to R1 and leave the sum in R1”. Leaving the machine’s memory aside, the state of a register machine between two instructions is the content of all registers plus the position of the next instruction.
A stack machine handles data with three kinds of instruction. A push puts a value on top of the operand stack. An operation such as add removes the values it needs from the top and puts its result on top. A pop removes the top value and stores it elsewhere. The state between two instructions is the whole stack plus the position of the next instruction.
Four practical differences follow.
Instruction size. A register instruction must carry its register numbers. A stack instruction carries none, so it can be shorter.
Instruction count. A stack machine needs separate instructions to bring each value to the top. A register machine can use a value where it lies, and use it again later.
The checker’s work. A checker for a register machine tracks, for each register, whether it holds a usable value. The eBPF verifier §3.3 of the Linux kernel rejects a program that reads a register it never wrote. Because each instruction names the registers it reads and writes, a checker can also work out which earlier instructions may have produced the value that an instruction reads, an analysis called reaching definitions §3.16. A checker for a stack machine tracks how many values are on the stack at each instruction, because an operation that finds too few has no defined input.
The engine’s work. An interpreter §2.13, an engine that carries out the instructions one at a time, can keep the registers of a virtual machine §2.3 in an array §14.5 in memory. A JIT §2.14 (just-in-time) compiler, which translates the program into instructions of the real processor, can give each virtual register a real one. The Linux documentation lists such a one-to-one assignment for 64-bit x86 processors.
Worked example
Compute in eBPF, where a program returns the value of R0. The bytes are in hexadecimal for a little-endian §4.21 machine, which stores the lowest byte of a number first. The last three columns show the registers after each instruction.
| instruction | bytes | R1 | R2 | R0 |
|---|---|---|---|---|
r1 = 3 |
b7 01 00 00 03 00 00 00 |
3 | ||
r2 = 4 |
b7 02 00 00 04 00 00 00 |
3 | 4 | |
r1 += r2 |
0f 21 00 00 00 00 00 00 |
7 | 4 | |
r1 *= 5 |
27 01 00 00 05 00 00 00 |
35 | 4 | |
r0 = r1 |
bf 10 00 00 00 00 00 00 |
35 | 4 | 35 |
exit |
95 00 00 00 00 00 00 00 |
35 | 4 | 35 |
The steps are and , and the program returns 35. In the third row, the byte 21 holds the two register numbers: 2 for the source and 1 for the destination.
The same computation on a stack machine, with the top of the stack written on the right:
| instruction | stack afterwards |
|---|---|
| push 3 | 3 |
| push 4 | 3, 4 |
| add | 7 |
| push 5 | 7, 5 |
| multiply | 35 |
The add removes 4 and 3 and puts 7 back. The multiply removes 5 and 7 and puts 35 back. Neither instruction names a location.
In WebAssembly these five instructions are i32.const 3, i32.const 4, i32.add, i32.const 5 and i32.mul. They occupy 8 bytes: 41 03 41 04 6a 41 05 6c. The four eBPF instructions that do the same arithmetic occupy bytes. Most of that difference comes from the fixed 8-byte format of eBPF, not from the register principle. The part that does come from the principle is visible in the WebAssembly bytes: the add is the single byte 6a, which has no room for operand names and needs none.
Two details matter in daily use. To square a value, a register machine needs one instruction, r1 *= r1, because the same register can be named twice. A stack machine must first get a second copy of the value onto the stack. And for subtraction, a stack machine must define which removed value is subtracted from which. WebAssembly subtracts the value pushed second from the value pushed first, so pushing 3, pushing 4 and subtracting gives .
Stated precisely
Write for the value in register number , and for the position of the next instruction. Leave memory aside. The state of a register machine is then the pair . The eBPF instruction that adds register number to register number changes the state as follows: becomes the low 64 bits of , becomes , and every other register keeps its value.
The state of a stack machine is the pair , where is a sequence of values whose last member is the top. The instruction add is defined only when holds at least two values. If the two top values are , pushed first, and , pushed second, then add replaces both by the single value , and becomes .
The two designs can express the same computations. They differ in how an instruction finds its operands, and the difference can be stated as the number of locations that an arithmetic instruction names. A stack machine names none. An accumulator machine, such as classic BPF §2.2, the predecessor of eBPF, names one: the other input and the result are always its single accumulator register. eBPF names two, and the first of them also receives the result. Instruction sets of real processors often name three: two inputs and a separate place for the result.
Limits and common mistakes
A virtual register is not a physical register. It is a numbered slot of the virtual machine. The interpreter or JIT compiler decides where its value really lives.
A register machine can still have a stack. eBPF has a block of stack memory, reached through register R10, for data that does not fit in the registers. That stack is ordinary memory, read and written with load and store instructions. It is not an operand stack.
Neither design is faster by nature. The 1993 paper that introduced classic BPF reports that its register-based filter ran up to 20 times faster than an earlier stack-based packet filter. One cause that the paper names is that the operand stack of the earlier filter had to be simulated with loads and stores to memory. That is a result about those engines and the processors of that time.
In the theory of computation, the name register machine is also used for an abstract model whose registers hold non-negative whole numbers of unlimited size. That model serves to define what can be computed at all, and it is a different subject.
Questions answered
How does a register machine differ from a stack machine?
The two machines differ in how an instruction finds its operands. An instruction of a register machine names them: it carries the numbers of the registers, which are the machine’s numbered storage slots, that hold its inputs and receive its result. An arithmetic instruction of a stack machine names nothing: it removes its inputs from the top of the operand stack, a pile of values, and puts its result back on top.
To compute , a register machine executes “put 3 in register 1, put 4 in register 2, add register 2 to register 1, multiply register 1 by 5”. A stack machine executes “push 3, push 4, add, push 5, multiply”. Both arrive at 35.
The consequences are practical. Register instructions are larger, because they carry register numbers, but fewer of them are needed, and a value can be used again without copying. Stack instructions are smaller, but every value must be pushed, and copied if it is needed twice. A program checker tracks different things: which registers hold a usable value, or how many values are on the stack. Neither design is faster in itself. The speed depends on the engine and the processor. Both designs can express the same computations.
Related entries: eBPF register convention (§2.6), Reaching definitions (§3.16), Classic BPF (cBPF) (§2.2), Virtual machine (VM) (§2.3), eBPF (§2.1). Further reading: [2, 73, 155, 160].