How Ethereum Virtual Machine (EVM) Works: Architecture and Opcodes Explained

·

12 min read

Overview

Ethereum processes billions of dollars in transactions daily (source), requiring developers to understand not just Solidity but also the Ethereum Virtual Machine (EVM). This guide explores EVM's execution of bytecode (opcodes), its components, and step-by-step transaction execution.

What You Will Need

What You Will Do


What Is a Virtual Machine (VM)?

A VM emulates physical hardware, enabling software to run isolated environments. VMs operate via machine code (binary) represented by opcodes like ADD, PUSH, or POP.

👉 Learn more about VMs


What Is the EVM?

The EVM executes Ethereum’s smart contract opcodes, defined in the Ethereum Yellow Paper. Key components:

EVM Architecture (Source: Ethereum.org)

Ethereum State Transition

The EVM’s deterministic function Y(S, T) = S' ensures transactions yield predictable results.


EVM Opcodes

Bytecode translates to opcodes instructing EVM components. Examples:

Full list: EVM Opcodes.

From Solidity to Bytecode

Example contract:

contract SimpleStorage {
  uint storedData;
  function set(uint x) public { storedData = x; }
  function get() public view returns (uint) { return storedData; }
}

Compiled bytecode (hex):

6080604052348015600e575f80fd5b506101438061001c5f395ff3fe...

Decompiled opcodes:

[00] PUSH1 80  
[02] PUSH1 40  
[04] MSTORE  
...

👉 Explore opcodes interactively


Transaction Execution Flow

  1. Decode Transaction: Validate nonce/signature (v, r, s).
  2. Execute Opcodes: Sequentially run bytecode (e.g., PUSH4 for function calls).
  3. Update State: Store results in persistent Storage.

Example calldata for set():

0x60fe47b10000000000000000000000000000000000000000000000000000000000010f2c

FAQ

Q: Why does the EVM use gas?
A: Gas prevents spam and ensures efficient resource use.

Q: What’s the max contract size?
A: 24 KB, but stored data is limited by gas, not size.

Q: How is bytecode stored?
A: In contract account state fields (codeHash).


Final Thoughts

Understanding EVM opcodes deepens smart contract development. For a deeper dive, explore:

Join us on Discord or follow Twitter for updates!


Additional Resources

We ❤️ Feedback!

Share suggestions.