EOF introduces new stack instructions with immediate operands: DUPN
, SWAPN
, and EXCHANGE
. The operands determine what elements of the stack will be accessed and manipulated.
There's no standard or obvious way in which these operands should be represented in human-readable assembly syntax, and different developers may come up with different solutions. If this ends up being subtly different across different tools (e.g., off by one), an assembly instruction may have different behavior depending on the syntax variant it's read as, and people or tools may be misled, which could be used to conceal malicious behavior in the worst case. Additionally, lack of clarity in communication about these opcodes could result in bugs in various places.
Because of this, we should standardize the representation of the operands of each of these instructions.
I'll describe three alternative approaches:
DUP*
and SWAP*
instructions.The final choice could be a combination of these approaches.
The notation <bytecode>
<-> <assembly>
means that <bytecode>
disassembles to <assembly>
and that <assembly>
assembles to <bytecode>
.
I'll use a space to separate an opcode from its immediate operand, like DUPN 4
, but the discussion should apply equally to different syntax, like DUPN[4]
.
DUPN
and SWAPN
Running examples and their effect described in 1-based indexing:
e6 03
: DUPN
instruction that duplicates the 4th stack element.e7 03
: SWAPN
instruction that swaps the 5th stack element with the 1st.Examples:
e6 03
<-> DUPN 3
e7 03
<-> SWAPN 3
According to this approach, for example, DUPN 4
should be functionally equivalent to DUP4
, and SWAPN 4
should be functionally equivalent to SWAP4
.
The assembly operand is thus the numerical value in the bytecode plus one.
Examples:
e6 03
<-> DUPN 4
e7 03
<-> SWAPN 4
In this approach, the operand of SWAPN
is encoded differently than the operand of DUPN
. Immediates that are equal in bytecode are not numerically equal in assembly.
Examples:
e6 03
<-> DUPN 4
e7 03
<-> SWAPN 5
Note that thus SWAPN 5
is functionally equivalent to SWAP4
.
EXCHANGE
Exchange has a single-byte immediate, but conceptually two nibble-sized operands.
It can be confusing, so I'll use one running example, e8 13
: the EXCHANGE
instruction with the effect of swapping the 3rd stack element with the 7th (in 1-based indexing).
Visualizing the stack: a b C d e f G h …
-> a b G d e f C h …
There are two ways to interpret the verbatim approach.
e8 13
<-> EXCHANGE 0x13
(in decimal EXCHANGE 19
)e8 13
<-> EXCHANGE 1 3
There is no direct equivalent to EXCHANGE
prior to EOF, so this alternative proposes to draw an equivalence with a sequence of SWAP
or SWAPN
instructions: EXCHANGE n m
should be functionally equivalent to SWAPN n SWAPN m SWAPN n
. For example, EXCHANGE 2 6
should be equivalent to SWAP2 SWAP6 SWAP2
.
Example:
e8 13
<-> EXCHANGE 2 6
Example:
e8 13
<-> EXCHANGE 3 7