Section 2.5

Register machine

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 (3+4)×5(3 + 4) \times 5, 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.

Worked example

Compute (3+4)×5(3 + 4) \times 5 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 3+4=73 + 4 = 7 and 7×5=357 \times 5 = 35, 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 4×8=324 \times 8 = 32 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 3−4=−13 - 4 = -1.

Stated precisely

Write R[i]R[i] for the value in register number ii, and pc\mathrm{pc} for the position of the next instruction. Leave memory aside. The state of a register machine is then the pair (pc,R)(\mathrm{pc}, R). The eBPF instruction that adds register number ss to register number dd changes the state as follows: R[d]R[d] becomes the low 64 bits of R[d]+R[s]R[d] + R[s], pc\mathrm{pc} becomes pc+1\mathrm{pc} + 1, and every other register keeps its value.

The state of a stack machine is the pair (pc,S)(\mathrm{pc}, S), where SS is a sequence of values whose last member is the top. The instruction add is defined only when SS holds at least two values. If the two top values are aa, pushed first, and bb, pushed second, then add replaces both by the single value a+ba + b, and pc\mathrm{pc} becomes pc+1\mathrm{pc} + 1.

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 (3+4)×5(3 + 4) \times 5, 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].