Operation Codes (Opcodes)
What the EVM Can Do
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):
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 EVM Data Structures 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
Last updated