The Ethereum Virtual Machine (EVM) is like a computer inside the Ethereum blockchain. It runs smart contracts, which are small programs that do tasks automatically.
Ethereum Virtual Machine (EVM) in Blockchain / Solidity
The EVM runs bytecode generated from high-level languages like Solidity. Example of Solidity function compiled to EVM bytecode: function add(uint a, uint b) public pure returns (uint) { return a + b; }
The EVM itself does not use a traditional programming language but runs low-level bytecode.
Developers write smart contracts in languages like Solidity, which are then compiled to EVM bytecode.
pragma solidity ^0.8.0; contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } }
pragma solidity ^0.8.0; contract Adder { function add(uint a, uint b) public pure returns (uint) { return a + b; } }
This contract keeps a count number. You can increase it by calling increment(), and check it with getCount(). The EVM runs this code on the blockchain.
pragma solidity ^0.8.0; contract Counter { uint public count = 0; function increment() public { count += 1; } function getCount() public view returns (uint) { return count; } }
The EVM ensures all nodes on the Ethereum network run the same code and get the same results.
Gas is used to pay for running code on the EVM, so complex operations cost more.
Once a smart contract is deployed on the EVM, its code cannot be changed.
The EVM is a secure computer inside Ethereum that runs smart contracts.
Developers write contracts in Solidity, which the EVM executes as bytecode.
The EVM makes decentralized apps possible by running code the same way everywhere.
Practice
Ethereum Virtual Machine (EVM) in the Ethereum network?Solution
Step 1: Understand EVM's purpose
The EVM is a virtual computer that runs smart contracts on Ethereum.Step 2: Identify correct role
It executes compiled bytecode from smart contracts, enabling decentralized apps to run safely and consistently.Final Answer:
It executes smart contract bytecode to run decentralized applications. -> Option AQuick Check:
EVM runs smart contracts = B [OK]
- Confusing EVM with blockchain storage
- Thinking EVM mines blocks
- Mixing EVM with wallet software
Solution
Step 1: Recall EVM instruction format
EVM uses stack operations like PUSH1 to push values, then ADD to add top stack items.Step 2: Identify correct instruction sequence
"PUSH1 5 PUSH1 3 ADD" pushes 5 and 3, then adds them on the stack.Final Answer:
PUSH1 5 PUSH1 3 ADD -> Option BQuick Check:
Push values then add = C [OK]
- Using invalid instructions like ADD 5 3 directly
- Confusing LOAD with PUSH
- Using non-EVM commands like SUM
PUSH1 0x02
PUSH1 0x03
ADD
What is the result on the stack after execution?
Solution
Step 1: Understand PUSH1 and ADD operations
PUSH1 0x02 pushes 2, PUSH1 0x03 pushes 3, ADD adds top two stack values.Step 2: Calculate addition result
2 + 3 = 5, so 0x05 remains on the stack.Final Answer:
0x05 -> Option AQuick Check:
2 + 3 = 5 = 0x05 [OK]
- Adding hex as strings instead of numbers
- Confusing 0x02 + 0x03 as 0x06
- Misreading PUSH1 as PUSH2
PUSH1 0x01
ADD
Solution
Step 1: Check stack before ADD
PUSH1 0x01 pushes one value, but ADD needs two values on stack.Step 2: Identify missing operand
Since only one value is pushed, ADD causes stack underflow error.Final Answer:
Missing second operand on stack for ADD instruction. -> Option DQuick Check:
ADD needs two stack values [OK]
- Assuming ADD works with one operand
- Thinking PUSH1 is invalid
- Believing code is correct as is
Solution
Step 1: Understand operation order
(4 + 5) * 3 means add 4 and 5 first, then multiply by 3.Step 2: Match EVM instructions to operations
PUSH1 0x04 PUSH1 0x05 ADD pushes 4 and 5, adds them; then PUSH1 0x03 MUL multiplies result by 3.Step 3: Check other options
Options B, C, and D change operation order or operands, producing incorrect results (B: 23, C: 35, D: 17).Final Answer:
PUSH1 0x04 PUSH1 0x05 ADD PUSH1 0x03 MUL -> Option CQuick Check:
Add then multiply = A [OK]
- Changing operation order incorrectly
- Confusing MUL and ADD positions
- Ignoring operator precedence
Ready to prove it?
Pass the test and this pattern moves unknown
