In my attempt to understand Solidity at a deeper level, I wanted to understand how bytes are manipulated using Yul during read and write functions. Thanks to Degatchi’s blog, I was able to see normal setter and getter functions implemented in Yul. However, the blog does not show how the bytes are changing as the code gets executed, so I wanted to dry-run the assembly code and show it.
The given function is an external function that is used to update a variable value. Normally the code is pretty straightforward, but here, we are using Yul where we can have more control over memory and reduce gas efficiently.
I am going to go through each line and show what exactly happens to the slot in the process.
Storage:
Breaking down each line of the code:
- Get rid of
000a
because out input will begin from the right hand side and it will be easier to modify the value directly instead of padding the input.
- Now we have to create our mask which will only keep our specific area and remove everything else.
XOR gate
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
- Input our value into the mask. [TBA]
- Add back the removed `a` value bits.
- Replace original 32 bytes
000014
with0001F4
Then, sstore just updates the bytes in storage
slot 1
to new_v