Back
Featured image of post Understanding EVM: an introduction to Ethereum OPCODES

Understanding EVM: an introduction to Ethereum OPCODES

Understanding EVM internal design. Learning bytecode design and OPCODE definitions

Table of Content

The Ethereum Virtual Machine or EVM

The Ethereum Virtual Machine is the software platform that developers can use to create decentralized applications (DApps) on Ethereum. At any given block in the chain, Ethereum has one and only one ‘canonical’ state, and the EVM is what defines the rules for computing a new valid state from block to block.

Diagram of Ethereum EVM internals

The EVM design simplified

The EVM behaves as a mathematical function would: Given an input, it produces a deterministic output. It therefore is quite helpful to more formally describe Ethereum as having a state transition function:

$$ Y(S, T)= S’ $$

Given an old valid state $(S)$ and a new set of valid transactions $(T)$, the Ethereum state transition function $Y(S, T)$ produces a new valid output state $S'$

State

In the context of Ethereum, the state is an enormous data structure called a modified Merkle Patricia Trie, which keeps all accounts linked by hashes and reducible to a single root hash stored on the blockchain. Transactions

Transactions

Transactions are cryptographically signed instructions from accounts. There are two types of transactions: those which result in message calls and those which result in contract creation.

Contract creation results in the creation of a new contract account containing compiled smart contract bytecode. Whenever another account makes a message call to that contract, it executes its bytecode.

EVM instructions

The EVM executes as a stack machine with a depth of 1024 items. Each item is a 256-bit word, which was chosen for the ease of use with 256-bit cryptography (such as Keccak-256 hashes or secp256k1 signatures).

During execution, the EVM maintains a transient memory (as a word-addressed byte array), which does not persist between transactions.

Contracts, however, do contain a Merkle Patricia storage trie (as a word-addressable word array), associated with the account in question and part of the global state.

Compiled smart contract bytecode executes as a number of EVM opcodes, which perform standard stack operations like XOR, AND, ADD, SUB, etc. The EVM also implements a number of blockchain-specific stack operations, such as ADDRESS, BALANCE, BLOCKHASH, etc.

So, we could define the EVM as a set of rules that allow us to write content in the blockchain according to conditions of the smart contracts.

It is important to note that, All implementations of the EVM must adhere to the specification described in the Ethereum Yellowpaper.

Ethereum OPCODE list

Remember that: The size of a “word” in EVM is 256 bits.

