- Published on
Understanding Ethereum Virtual Machine (EVM) Basics
- Authors
- Name
- Frank
Understanding the Ethereum Virtual Machine (EVM)
The Ethereum Virtual Machine (EVM) is the heart of Ethereum. It's a decentralized computing engine that powers smart contracts and manages blockchain state transitions. This article breaks down what the EVM is and how it works.
What is the EVM?
- Computation Engine: The EVM is Ethereum's computation engine, similar to interpreters in other bytecode-compiled languages like Java.
- Smart Contract Execution: It handles the deployment and execution of smart contracts, which are self-executing programs on the blockchain.
- State Updates: Nearly everything that happens on Ethereum involves a state update computed by the EVM.
- Global Decentralized Computer: Think of the EVM as a global decentralized computer containing millions of executable objects (smart contracts), each with its own permanent data store.
Ethereum as a State Machine
- Transaction-Based State Machine: Ethereum can be described as a transaction-based state machine.
- EVM's Job: The EVM's job is to update Ethereum's state by computing valid state transitions resulting from smart contract execution.
World State
The Ethereum state consists of a mapping of Ethereum addresses (160-bit values) to Account objects. There are two types of accounts:
- Externally Owned Accounts (EOAs)
- Balance: Holds an amount of Ether (in Wei).
- Nonce: Counts the number of transactions sent from this address.
- Storage and Code: EOAs have no storage or associated code.
- Smart Contracts
- Balance: Holds Ether (in Wei).
- Nonce: Counts the number of contract creations.
- Storage: Contains the contract's permanent data store.
- Program Code: Contains the smart contract's code, which the EVM executes.
EVM Instruction Set - Bytecode Operations
The EVM uses a low-level, stack-based language consisting of various opcodes. Below is a breakdown of the main categories of EVM instructions:
Arithmetic Operations
- ADD - Add the top two stack items.
- MUL - Multiply the top two stack items.
- SUB - Subtract the top two stack items.
- DIV - Integer division.
- SDIV - Signed integer division.
- MOD - Modulo (remainder) operation.
- SMOD - Signed modulo operation.
- ADDMOD - Addition modulo any number.
- MULMOD - Multiplication modulo any number.
- EXP - Exponential operation.
- SIGNEXTEND - Extend the length of a two's complement signed integer.
- SHA3 - Compute the Keccak-256 hash of a block of memory.
- Note: All arithmetic is performed modulo 2^256.
Stack Operations
- POP - Remove the top item from the stack.
- MLOAD - Load a word from memory.
- MSTORE - Save a word to memory.
- MSTORE8 - Save a byte to memory.
- SLOAD - Load a word from storage.
- SSTORE - Save a word to storage.
- MSIZE - Get the size of active memory in bytes.
- PUSHx - Place x byte item on the stack (x = 1 to 32).
- DUPx - Duplicate the x-th stack item (x = 1 to 16).
- SWAPx - Exchange the 1st and (x+1)-th stack items (x = 1 to 16).
Process Flow Operations
- STOP - Halt execution.
- JUMP - Set the program counter to a specific value.
- JUMPI - Conditionally alter the program counter.
- PC - Get the value of the program counter.
- JUMPDEST - Mark a valid destination for jumps.
System Operations
- LOGx - Append a log record with x topics (x = 0 to 4).
- CREATE - Create a new account with associated code.
- CALL - Message-call into another account.
- CALLCODE - Message-call into this account with another account's code.
- RETURN - Halt execution and return output data.
- DELEGATECALL - Message-call into this account with alternative account's code.
- STATICCALL - Static message-call into an account.
- REVERT - Halt execution, revert state changes, return data and remaining gas.
- INVALID - Designated invalid instruction.
- SELFDESTRUCT - Halt execution and register account for deletion.
Logic Operations
- LT - Less-than comparison.
- GT - Greater-than comparison.
- SLT - Signed less-than comparison.
- SGT - Signed greater-than comparison.
- EQ - Equality comparison.
- ISZERO - Simple NOT operator.
- AND - Bitwise AND operation.
- OR - Bitwise OR operation.
- XOR - Bitwise XOR operation.
- NOT - Bitwise NOT operation.
- BYTE - Retrieve a single byte from a full-width word.
Environmental Operations
- GAS - Get the amount of available gas.
- ADDRESS - Get the address of the currently executing account.
- BALANCE - Get the account balance of any given account.
- ORIGIN - Get the address of the EOA that initiated this EVM execution.
- CALLER - Get the address of the caller immediately responsible for this execution.
- CALLVALUE - Get the Ether amount deposited by the caller.
- CALLDATALOAD - Get the input data sent by the caller.
- CALLDATASIZE - Get the size of the input data.
- CALLDATACOPY - Copy the input data to memory.
- CODESIZE - Get the size of code running in the current environment.
- CODECOPY - Copy the code running in the current environment to memory.
- GASPRICE - Get the gas price specified by the originating transaction.
- EXTCODESIZE - Get the size of any account's code.
- EXTCODECOPY - Copy any account's code to memory.
- RETURNDATASIZE - Get the size of the output data from the previous call.
- RETURNDATACOPY - Copy data output from the previous call to memory.
Block Operations
- BLOCKHASH - Get the hash of one of the 256 most recently completed blocks.
- COINBASE - Get the block's beneficiary address.
- TIMESTAMP - Get the block's timestamp.
- NUMBER - Get the block's number.
- DIFFICULTY - Get the block's difficulty.
- GASLIMIT - Get the block's gas limit.
My shorthand notes were the source material for this article produced by generative AI.