Member-only story
Solidity Tutorial: All About Stack
Exploring the EVM stack machine, and how pushing and popping this in and out of the stack work.
This is Part IV of the “All About Data Locations” subseries.
In today’s article, we will look at the fourth data location of the EVM: the stack. We use it all of the time in Solidity when we assign values to variables, but how does the EVM stack work under the hood?
We will see the layout of the EVM stack and what opcodes are used to interact with it at the low level. We will then explain what block scopes are (blocks of codes wrapped around curly braces { }
), how they work, their benefits, and how to use them in Solidity.
Finally, we will go in depth about the unpopular “stack too deep” error. Rather than looking at tips and tricks to avoid this error, we will look at how to refactor your code to minimise stack items and prevent this error.
Table of Contents
Introduction
The EVM Stack – Overview
Layout of the EVM Stack
Padding Rules for Variables Stored on the Stack
Variables on the Stack: Direct vs Pointer Types
The Concept of Stack Balancing
Block Scopes { }
Stack vs “Call” Stack
Stack and Inline Assembly
Avoiding “Stack Too Deep” Error