Last active
August 9, 2023 08:36
-
-
Save seaona/6343017e48f9d20c74002071159afbf8 to your computer and use it in GitHub Desktop.
Yul memory storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.8.18; | |
contract Assembly { | |
uint256 a = 9; // Slot 0 | |
uint256 b = 8; // Slot 1 | |
uint256 c = 7; // Slot 2 | |
uint256 d = 6; // Slot 3 | |
uint8 e = 4; // Slot 4 | |
uint8 f =5; // Slot 4 | |
uint128 g =34; // Slot 4 | |
uint128 h = 23; // Slot 5 | |
function learningMemory(bytes32 a) public { | |
assembly { | |
sstore(0x80, a) // the free memory pointer points to 0x80 initially. We add 32 bytes (128+32 = 160) | |
sstore(0xa0, 0x7465737400000000000000000000000000000000000000000000000000000000) // next memory position is 160 = 0xa0 | |
} | |
} | |
function learningStorage(uint256 num) public view returns (bytes32 result) { | |
assembly { | |
result := sload(num) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment