Supported Opcodes
| Opcode | Hex | Stack Input | Stack Output | Description |
|---|---|---|---|---|
| SLOAD | 0x54 | key | value | Load value from storage |
| SSTORE | 0x55 | key, value | - | Store value to storage |
Storage Model
EVM storage has these characteristics:- Key-value store: Each contract has 2^256 storage slots
- Persistent: Data survives contract execution and is saved on-chain
- Initially zero: All slots start with value 0
- Word-sized: Each slot holds a 32-byte (256-bit) value
- Expensive: Storage operations are costly compared to memory
SSTORE (0x55)
Stores a 32-byte value to a storage slot. Stack Input:key: Storage slot to write tovalue: 32-byte value to store
SLOAD (0x54)
Loads a 32-byte value from a storage slot. Stack Input:key: Storage slot to read from
value: 32-byte value from storage
Combined Example
This example shows storing and loading in one operation:Implementation
Storage operations are implemented insrc/vm.rs:298-310:
Storage vs Memory
| Feature | Storage (SLOAD/SSTORE) | Memory (MLOAD/MSTORE) |
|---|---|---|
| Persistence | Persistent (saved on-chain) | Volatile (cleared after execution) |
| Cost | Very expensive | Cheaper |
| Use Case | Long-term state, contract data | Temporary calculations |
| Access | 2^256 key-value slots | Linear byte array |
| Initial Value | All zeros | All zeros |
Storage Patterns
Simple state variable:Common Storage Slots
In Solidity, storage slots are assigned sequentially:- Slot 0: First state variable
- Slot 1: Second state variable
- Slot 2: Third state variable
- etc.
Gas Considerations
Best Practices
- Minimize writes: SSTORE is expensive
- Batch updates: Change storage in groups when possible
- Pack data: Use bit manipulation to store multiple values in one slot
- Clear unused data: Set to zero for gas refunds in real EVM
- Use memory for temps: Don’t store temporary values in storage