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: 0x5e
Introduced: Cancun (EIP-5656)
Deprecated: Never
MCOPY copies a region of memory from source to destination. It handles overlapping regions correctly using an internal temporary buffer. This is the first memory-to-memory copy opcode in the EVM, replacing manual byte-by-byte loops with a single atomic operation.
Before Cancun, copying memory required loops with MLOAD/MSTORE or MSTORE8, which was inefficient and error-prone for overlapping regions.
Specification
Stack Input:
Stack Output:
Gas Cost: 3 + memory expansion cost + copy cost
Copy cost formula:
Operation:
Behavior
MCOPY pops three values from stack: dest (top), src (middle), len (bottom). It copies len bytes from src to dest, expanding memory as needed.
- All addresses interpreted as unsigned 256-bit integers
- Copy handles overlapping regions correctly (atomic, not in-place)
- Memory expansion covers both source AND destination ranges
- Zero-length copy (len=0) charges only base gas, no expansion
- Expansion cost quadratic; copy cost linear in words
Stack order note: Different from most opcodes - destination popped first.
Examples
Basic Copy
Zero-Length Copy
Forward Overlap (Non-Destructive)
Backward Overlap (Non-Destructive)
Exact Overlap (Same Source and Destination)
Large Copy
Gas Cost
Base cost: 3 gas
Memory expansion: Quadratic, covers max(src+len, dest+len)
Copy cost: 3 gas per word (rounded up)
Formula:
Examples:
- Copy 32 bytes (1 word), no expansion: 3 + 0 + 3 = 6 gas
- Copy 64 bytes (2 words), no expansion: 3 + 0 + 6 = 9 gas
- Copy 33 bytes (2 words, rounded up): 3 + 0 + 6 = 9 gas
- Copy with large expansion: 3 + exp(max_range) + copy_cost
Zero-length copy charges only 3 gas (no expansion, no copy cost).
Hardfork Availability
MCOPY is only available on Cancun and later:
Attempting MCOPY on pre-Cancun chains reverts with InvalidOpcode.
Edge Cases
Uninitialized Source
Massive Offset
Out of Gas During Expansion
Stack Underflow
Common Usage
Copy Constructor Arguments
Cache Optimization
Memory Consolidation
Memory-to-Memory Transfer
Memory Safety
Copy safety properties:
- Atomic: Entire copy completes without intermediate states visible
- Non-destructive for overlaps: Uses temporary buffer internally
- Initialization: Uninitialized source reads as zero
- No side effects: Doesn’t affect storage or state
Memory layout considerations:
Implementation
Testing
Test Coverage
Edge Cases Tested
- Basic copy (32 bytes)
- Zero-length copy
- Forward overlap
- Backward overlap
- Exact overlap (src == dest)
- Uninitialized source (zeros)
- Memory expansion
- Copy cost calculation
- Large copies (256+ bytes)
- Hardfork checking
- Stack underflow/overflow
- Out of gas conditions
Security Considerations
Overlap Handling
MCOPY correctly handles all overlap scenarios with internal buffering:
Memory Exhaustion
Large copies can trigger quadratic memory expansion costs:
Benchmarks
MCOPY efficiency gains over manual loops:
MLOAD/MSTORE loop (32 bytes):
MCOPY (32 bytes):
3x more efficient for single-word copies.
Gains increase for larger copies due to reduced opcode overhead.
References