SKIP TO CONTENT
Blockchain

Opcode

Individual machine instructions that the EVM executes when running smart contract bytecode.

What is an Opcode?

An opcode (operation code) is a single instruction that the Ethereum Virtual Machine can execute. Smart contract bytecode consists of sequences of these opcodes, each performing a specific low-level operation like arithmetic, memory access, or control flow. Understanding opcodes provides insight into exactly how smart contracts execute and consume gas, making this knowledge valuable for developers optimizing contracts and auditors analyzing security.

Common EVM Opcodes

The EVM defines approximately 140 opcodes, each with a specific function and gas cost. Stack operations include PUSH (add values to stack), POP (remove values), DUP (duplicate), and SWAP (exchange positions). Arithmetic opcodes handle ADD, SUB, MUL, DIV, and more complex operations like MULMOD and ADDMOD for modular arithmetic used in cryptographic functions.

Memory and storage opcodes manage data persistence. MLOAD and MSTORE access volatile memory within a transaction, while SLOAD and SSTORE interact with permanent contract storage. The most expensive operations gas-wise. CALLDATALOAD reads input data sent with transactions. MSIZE returns the current memory size, and MCOPY (added in recent upgrades) enables efficient memory copying.

Control flow opcodes include JUMP, JUMPI (conditional jump), and JUMPDEST (valid jump destination). CALL, DELEGATECALL, and STATICCALL enable contract-to-contract interactions with different security properties and context preservation. CREATE and CREATE2 deploy new contracts with deterministic or non-deterministic addresses.

Gas Costs and Optimization

Each opcode has an associated gas cost reflecting its computational or storage requirements. Simple arithmetic operations cost 3-5 gas, while SSTORE can cost up to 20,000 gas for new storage slots. Understanding these costs helps developers write gas-efficient contracts and explains why certain operations are expensive.

Optimization techniques include minimizing storage writes, using memory efficiently, packing storage variables, and structuring code to reduce expensive operations. Some opcodes like SLOAD have been repriced through Ethereum upgrades to better reflect their actual resource consumption.

Security Considerations

Certain opcodes carry security implications. DELEGATECALL preserves the caller's context, which enables proxy patterns but can introduce vulnerabilities if misused. SELFDESTRUCT removes contracts from state but has complex edge cases and is being deprecated. CREATE2 enables deterministic contract deployment but requires careful salt management to prevent front-running.

Examples

  • SSTORE costs up to 20,000 gas to write new data to storage, while ADD costs only 3 gas

Put this term to work. Browse live yield data.

Track live yields, compare protocols, and build your DeFi portfolio with Fensory.

GET EARLY ACCESSArrow right