EIP-8024 Backwards-compatible SWAPN, DUPN, and EXCHANGE (Backward Compatible SWAPN, DUPN, EXCHANGE)

No call decision · EVM & execution · EIP status Last Call · created 2025-08-16

Adds new instructions SWAPN, DUPN, and EXCHANGE that lift the limitation of the existing SWAP and DUP instructions, which could only reach up to the 16th stack item, by duplicating or swapping deeper stack items with a single instruction.

Why it is needed

The EVM stack can hold up to 1024 values, but the existing SWAP and DUP instructions can only directly access up to the 16th item from the top. To use more local variables, a compiler has to move some values out to memory and later load them back in, which makes the code more complex and introduces inefficiency. Also, when higher-level constructs such as functions are implemented on top of the EVM, the input values, output values, and return location all get pushed onto the stack, easily exceeding 16 items, and the compiler needs extra work to arrange things so all these values remain accessible. For compilers that use stack scheduling algorithms to minimize stack movement, the ability to swap positions between items other than the top one is also important. This EIP opens a path to solving deep stack access with a single instruction.

What changes

Today

After EIP-8024

Who is affected

In depth

The three new instructions and their semantics

Three opcodes are added: DUPN (0xe6), SWAPN (0xe7), and EXCHANGE (0xe8). Each instruction is followed immediately by a 1-byte immediate operand, and using 1-indexed counting, DUPN n duplicates the nth stack item to the top, SWAPN n swaps the (n + 1)th item with the top item, and EXCHANGE n m swaps the (n + 1)th item with the (m + 1)th item. During execution, 3 gas is deducted, then the immediate is read and checked against the forbidden range, and if the decoded depth exceeds the current stack depth, execution halts with an exception. On success, pc is incremented by 2. If the immediate byte goes past the end of the code, it is evaluated as 0, just like PUSH.

Immediate encoding and the accessible range

The immediates of DUPN and SWAPN are decoded via decode_single(x) = (x + 145) % 256, returning an n between 17 and 235. Values 1 through 16 are already handled by the existing DUP1 through DUP16 and SWAP1 through SWAP16, so the new instructions cover the range beyond that. The immediate of EXCHANGE returns a pair (n, m) via decode_pair, within the range 1 <= n <= 14, n < m <= 30 - n, meaning the pair is specified within the top 30 items of the stack. Assemblers and compilers must generate immediates by inverting this via encode_single and encode_pair. Dynamically choosing the exchange target at runtime is not allowed; since static analysis of stack contents is an important tool for security audits, the immediate is forced to be a fixed value.

Backward compatibility preserved by forbidding the 0x5b and PUSH ranges

The EVM has the property that execution cannot jump into the middle of an instruction, and this was previously guaranteed by excluding PUSH's immediate from jump destination analysis (jumpdest analysis). This EIP does not change jump analysis at all; instead, if the immediate is 0x5b (JUMPDEST) or in the range 0x60 to 0x7f (PUSH1 through PUSH32), it is treated as an exception at execution time. For example, the byte sequence e6 5b was previously interpreted as INVALID JUMPDEST, which contains a valid jump destination; if jump analysis were changed to exclude the immediate, this destination would disappear and break existing contracts. Since arbitrary data can be included in code, it must be assumed that such byte sequences are already deployed. This design therefore preserves the guarantee that every execution flow that does not execute an undefined opcode behaves identically before and after this change is introduced, and that no jump destination is newly created or removed. Disassemblers should label such cases as INVALID_DUPN and similar.

The design of EXCHANGE and its offset

The previous proposal only allowed swapping two items close to each other, so while the 30th and 29th items could be swapped, bringing the 30th item up to the 2nd position was not possible in a single instruction. The current proposal prioritizes moving deep items toward the top of the stack, judging that to be more valuable. decode_pair was designed to operate using only basic arithmetic, bitwise operations, and few branches, and as a tradeoff some of the immediate range is left unused; this space can be used to add roughly a dozen more pairs in a future network upgrade. The assembly notation EXCHANGE n m actually operates on depths n + 1 and m + 1, to align with SWAP and SWAPN; EXCHANGE n m has the same effect as the three instructions SWAP{n} SWAP{m} SWAP{n}. Compilers using stack scheduling can handle this with one instruction instead of three, and since the implementation is a pointer swap, there is no additional execution cost for clients.

Rationale for the immediate size and gas cost

An approach giving DUPN and SWAPN a 16-bit immediate to cover the full 1024-item stack was also considered, but was rejected due to the added n < 1024 check and the burden of one extra byte, settling on 1 byte instead. The source material explains that even increasing from a depth of 16 to 256 is already a significant improvement. EXCHANGE adopted a 1-byte scheme covering 30 items for the same reason. The gas cost is 3, the same as the existing DUP and SWAP, because these operations are implementation-wise just pointer swaps and the immediate decoding cost is also negligible. On the security side, it states that since the stack is fixed at 1024 items and most implementations always keep it in memory, no additional risk is known beyond the increase in the number of accessible items.

Progress status

This EIP is in the Core category of the Standards Track, and at the time the source material was written its status was Last Call, with a last-call-deadline of 2026-11-01. It is not yet Final, so its content may still change during the final review process. The records this explainer draws on contain no core developer call decision to include it in a specific hard fork.

Original EIP · ← EIPs