---
eip: 8024
label: EIP-8024
title: Backward compatible SWAPN, DUPN, EXCHANGE
status: Last Call
type: Standards Track
category: Core
created: 2025-08-16
official: https://eips.ethereum.org/EIPS/eip-8024
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-8024.md
canonical: https://eips.huatuo.xyz/eip-8024/
lang: en
tldr: "Adds DUPN, SWAPN and EXCHANGE opcodes so compilers can reach stack items deeper than 16, using a clever immediate encoding that keeps existing bytecode's jump targets unchanged."
tags: [evm, opcodes, stack, compilers, core]
---

# EIP-8024: Backward compatible SWAPN, DUPN, EXCHANGE

> Introduce additional instructions for manipulating the stack which allow accessing the stack at higher depths

**TL;DR:** Adds DUPN, SWAPN and EXCHANGE opcodes so compilers can reach stack items deeper than 16, using a clever immediate encoding that keeps existing bytecode's jump targets unchanged.

## What problem does it solve?

The EVM stack holds up to 1024 items, but `DUP1`–`DUP16` and `SWAP1`–`SWAP16` can only reach the top 16. Anyone who has seen Solidity's "stack too deep" error has hit this limit. Compilers work around it by spilling variables to memory, which makes code bigger, slower and harder to analyse. Reordering items for the next operation also often takes several instructions (`SWAP2 SWAP3 SWAP2`) when the client could do it with one pointer swap.

Similar opcodes were proposed before for EOF ([EIP-663](/eip-663/)), which relied on EOF's bytecode validation. EIP-8024 brings them to the existing EVM without EOF.

## How it works

Three opcodes, each followed by a one-byte immediate. Each costs 3 gas, the same as `DUP`/`SWAP`:

| Opcode | Byte | Effect |
| --- | --- | --- |
| `DUPN n` | `0xe6` | Push a copy of the `n`th item (n = 17…235) |
| `SWAPN n` | `0xe7` | Swap the top with item `n + 1` (n = 17…235) |
| `EXCHANGE n m` | `0xe8` | Swap items `n + 1` and `m + 1` (1 ≤ n < m, n + m ≤ 30) |

**The backward-compatibility trick.** Old bytecode may contain the bytes `e6 5b` (`INVALID JUMPDEST`). If the new opcodes simply treated the next byte as data, that `JUMPDEST` would stop being a valid jump target and existing contracts could break. So jump-destination analysis is left unchanged, and immediates that would be `0x5b` (`JUMPDEST`) or `0x60`–`0x7f` (`PUSH1`–`PUSH32`) are forbidden. Executing such a byte halts exceptionally. To avoid wasting the useful range, the raw byte is mapped to `n` with modular arithmetic:

```python
def decode_single(x):          # DUPN / SWAPN
    assert 0 <= x <= 90 or 128 <= x <= 255
    return (x + 145) % 256     # 17..235

def encode_single(n):
    assert 17 <= n <= 235
    return (n + 111) % 256
```

`EXCHANGE` packs two indices into one byte with a separate `decode_pair` / `encode_pair` scheme, defined in the spec.

## Sample code

Test vectors from the spec (hex bytecode → disassembly):

```text
e680      DUPN 17
e7db      SWAPN 108
e89d      EXCHANGE 2 3
e82f      EXCHANGE 1 19
e6805b    DUPN 17, JUMPDEST
e75b      INVALID_SWAPN, JUMPDEST   ; 0x5b immediate is not allowed
e6605b    INVALID_DUPN, PUSH1 0x5b  ; PUSH range is not allowed
```

Execution example: `600060016002e88e` pushes 0, 1, 2 and then `EXCHANGE 1 2` (immediate `0x8e`), leaving the stack top-to-bottom as `[2, 0, 1]`.

## Things to know

- **Status: Last Call.** The spec itself does not name an activation fork. Check the fork meta-EIPs before relying on it.
- `DUPN`/`SWAPN` start at depth 17 because 1–16 are already covered by `DUP1`–`DUP16`/`SWAP1`–`SWAP16`.
- Operands are immediate, never taken from the stack, on purpose: dynamic stack access would make static analysis and auditing much harder.
- Disassemblers must decode an invalid immediate as `INVALID_*` followed by the next byte as its own instruction.
- `EXCHANGE n m` is defined to equal `SWAPn SWAPm SWAPn`, which explains its "off by one" operands.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-8024
