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:0x80Introduced: Frontier (EVM genesis)DUP1 duplicates the top stack item and pushes it to the top of the stack. The original item remains in place.
Specification
Stack Input:
[..., value]
Stack Output:
[..., value, value]
Gas Cost: 3 (GasFastestStep)Operation:
value = stack[depth - 1]stack.push(value)
Behavior
DUP1 copies the top stack item without removing it. Requires stack depth ≥ 1.Key characteristics:
Requires stack depth ≥ 1
Original value unchanged
New copy pushed to top
StackUnderflow if depth < 1
Stack depth increases by 1
Examples
Basic Usage
import { handler_0x80_DUP1 } from '@tevm/voltaire/evm/stack/handlers';import { createFrame } from '@tevm/voltaire/evm/Frame';// Duplicate top itemconst frame = createFrame({ stack: [100n], gasRemaining: 1000n});const err = handler_0x80_DUP1(frame);console.log(frame.stack); // [100n, 100n] - top duplicatedconsole.log(frame.gasRemaining); // 997n (3 gas consumed)
Solidity Compilation
contract Example { function reuse() public pure returns (uint256, uint256) { uint256 x = 42; // Need x twice return (x, x); // Compiler uses DUP1 // PUSH1 0x2a // DUP1 // DUP1 }}
Cost: 3 gas (GasFastestStep)All DUP1-16 operations cost the same despite different stack depths accessed.Comparison:
Operation
Gas
Note
DUP1
3
Duplicate 1th item
PUSH1-32
3
Same cost tier
POP
2
Cheaper
Common Usage
Value Reuse
assembly { let x := 100 dup1 // Reuse x dup1 // Reuse again // Stack: [100, 100, 100] add // Use first two // Stack: [200, 100]}```### 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
// DUP1 requires 1 items on stackassembly { // Only 0 items - DUP1 will fail! dup1 // StackUnderflow}