Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Ethereum Virtual Machine (EVM) in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Ethereum Virtual Machine (EVM)
Start Transaction
↓
Fetch Contract Bytecode
↓
Initialize EVM Environment
↓
Execute Bytecode Instructions
↓
Update State & Storage
↓
Return Output or Error
↓
End
The EVM takes a transaction, loads contract code, runs instructions step-by-step, updates blockchain state, and returns results.
Execution Sample
Blockchain / Solidity
PUSH1 0x60
PUSH1 0x40
MSTORE
CALLVALUE
DUP1
ISZERO
PUSH2 0x0010
JUMPI
This EVM bytecode sets up memory, checks if any ether was sent, and conditionally jumps based on that.
Execution Table
StepInstructionStack BeforeStack AfterMemory ChangesProgram CounterNotes
1PUSH1 0x60[][0x60]No change1Push 0x60 onto stack
2PUSH1 0x40[0x60][0x40, 0x60]No change2Push 0x40 onto stack
3MSTORE[0x40, 0x60][]Memory[0x40..0x5f] = 0x603Store 0x60 at memory offset 0x40
4CALLVALUE[][0x00]No change4Push call value (0 ether) onto stack
5DUP1[0x00][0x00, 0x00]No change5Duplicate top stack item
6ISZERO[0x00, 0x00][0x01, 0x00]No change6Check if top is zero, push 1 (true)
7PUSH2 0x0010[0x01, 0x00][0x0010, 0x01, 0x00]No change7Push jump destination 0x10
8JUMPI[0x0010, 0x01, 0x00][0x00]No change8Jump to 0x10 because condition is true
9End[0x00][0x00]No changeEndExecution jumps to 0x10 or ends
💡 Execution ends after jump or reaching end of bytecode
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8Final
Stack[][0x60][0x40, 0x60][][0x00][0x00, 0x00][0x01, 0x00][0x0010, 0x01, 0x00][0x00][0x00]
Memoryemptyemptyempty0x40:0x60 stored0x40:0x60 stored0x40:0x60 stored0x40:0x60 stored0x40:0x60 stored0x40:0x60 stored0x40:0x60 stored
Program Counter012345678End
Key Moments - 3 Insights
Why does the stack get emptied after MSTORE at step 3?
MSTORE consumes two items from the stack (offset and value) to store in memory, so both are removed, leaving the stack empty as shown in execution_table row 3.
How does JUMPI decide to jump or not?
JUMPI checks the top stack item as a condition; if it's nonzero (true), it jumps to the address below it on the stack. Here, at step 8, condition is 1 (true), so it jumps.
What does CALLVALUE push onto the stack?
CALLVALUE pushes the amount of ether sent with the call. In this example, it's 0, so 0x00 is pushed at step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6. What is the stack after ISZERO executes?
A[]
B[0x00, 0x00]
C[0x01, 0x00]
D[0x0010, 0x01]
💡 Hint
Check the 'Stack After' column for step 6 in the execution_table.
At which step does the memory get updated with the value 0x60?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Memory Changes' column in the execution_table.
If CALLVALUE pushed 0x05 instead of 0x00, what would be the stack after ISZERO at step 6?
A[0x00, 0x05]
B[0x01]
C[0x00, 0x00]
D[0x00]
💡 Hint
ISZERO pushes 1 if input is zero, else 0; check variable_tracker for CALLVALUE value.
Concept Snapshot
Ethereum Virtual Machine (EVM) executes smart contract bytecode step-by-step.
It uses a stack for operations and memory/storage for data.
Instructions manipulate stack, memory, and program counter.
JUMPI conditionally jumps based on stack top.
CALLVALUE pushes sent ether amount.
MSTORE stores data in memory at given offset.
Full Transcript
The Ethereum Virtual Machine (EVM) runs smart contract code by reading bytecode instructions one by one. It uses a stack to hold temporary values and memory to store data during execution. For example, PUSH1 adds a value to the stack, MSTORE saves a value from the stack into memory, and CALLVALUE pushes the amount of ether sent with the transaction. The program counter moves through instructions, and JUMPI can jump to a different instruction if a condition on the stack is true. This step-by-step process updates the blockchain state or returns output. The execution table shows how the stack and memory change after each instruction, helping beginners see exactly what happens inside the EVM.

Practice