OpcodeNameDescriptionExtra InfoGas
0x00STOPHalts execution-0
0x01ADDAddition operation-3
0x02MULMultiplication operation-5
0x03SUBSubtraction operation-3
0x04DIVInteger division operation-5
0x05SDIVSigned integer division operation (truncated)-5
0x06MODModulo remainder operation-5
0x07SMODSigned modulo remainder operation-5
0x08ADDMODModulo addition operation-8
0x09MULMODModulo multiplication operation-8
0x0aEXPExponential operation-10*
0x0bSIGNEXTENDExtend length of two’s complement signed integer-5
0x0c - 0x0fUnusedUnused-
0x10LTLess-than comparison-3
0x11GTGreater-than comparison-3
0x12SLTSigned less-than comparison-3
0x13SGTSigned greater-than comparison-3
0x14EQEquality comparison-3
0x15ISZEROSimple not operator-3
0x16ANDBitwise AND operation-3
0x17ORBitwise OR operation-3
0x18XORBitwise XOR operation-3
0x19NOTBitwise NOT operation-3
0x1aBYTERetrieve single byte from word-3
0x1bSHLShift LeftEIP1453
0x1cSHRLogical Shift RightEIP1453
0x1dSARArithmetic Shift RightEIP1453
0x20KECCAK256Compute Keccak-256 hash-30*
0x21 - 0x2fUnusedUnused
0x30ADDRESSGet address of currently executing account-2
0x31BALANCEGet balance of the given account-700
0x32ORIGINGet execution origination address-2
0x33CALLERGet caller address-2
0x34CALLVALUEGet deposited value by the instruction/transaction responsible for this execution-2
0x35CALLDATALOADGet input data of current environment-3
0x36CALLDATASIZEGet size of input data in current environment-2*
0x37CALLDATACOPYCopy input data in current environment to memory-3
0x38CODESIZEGet size of code running in current environment-2
0x39CODECOPYCopy code running in current environment to memory-3*
0x3aGASPRICEGet price of gas in current environment-2
0x3bEXTCODESIZEGet size of an account’s code-700
0x3cEXTCODECOPYCopy an account’s code to memory-700*
0x3dRETURNDATASIZEPushes the size of the return data buffer onto the stackEIP 2112
0x3eRETURNDATACOPYCopies data from the return data buffer to memoryEIP 2113
0x3fEXTCODEHASHReturns the keccak256 hash of a contract’s codeEIP 1052700
0x40BLOCKHASHGet the hash of one of the 256 most recent complete blocks-20
0x41COINBASEGet the block’s beneficiary address-2
0x42TIMESTAMPGet the block’s timestamp-2
0x43NUMBERGet the block’s number-2
0x44DIFFICULTYGet the block’s difficulty-2
0x45GASLIMITGet the block’s gas limit-2
0x46CHAINIDReturns the current chain’s EIP-155 unique identifierEIP 13442
0x47 - 0x4fUnused-
0x48BASEFEEReturns the value of the base fee of the current block it is executing in.EIP 31982
0x50POPRemove word from stack-2
0x51MLOADLoad word from memory-3*
0x52MSTORESave word to memory-3*
0x53MSTORE8Save byte to memory-3
0x54SLOADLoad word from storage-800
0x55SSTORESave word to storage-20000**
0x56JUMPAlter the program counter-8
0x57JUMPIConditionally alter the program counter-10
0x58GETPCGet the value of the program counter prior to the increment-2
0x59MSIZEGet the size of active memory in bytes-2
0x5aGASGet the amount of available gas, including the corresponding reduction for the cost of this instruction-2
0x5bJUMPDESTMark a valid destination for jumps-1
0x5c - 0x5fUnused-
0x60PUSH1Place 1 byte item on stack-3
0x61PUSH2Place 2-byte item on stack-3
0x62PUSH3Place 3-byte item on stack-3
0x63PUSH4Place 4-byte item on stack-3
0x64PUSH5Place 5-byte item on stack-3
0x65PUSH6Place 6-byte item on stack-3
0x66PUSH7Place 7-byte item on stack-3
0x67PUSH8Place 8-byte item on stack-3
0x68PUSH9Place 9-byte item on stack-3
0x69PUSH10Place 10-byte item on stack-3
0x6aPUSH11Place 11-byte item on stack-3
0x6bPUSH12Place 12-byte item on stack-3
0x6cPUSH13Place 13-byte item on stack-3
0x6dPUSH14Place 14-byte item on stack-3
0x6ePUSH15Place 15-byte item on stack-3
0x6fPUSH16Place 16-byte item on stack-3
0x70PUSH17Place 17-byte item on stack-3
0x71PUSH18Place 18-byte item on stack-3
0x72PUSH19Place 19-byte item on stack-3
0x73PUSH20Place 20-byte item on stack-3
0x74PUSH21Place 21-byte item on stack-3
0x75PUSH22Place 22-byte item on stack-3
0x76PUSH23Place 23-byte item on stack-3
0x77PUSH24Place 24-byte item on stack-3
0x78PUSH25Place 25-byte item on stack-3
0x79PUSH26Place 26-byte item on stack-3
0x7aPUSH27Place 27-byte item on stack-3
0x7bPUSH28Place 28-byte item on stack-3
0x7cPUSH29Place 29-byte item on stack-3
0x7dPUSH30Place 30-byte item on stack-3
0x7ePUSH31Place 31-byte item on stack-3
0x7fPUSH32Place 32-byte (full word) item on stack-3
0x80DUP1Duplicate 1st stack item-3
0x81DUP2Duplicate 2nd stack item-3
0x82DUP3Duplicate 3rd stack item-3
0x83DUP4Duplicate 4th stack item-3
0x84DUP5Duplicate 5th stack item-3
0x85DUP6Duplicate 6th stack item-3
0x86DUP7Duplicate 7th stack item-3
0x87DUP8Duplicate 8th stack item-3
0x88DUP9Duplicate 9th stack item-3
0x89DUP10Duplicate 10th stack item-3
0x8aDUP11Duplicate 11th stack item-3
0x8bDUP12Duplicate 12th stack item-3
0x8cDUP13Duplicate 13th stack item-3
0x8dDUP14Duplicate 14th stack item-3
0x8eDUP15Duplicate 15th stack item-3
0x8fDUP16Duplicate 16th stack item-3
0x90SWAP1Exchange 1st and 2nd stack items-3
0x91SWAP2Exchange 1st and 3rd stack items-3
0x92SWAP3Exchange 1st and 4th stack items-3
0x93SWAP4Exchange 1st and 5th stack items-3
0x94SWAP5Exchange 1st and 6th stack items-3
0x95SWAP6Exchange 1st and 7th stack items-3
0x96SWAP7Exchange 1st and 8th stack items-3
0x97SWAP8Exchange 1st and 9th stack items-3
0x98SWAP9Exchange 1st and 10th stack items-3
0x99SWAP10Exchange 1st and 11th stack items-3
0x9aSWAP11Exchange 1st and 12th stack items-3
0x9bSWAP12Exchange 1st and 13th stack items-3
0x9cSWAP13Exchange 1st and 14th stack items-3
0x9dSWAP14Exchange 1st and 15th stack items-3
0x9eSWAP15Exchange 1st and 16th stack items-3
0x9fSWAP16Exchange 1st and 17th stack items-3
0xa0LOG0Append log record with no topics-375
0xa1LOG1Append log record with one topic-750
0xa2LOG2Append log record with two topics-1125
0xa3LOG3Append log record with three topics-1500
0xa4LOG4Append log record with four topics-1875
0xa5 - 0xafUnused-
0xb0JUMPTOTentative libevmasm has different numbersEIP 615
0xb1JUMPIFTentativeEIP 615
0xb2JUMPSUBTentativeEIP 615
0xb4JUMPSUBVTentativeEIP 615
0xb5BEGINSUBTentativeEIP 615
0xb6BEGINDATATentativeEIP 615
0xb8RETURNSUBTentativeEIP 615
0xb9PUTLOCALTentativeEIP 615
0xbaGETLOCALTentativeEIP 615
0xbb - 0xe0Unused-
0xe1SLOADBYTESOnly referenced in pyethereum--
0xe2SSTOREBYTESOnly referenced in pyethereum--
0xe3SSIZEOnly referenced in pyethereum--
0xe4 - 0xefUnused-
0xf0CREATECreate a new account with associated code-32000
0xf1CALLMessage-call into an account-Complicated
0xf2CALLCODEMessage-call into this account with alternative account’s code-Complicated
0xf3RETURNHalt execution returning output data-0
0xf4DELEGATECALLMessage-call into this account with an alternative account’s code, but persisting into this account with an alternative account’s code-Complicated
0xf5CREATE2Create a new account and set creation address to sha3(sender + sha3(init code)) % 2**160-
0xf6 - 0xf9Unused--
0xfaSTATICCALLSimilar to CALL, but does not modify state-40
0xfbUnused--
0xfcTXEXECGASNot in yellow paper--
0xfdREVERTStop execution and revert state changes, without consuming all provided gas and providing a reason-0
0xfeINVALIDDesignated invalid instruction-0
0xffSELFDESTRUCTHalt execution and register account for later deletion-5000*

Some additional notes

  • Both suicide() and selfdestruct() Solidity calls are represented internally by opcode 0xff as SELFDESTRUCT.
  • Push opcodes are the only way to add new content to the stack.

References



💬 Share this post in social media

Thanks for checking this out and I hope you found the info useful! If you have any questions, don't hesitate to write me a comment below. And remember that if you like to see more content on, just let me know it and share this post with your colleges, co-workers, FFF, etc.

Please, don't try to hack this website servers. Guess why...