= Solution
A computed <memory address> is just another <integer> on the operand <stack>. For a <pointer> held at fixed address $p$, `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 $b$ and whose element size is $w$ address units, the address of entry $i$ is $b+wM[p_i]$, where $p_i$ stores the index. Add <zero-address instructions> `mul` and `dup`, with `dup` mapping $S,p$ to $S,p,p$. A suitable sequence is
``
const b
const p_i
load
const w
mul
add
dup
load
const 1
add
store
``
The first six instructions leave the element address $p=b+wM[p_i]$. The remaining suffix evolves as
$$
S,p\longmapsto S,p,p\longmapsto S,p,M[p]\longmapsto S,p,M[p],1\longmapsto S,p,M[p]+1\longmapsto S,
$$
with the final effect
$$
\boxed{M[b+wM[p_i]]\gets M[b+wM[p_i]]+1.}
$$
The address is computed once and duplicated, so both accesses refer to the same <array> element. For unit-sized elements omit `const 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.
Back to article page