Overview
Opcode:0x81
Introduced: Frontier (EVM genesis)
DUP2 duplicates the 2nd stack item and pushes it to the top of the stack. The original 2th item remains in place.
Specification
Stack Input:Behavior
DUP2 copies the 2th-from-top stack item without removing it. Requires stack depth ≥ 2. Key characteristics:- Requires stack depth ≥ 2
- Original value unchanged
- New copy pushed to top
- StackUnderflow if depth < 2
- Stack depth increases by 1
Examples
Basic Usage
Solidity Compilation
Assembly Usage
Gas Cost
Cost: 3 gas (GasFastestStep) All DUP1-16 operations cost the same despite different stack depths accessed. Comparison:| Operation | Gas | Note |
|---|---|---|
| DUP2 | 3 | Duplicate 2th item |
| PUSH1-32 | 3 | Same cost tier |
| POP | 2 | Cheaper |
Common Usage
Access Previous Value
Conditional Logic
Stack Depth Requirements
Minimum Depth
Safe Usage
Implementation
- TypeScript
Edge Cases
Stack Underflow
Stack Overflow
Out of Gas
Maximum Value
References
- Yellow Paper - Section 9.1 (Stack Operations)
- EVM Codes - DUP2
- Solidity Assembly - dup2

