Demystifying Ethereum Assembly
Demystifying Ethereum Assembly
jtriley.eth
EVM Smart Contract Engineer
Section 1
https://linktr.ee/evmassembly
Instruction Set
The EVM is a stack-based virtual machine with a relatively small instruction set.
The instructions can be categorized by one of the following.
❖ Stack Instructions
❖ Arithmetic Instructions
❖ Comparison Instructions
❖ Bitwise Instructions
❖ Memory Instructions
❖ Context Instructions
➢ Read
➢ Write
Stack Instructions
Stack instructions involve manipulating the position of values on the stack.
- pushN value: pushes a value to the top of the stack where N is the byte
size of the value.
- pop: pops a value from the top of the stack.
- swapN: swaps the value from the top of the stack with a value at stack
index N.
- dupN: duplicates a value from the stack at index N and pushes it to the
stack.
Stack Example
Arithmetic Instructions
Arithmetic instructions pop two or more values from the stack, performs an arithmetic
operation, and pushes the result.
- lt / slt pushes true if the top stack value is less than the second.
- gt / sgt pushes true if the top stack value is greater than the second.
- eq pushes true if the top two stack values are equal.
- iszero pushes true if the top stack value is zero.
Comparison Example
Bitwise Instructions
Bitwise instructions pop one or more values from the stack and performs
bitwise operations on them.
To the left, there is a simple contract that will store the caller’s
address in persistent storage, then return “true” to indicate
success.
Section 2
Yul Syntax
Yul Overview
Yul is a low level language that may be written in-line in Solidity, as a
standalone language, and as a compilation target.
Built into the language are most EVM instructions callable as functions, basic
control flow support, and functions.
Notice that the stack is largely abstracted away with the exception of a built-in
pop function to drop variables.
Syntax Overview
Notice that object and code keywords are only used in stand-
alone Yul files, not in-line Solidity.
Yul in Solidity
Solidity Standards Overview
Solidity has created abstractions for standards that engineers and auditors must
be aware of when dealing with in-line Yul.
- Calldata Layout
- Memory Layout
- Storage Layout
- Event Logging
- Errors
Calldata Layout
Per the Application Binary Interface (ABI) standardization, the calldata layout
is as follows.
- The selector is the leftmost 4 bytes of a Keccak-256 hash of the function
signature (name and argument types).
- Each argument is padded to 32 bytes.
- If an argument is of dynamic size, the 32 byte slot will be a pointer to the
dynamic value later in the calldata.
Calldata Visualization
Memory Layout
Per the Solidity documentation, the first four slots of memory are reserved.
- 0x00 : scratchspace
- 0x20 : scratchspace
- 0x40 : free memory pointer
- 0x60 : zero slot
Dynamically sized arrays occupy one slot to point to the value in memory, one slot
to indicate length, then one slot for each element.
Byte arrays and strings are similar, except their elements are tightly packed and
aligned to the left.
Memory Example
Storage (Statically Sized Variables)
Per Solidity documentation:
@jtriley_eth