(1/5)
1. What is the main role of the Ethereum Virtual Machine (EVM) in the Ethereum network?
easy
A. It executes smart contract bytecode to run decentralized applications.
B. It stores user account balances and transaction history.
C. It mines new Ethereum blocks using proof of work.
D. It manages the Ethereum wallet user interface.

Solution

  1. Step 1: Understand EVM's purpose

    The EVM is a virtual computer that runs smart contracts on Ethereum.
  2. Step 2: Identify correct role

    It executes compiled bytecode from smart contracts, enabling decentralized apps to run safely and consistently.
  3. Final Answer:

    It executes smart contract bytecode to run decentralized applications. -> Option A
  4. Quick Check:

    EVM runs smart contracts = B [OK]
Hint: EVM runs code, not stores or mines [OK]
Common Mistakes:
  • Confusing EVM with blockchain storage
  • Thinking EVM mines blocks
  • Mixing EVM with wallet software
2. Which of the following is the correct way to represent a simple EVM instruction to add two numbers?
easy
A. ADD 5 3
B. PUSH1 5 PUSH1 3 ADD
C. LOAD 5 3 ADD
D. SUM 5 3

Solution

  1. Step 1: Recall EVM instruction format

    EVM uses stack operations like PUSH1 to push values, then ADD to add top stack items.
  2. Step 2: Identify correct instruction sequence

    "PUSH1 5 PUSH1 3 ADD" pushes 5 and 3, then adds them on the stack.
  3. Final Answer:

    PUSH1 5 PUSH1 3 ADD -> Option B
  4. Quick Check:

    Push values then add = C [OK]
Hint: EVM uses PUSH and then operation instructions [OK]
Common Mistakes:
  • Using invalid instructions like ADD 5 3 directly
  • Confusing LOAD with PUSH
  • Using non-EVM commands like SUM
3. Given this EVM bytecode snippet:
PUSH1 0x02
PUSH1 0x03
ADD

What is the result on the stack after execution?
medium
A. 0x05
B. 0x06
C. 0x23
D. 0x00

Solution

  1. Step 1: Understand PUSH1 and ADD operations

    PUSH1 0x02 pushes 2, PUSH1 0x03 pushes 3, ADD adds top two stack values.
  2. Step 2: Calculate addition result

    2 + 3 = 5, so 0x05 remains on the stack.
  3. Final Answer:

    0x05 -> Option A
  4. Quick Check:

    2 + 3 = 5 = 0x05 [OK]
Hint: Add pushed hex values on stack [OK]
Common Mistakes:
  • Adding hex as strings instead of numbers
  • Confusing 0x02 + 0x03 as 0x06
  • Misreading PUSH1 as PUSH2
4. Identify the error in this EVM bytecode sequence:
PUSH1 0x01
ADD
medium
A. No error; code is correct.
B. PUSH1 opcode is invalid.
C. ADD cannot be used after PUSH1.
D. Missing second operand on stack for ADD instruction.

Solution

  1. Step 1: Check stack before ADD

    PUSH1 0x01 pushes one value, but ADD needs two values on stack.
  2. Step 2: Identify missing operand

    Since only one value is pushed, ADD causes stack underflow error.
  3. Final Answer:

    Missing second operand on stack for ADD instruction. -> Option D
  4. Quick Check:

    ADD needs two stack values [OK]
Hint: ADD requires two values on stack [OK]
Common Mistakes:
  • Assuming ADD works with one operand
  • Thinking PUSH1 is invalid
  • Believing code is correct as is
5. You want to write an EVM bytecode sequence that calculates (4 + 5) * 3. Which sequence correctly achieves this?
hard
A. PUSH1 0x03 PUSH1 0x04 ADD PUSH1 0x05 MUL
B. PUSH1 0x04 PUSH1 0x05 MUL PUSH1 0x03 ADD
C. PUSH1 0x04 PUSH1 0x05 ADD PUSH1 0x03 MUL
D. PUSH1 0x04 PUSH1 0x03 MUL PUSH1 0x05 ADD

Solution

  1. Step 1: Understand operation order

    (4 + 5) * 3 means add 4 and 5 first, then multiply by 3.
  2. 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.
  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).
  4. Final Answer:

    PUSH1 0x04 PUSH1 0x05 ADD PUSH1 0x03 MUL -> Option C
  5. Quick Check:

    Add then multiply = A [OK]
Hint: Add first, then multiply in EVM code [OK]
Common Mistakes:
  • Changing operation order incorrectly
  • Confusing MUL and ADD positions
  • Ignoring operator precedence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard