# With a list of opcodes for a virtual machine, is it possible to assign a number to every possible program of size n that can be run on that machine?

Say you have some virtual machine with < 100 numbered opcodes that fully specify its behaviour. Is there an easy way to order and enumerate all possible programs of less than n instructions, the result being that a particular number refers to a particular program? Preferably with natural numbers and short programs at the begining.

As this list would be combinatorially large, it would be helpful to assign these numbers without running the programs or loading them into memory.

• Say $m$ is the number of opcodes, then the natural thing would be to encode programs with at most $n$ commands as $n$-length strings over the alphabet $\{0,...,m-1\}$. This is equivalent to considering $n$ digit numbers in base $m$, or natural numbers from $0$ to $m^n-1$ where the number $k$ represents the program obtained from the base $m$ representation of $k$. – Ariel Apr 1 at 10:57
• @Ariel Write as an answer? – Yuval Filmus Apr 1 at 13:34
• Perhaps later, feel free! – Ariel Apr 1 at 13:55

If you have $$m$$ opcodes and your program has $$n$$ instructions, then it can be interpreted as an $$n$$-"digit" number written in base $$m$$.