A zero-address instruction has no explicit operand addresses, so its operands and result location must be implicit in the instruction set architecture. A stack machine supplies these naturally: operations remove their inputs from the top of an operand stack and push the result. A fixed-processor register design is logically possible, so “almost certainly” is appropriately weaker than a proof that only a stack machine could work.
Write the stack from bottom to top, with its top on the right. Let stand for any unchanged prefix. Suitable effects areThus
add pops two operands and pushes one sum, reducing stack occupancy by one. The special const c instruction pushes its immediate constant and increases occupancy by one. Only const needs an explicit value in its encoding.Use two zero-address instructions for indirect access to computer memory. Define The successive stack suffixes are , then , then , then empty. HenceThe surrounding stack is restored. Other
load to replace the top memory address by the value at that address, and define store to pop a value followed by the address just beneath it:Both have no encoded address operand; the stack supplies it. If the known fixed memory addresses of the variables are , implement the assignment withconst p_x
const p_y
load
storestore conventions are possible, but the operand order must be stated and used consistently.A computed memory address is just another integer on the operand stack. For a pointer held at fixed address ,
const p; load obtains the pointed-to address; another load obtains its contents. More generally, any expression that leaves an address on top may be followed by load, or may be retained beneath a computed value for store.For an array whose base address is and whose element size is address units, the address of entry is , where stores the index. Add zero-address instructions The first six instructions leave the element address . The remaining suffix evolves aswith the final effectThe address is computed once and duplicated, so both accesses refer to the same array element. For unit-sized elements omit
mul and dup, with dup mapping to . A suitable sequence isconst b
const p_i
load
const w
mul
add
dup
load
const 1
add
storeconst w; mul. If the base address is itself held in a variable, replace const b by the instructions loading that variable's value. No instruction other than const requires an explicit operand.Fix
sub to mean . If the depth-minimizing order evaluates the right operand before the left operand , the stack ends in , so ordinary sub would compute the wrong sign. Two possible instruction set architecture extensions are:- Add
swap, with . After computing and then , useswap; subto obtain . One general zero-address instruction handles every noncommutative binary operation and is useful elsewhere, but each reversed evaluation needs an extra instruction and extra execution time. It can be implemented without an additional operand stack slot. - Add reverse-operand variants, such as
rsubwith , and analogously reverse division and other required operations. After computing and then , a singlersubobtains . This avoids the extra executedswap, but requires more opcodes and decoder support, usually one reverse form for each affected operation.