CS4998: Blockchain Development Textbook
  • CS4998: Blockchain Development
  • Prerequisites
  • Introduction
    • Blockchain Theory
      • Bitcoin and the UTXO Model
      • Ethereum and the State-Based Model
    • Remix - A First Glance
    • Hello World!
      • Solidity File Structure
      • Primitive Values & Types
      • Contract Structure
      • Functions
      • Data Structures
      • Summary & Exercises
    • Hello World! Pt. 2
      • Control Flow
      • Interfaces and Inheritance
      • Constructors
      • Contract Interactions
      • Modifiers
      • Dynamic Arrays and Strings
        • Dynamic Arrays
        • Strings
      • Errors
      • Events
      • Units and Global Variables
      • Default Functions
  • Local Development
    • Node Providers
    • Interacting With On-Chain Contracts
    • Migrating to Foundry & VS Code
      • The Basics of Forge
      • Installing and Using Dependencies
      • Cast
      • Anvil
  • Understanding the EVM
    • The Ethereum Virtual Machine
      • A First Look at Computers
      • The Turing Machine
      • EVM Data Structures
      • Operation Codes (Opcodes)
      • Gas
      • Contract Compilation
      • Contract Runtime
    • Gas Optimizations
  • Yul & Advanced EVM Topics
    • Yul
    • Metamorphism
    • Bitwise Manipulations
  • Correctness
    • Security
    • Types of Testing
  • ERC Standards
    • Why ERCs?
    • ERC20
    • ERC721
    • ERC777
    • ERC1155
  • Frequently Used Smart Contracts
    • OpenZeppelin
    • Uniswap
    • Multisignature Contracts
    • AAVE/Compound
  • MEV & Advanced Blockchain Theory
    • Consensus Mechanisms vs Sybil Resistance Mechanisms
    • Maximal Extractable Value (MEV)
    • Looking Past The EVM
  • Etcetera
    • Developer Practices
    • Spring 2023 Past Resources
Powered by GitBook
On this page
  • Stack-Based Opcodes
  • Memory-Based Opcodes
  • Storage-Based Opcodes
  1. Understanding the EVM
  2. The Ethereum Virtual Machine

Operation Codes (Opcodes)

What the EVM Can Do

PreviousEVM Data StructuresNextGas

Last updated 1 year ago

In the previous section, we went over the data structures that define the state of the EVM at any point of time. While we might have a grasp at their implementation, what we haven't yet covered is how we can manipulate these data structures.

In this section, we will go over opcodes - the set of all instructions supported by the EVM which allows us to manipulate the data structures previously mentioned. The number of opcodes available to use is large; therefore, we will go over opcodes by identifying the different grouping of opcodes available to us. In particular, we will go over opcodes in the following order:

  • Opcodes related solely to the stack

  • Opcode related to memory

  • Opcodes related to storage

  • Arithmetic Opcodes

  • Logical & Bitwise Opcodes

  • Message Opcodes

  • Environment Opcodes

  • Event Opcodes

  • Contract Creation, Return, and Miscellaneous Opcodes

Stack-Based Opcodes

As it will become evident throughout the rest of this section, the stack is the main data structure for which we will extract the arguments for other opcodes. This brings up a good first question - how do we actually get items onto the stack? We will look at our first opcode for this - the PUSH1 opcode. The PUSH1 opcode has the following syntax (in hexadecimal notation):

60 <arg>

where arg is the value to push onto the stack. The next logical question to ask is what value can I set arg to? To this, look at the integer attached to PUSH1. The 1 indicates that arg is at most a 1-byte value. Therefore, the PUSH1 opcode allows us to push a 1-byte value onto the stack.

If you recall from the section, each element on the stack is 32-byte long. Therefore, we are not limited to just pushing 1-byte values onto the stack. In fact, for any 1 <= n <= 32, there exists a PUSHn opcode.

While pushing values onto the stack allows us to feed in arguments to the EVM, consider the following scenarios:

  • What if we want to switch the position of an element on the stack?

  • What if we wanted to duplicate elements on the stack?

To address both of these scenarios, we have the SWAP and the DUP opcodes which, as their abbreviations might suggest, allow us to swap and duplicate stack items, respectively.

Memory-Based Opcodes

Storage-Based Opcodes

EVM Data Structures