Overview
Opcode:0x98
Introduced: Frontier (EVM genesis)
SWAP9 exchanges the top stack item with the 10th item from the top. Only these two positions change - all other items remain in place.
Specification
Stack Input:Behavior
SWAP9 exchanges positions of the top item and the item at position 10 from top. Requires stack depth ≥ 10. Key characteristics:- Requires stack depth ≥ 10
- Only two items change positions
- Middle items (items 1-9) unchanged
- StackUnderflow if depth < 10
- Stack depth unchanged
Examples
Basic Usage
Solidity Compilation
Assembly Usage
Gas Cost
Cost: 3 gas (GasFastestStep) All SWAP1-16 operations cost the same despite different stack depths accessed. Comparison:| Operation | Gas | Note |
|---|---|---|
| SWAP9 | 3 | Swap with 10th item |
| DUP1-16 | 3 | Same cost tier |
| POP | 2 | Cheaper |
Common Usage
Deep Stack Manipulation
Storage Optimization
Stack Depth Requirements
Minimum Depth
Safe Usage
Implementation
- TypeScript
Edge Cases
Stack Underflow
Out of Gas
Identity Swap
Maximum Values
References
- Yellow Paper - Section 9.1 (Stack Operations)
- EVM Codes - SWAP9
- Solidity Assembly - swap9

