This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0x81Introduced: 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:
[..., value, item1, ..., item1]
Stack Output:
[..., value, item1, ..., item1, value]
Gas Cost: 3 (GasFastestStep)Operation:
value = stack[depth - 2]stack.push(value)
Behavior
DUP2 copies the 2th-from-top stack item without removing it. Requires stack depth ≥ 2.Key characteristics:
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
assembly { let a := 10 let b := 20 dup2 // Get 'a' again // Stack: [b, a, a]}```### Efficient Copies```solidity// Instead of multiple loadsassembly { let value := sload(slot) // Expensive // Use value let value2 := sload(slot) // Wasteful!}// Use DUP to reuseassembly { let value := sload(slot) // Load once dup1 // Copy // Use both copies}
Conditional Logic
assembly { let condition := calldataload(0) dup1 // Keep condition for later iszero jumpi(skip) // Use condition again skip:}
Stack Depth Requirements
Minimum Depth
// DUP2 requires 2 items on stackassembly { push1 0x01 // Only 1 items - DUP2 will fail! dup2 // StackUnderflow}