[step:Implement each subroutine and assemble the machine]
We detail the register-machine realisation of the algorithm.
*Register layout.* Input registers $0, \ldots, k-1$ hold $w_1, \ldots, w_k$; register $k$ holds $v$. Fresh scratch registers:
- $R_0, \ldots, R_{k-1}$: master copies of $w$, preserved across loop iterations.
- $R_v$: master copy of $v$.
- $R_u$: the loop counter $u$.
- $R_c$: the current configuration code $c$.
- $R_\text{st}$: scratch for the extracted state field.
- $R_{\text{out}}$: output staging (written to register $0$ at the end).
*Phase 0: initialisation.*
- Copy $w_1, \ldots, w_k$ into $R_0, \ldots, R_{k-1}$, and copy $v$ into $R_v$ (these master copies will be restored before each subroutine call that destructively reads its input registers).
- Erase $R_u$ (set $u := \varepsilon$).
*Phase 1: the main loop body.*
(1a) *Compare $u$ to $v$.* Copy $R_u$ into input register $0$ of the shortlex-comparison machine $M_<$ (renamed to avoid register clashes), copy $R_v$ into input register $1$, and run $M_<$. By [Shortlex Order and Computable Successor](/theorems/1815), $M_<$ halts in finite time with output register $0$ holding $a$ iff $u < v$, and $\varepsilon$ otherwise. Read the output: if it is $\varepsilon$, branch to Phase 3 with output $\varepsilon$; if it is $a$, continue.
(1b) *Compute $c := h_M(w, u)$.* By [Computability of the Configuration Function](/theorems/1818), there is a register machine $M_h$ that computes $h$ on input $(\operatorname{code}(M), \operatorname{code}(w), u)$. We specialise it to fixed $M$: the first input is always the literal $\operatorname{code}(M)$ (a hard-wired finite word), and the second input is $\operatorname{code}(w) = \sharp w_1 \sharp w_2 \sharp \cdots \sharp w_k \sharp$ built at runtime from $R_0, \ldots, R_{k-1}$ via a fixed sequence of "write $\sharp$" and "copy from $R_i$" instructions (the sequence is finite because $k$ is the fixed arity). The third input is $u$ from $R_u$.
Concretely, restore the three input registers of $M_h$: write $\operatorname{code}(M)$ as a literal into input register $0$ of $M_h$ (a finite sequence of "write letter" instructions, one per letter of $\operatorname{code}(M)$); assemble $\operatorname{code}(w)$ into input register $1$ of $M_h$ as above; copy $R_u$ into input register $2$ of $M_h$. Run $M_h$. It halts in finite time (its divergent branches are triggered only by ill-formed inputs, and our inputs are well-formed by construction). Copy its output into $R_c$.
(1c) *Extract the state field of $c$.* The encoding is $\operatorname{code}(C) = \operatorname{code}(q) \cdot | \cdot \sharp r_0 \sharp \cdots \sharp r_{N-1} \sharp$. Scan $R_c$ from the left (using the reversal trick to read left-to-right as in the proof of [Regular Languages are Computable](/theorems/1814)), copying letters one at a time into $R_\text{st}$ until the separator $|$ is read; discard the $|$ and do not copy any further letters. The register $R_\text{st}$ now holds $\operatorname{code}(q)$ where $q$ is the current state of $C = C(M, w, \#u)$.
(1d) *Compare $\operatorname{code}(q)$ to $\operatorname{code}(q_H)$.* Since $\operatorname{code}(q_H)$ is a fixed finite word known at construction time (because $M$ is fixed), word-equality with $\operatorname{code}(q_H)$ is a finite regular-language test: the test is equivalent to "$\operatorname{code}(q)$ belongs to the singleton language $\{\operatorname{code}(q_H)\}$", which is regular. By [Regular Languages are Computable](/theorems/1814), word-equality with a fixed word is computable; a direct letter-by-letter comparison machine also works (read letters of $R_\text{st}$ and compare with the hard-wired literal sequence from $\operatorname{code}(q_H)$, branching on any mismatch or on a length mismatch).
The test yields: if $\operatorname{code}(q) = \operatorname{code}(q_H)$, branch to Phase 2 with output $a$; otherwise, continue.
(1e) *Advance $u := s(u)$.* Copy $R_u$ into the input register of the successor machine $M_s$ of [Shortlex Order and Computable Successor](/theorems/1815), run $M_s$, copy its output back into $R_u$. By the cited theorem, $s(u)$ is the shortlex-successor of $u$, and $\#s(u) = \#u + 1$. Return to step (1a).
*Phase 2: emit $a$.* Erase register $0$, write $a$ to register $0$, halt.
*Phase 3: emit $\varepsilon$.* Erase register $0$, halt.
[claim:The loop terminates after exactly $\min(\#v, \#v^\star)$ iterations, where $\#v^\star$ is the least $n$ with $C(M, w, n)$ in state $q_H$ (or $+\infty$ if no such $n$ exists within $\#v$ steps), and the output is $a$ iff such an $n$ exists]
Let us track the value of $u$ across iterations. Initially $u = \varepsilon$ and $\#u = 0$. After $t$ successful completions of step (1e) without early exit, $u = s^t(\varepsilon)$ and $\#u = t$, by $t$-fold application of $\#s = \# + 1$.
At each iteration the loop either (A) exits through (1a) when $\#u \ge \#v$, or (B) exits through (1d) when the state of $C(M, w, \#u)$ is $q_H$, or (C) continues.
- If there is no $n < \#v$ with $C(M, w, n)$ in state $q_H$, then (B) never triggers. The loop runs iterations $t = 0, 1, 2, \ldots$ with $\#u = t$ at the start of each. Iteration $t = \#v$ has $\#u = \#v$, so (1a) fires and the loop exits via (A) with output $\varepsilon$. The output matches the specification.
- If there is some $n < \#v$ with $C(M, w, n)$ in state $q_H$, let $n^\star$ be the least such. At iteration $t = n^\star$, $\#u = n^\star < \#v$, so (1a) continues; (1b) computes $c = \operatorname{code}(C(M, w, n^\star))$, whose state field is $\operatorname{code}(q_H)$ (by the choice of $n^\star$); (1d) triggers the $a$ branch, and the output is $a$. The output matches the specification.
In either case the loop runs at most $\#v$ iterations, each iteration consists of a bounded number of subroutine calls (each halting in finite time because the called machines $M_<$, $M_h$, $M_s$ always halt on their valid inputs), so the total number of steps is finite. The machine halts on every input.
[/claim]
[/step]