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: 0x51
Introduced: Frontier (EVM genesis)
MLOAD reads a 32-byte word from memory at the specified offset. The value is interpreted as a big-endian 256-bit unsigned integer and pushed to the stack. Uninitialized memory reads as zero.
This is the primary mechanism for reading arbitrary data from memory during execution.
Specification
Stack Input:
Stack Output:
Gas Cost: 3 + memory expansion cost
Operation:
Behavior
MLOAD pops an offset from the stack, reads 32 bytes starting at that offset, and pushes the result as a 256-bit value.
- Offset is interpreted as unsigned 256-bit integer (max 2^256 - 1)
- Reads exactly 32 bytes (1 word)
- Uninitialized bytes read as 0x00
- Memory automatically expands to accommodate read (quadratic cost)
- Bytes are combined in big-endian order (byte 0 = most significant)
Examples
Basic Load
Load from Uninitialized Memory
Load with Non-Zero Offset
Multiple Reads
Gas Cost
Base cost: 3 gas (GasFastestStep)
Memory expansion: Quadratic based on access range
Formula:
Examples:
- Reading bytes 0-31: 1 word, no prior expansion: 3 gas
- Reading bytes 1-32: 2 words (rounds up), 1 word prior: 3 + (4 - 1) = 6 gas
- Reading bytes 0-4095: ~125 words: 3 + (125² / 512 + expansion) ≈ 3 + 30 = 33 gas
Memory is expensive for large accesses due to quadratic expansion formula.
Edge Cases
Byte Alignment
Maximum Offset
Out of Bounds
Stack Underflow
Common Usage
Loading ABI-Encoded Data
Reading Function Parameters
Iterating Memory
Memory Safety
Load safety properties:
- No side effects: Reading memory never modifies state or storage
- Initialization: Uninitialized memory safely reads as zero
- Bounds: Out-of-bounds reads don’t error - they just allocate and charge gas
- Atomicity: 32-byte load is atomic (no tearing)
Applications must ensure offset validity:
Implementation
Testing
Test Coverage
Edge Cases Tested
- Basic load (32 bytes)
- Uninitialized memory (zeros)
- Non-zero offset with word boundary alignment
- Memory expansion costs
- Stack underflow/overflow
- Out of gas conditions
- Endianness verification
Security Considerations
Memory Disclosure
Memory is transaction-scoped and doesn’t persist to state. However, careful handling needed:
Out-of-Bounds Reads
Memory expands automatically - reading beyond allocated areas is safe but expensive:
Benchmarks
MLOAD is among the fastest EVM operations:
Relative performance:
- MLOAD (initialized): 1.0x baseline
- MLOAD (uninitialized): 1.0x baseline
- MSTORE: 1.0x (similar cost)
- SLOAD: 100x slower (storage vs memory)
Gas scaling:
- First word: 3 gas
- Second word: 3 gas (expansion ≈ 3)
- Large memory: Quadratic scaling beyond practical use
References