⚠️ This EIP is not recommended for general use or implementation as it is likely to change.

EIP-663: Unlimited SWAP and DUP instructions Source

Introduce SWAPN and DUPN which take an immediate value for the depth

AuthorAlex Beregszaszi
TypeStandards Track
Requires 3540


Currently, SWAP and DUP instructions are limited to a stack depth of 16. Introduce two new instructions, SWAPN and DUPN, which lift this limitation and allow accessing the stack up to depth of 256 items.


While the stack is 1024 items deep, easy access is only possible for the top 16 items. Supporting more local variables is possible via manually keeping them in memory or through a “stack to memory elevation” in a compiler. This can result in complex and inefficient code.

Furthermore, implementing higher level constructs, such as functions, on top of EVM will result in a list of input and output parameters as well as an instruction offset to return to.

The number of these arguments (or stack items) can easily exceed 16 and thus will require extra care from a compiler to lay them out in a way that all of them are still accessible.

Introducing SWAPN and DUPN will provide an option to compilers to simplify accessing deep stack items at the price of possibly increased gas costs.


We introduce two new instructions:

  1. DUPN (0xb3)
  2. SWAPN (0xb4)

If the code is legacy bytecode, both of these instructions result in an exceptional halt. (Note: This means no change to behaviour.)

If the code is valid EOF1, the following execution rules apply:

These instructions are followed by an 8-bit immediate value, which we call imm, and can have a value of 0 to 255. We introduce the variable n which equals to imm + 1.

If the current stack depth is less than n, then a stack underflow exception is issued. If the current stack depth is at the limit, a stack overflow exception is issued. In both of these cases the EVM stops and all gas is consumed.


  • for DUPN the stack item at depth n - 1 is duplicated at the top of the stack
  • for SWAPN the top stack item is swapped with the item at depth n

The gas cost for both instructions is set at 3.



Backwards Compatibility

This has no effect on backwards compatibility because the opcodes were not previously allocated.

Test Cases


Security Considerations

The authors are not aware of any additional risks introduced here. The EVM stack is fixed at 1024 items and most implementations keep that in memory at all times. This change will increase the easy-to-access number of items from 16 to 256.

Copyright and related rights waived via CC0.


Please cite this document as:

Alex Beregszaszi, "EIP-663: Unlimited SWAP and DUP instructions [DRAFT]," Ethereum Improvement Proposals, no. 663, July 2017. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-663.