1/5
_Follow along with this video:_ --- ### Inline Assembly Let's look at a simple example of how `Yul` can be used as inline assembly in Solidity. We'll do this by converting our `HorseStore.sol` contract function logic! ```js // SPDX-License-Identifier: GPL-3.0-only pragma solidity 0.8.20; contract HorseStore { uint256 numberOfHorses; function updateHorseNumber(uint256 newNumberOfHorses) external { assembly { // YUL GOES HERE } } function readNumberOfHorses() external view returns (uint256) { assembly { // YUL GOES HERE } } } ``` In order to access Yul, we just need to wrap our operations in an `assembly` object, as shown above. From here we can access op codes almost like functions, with the stack layer of consideration abstracted away for us. ```js function updateHorseNumber(uint256 newNumberOfHorses) external { assembly { sstore(numberOfHorses.slot, newNumberOfHorses) } } ``` In this example we see `sstore()` being used like our SSTORE op code, with the stack inputs being passed as parameters. The first input is the storage slot we're storing our data to, and the second input is the data we're storing. That's all it takes to `updateHorseNumber`! ```js function readNumberOfHorses() external view returns (uint256) { assembly { let num := sload(numberOfHorses.slot) mstore(0, num) return(0,0x20) } } ``` We have to remember that we can't return or manipulate data in storage, it needs to be in memory for this. In the above function we load the `numberOfHorses` storage slot, store it in memory, and then return the number from memory! We can see `Yul` is a little bit different from working with Huff or working directly with the stack. It almost abstracts out the stack from our work, taking stack items as 'parameters'.
A comprehensive guide to Yul Inline Assembly. The lesson covers how to use Yul inline assembly with Solidity smart contracts, demonstrating how to set up your environment and implement a simple example of storing and reading data within a smart contract.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Solidity Developer
Assembly and Formal VerificationDuration: 30min
Duration: 4h 38min
Duration: 3h 57min
Duration: 1h 56min
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar