DEV Community

Cover image for Simplifying Solidity with Assembly: A Beginner's Guide -(Part 1)
Hail The Lord
Hail The Lord

Posted on

Simplifying Solidity with Assembly: A Beginner's Guide -(Part 1)

Solidity is a popular language for developing smart contracts on the Ethereum blockchain due to its straightforward syntax and ease of use. However, combining Solidity with low-level assembly can be daunting for newcomers. In this guide, we'll break down the essentials and guide you through working with assembly to maximize your contract's performance.

Image description

when you see the bytecode of the above code it would look something like below :

Image description

Global variables in Solidity are stored as part of the contract's bytecode. This bytecode serves as a blueprint for the Ethereum Virtual Machine (EVM) when executing your smart contract. When you send a transaction to your smart contract, the EVM accesses the bytecode to determine the value of the global variables and how to interact with them. In Solidity, managing global variables is straightforward due to the language's built-in getter and setter functions, simplifying the process of retrieving and updating data and making smart contract development more accessible.

However, let's dive deeper and explore how to achieve the same functionality using low-level Assembly code within Solidity. By working directly with the EVM in Assembly, you can access storage slots and manipulate data at a granular level, gaining more control over your smart contracts and optimizing their performance.

sload : intakes any slot value, returns value stored at slot given
.slot : gives the slot of global variable.
Image description
warning : state mutability can be restricted to pure
The warning recommends changing the function to pure, as it is currently accessing the storage slot of val1 but not the value. By using sload to return the value stored at the val1.slot, the function correctly accesses the storage location and returns the intended data, resolving the warning.

Image description

Thank you for reading! For more insightful explanations and to follow my journey as a security researcher in the Web3 space, connect with me on Twitter. Stay tuned for Part 2, where we'll dive deeper into advanced concepts. Your engagement and interest are greatly appreciated.

Top comments (0)