Last active
September 14, 2022 14:11
-
-
Save ItsCuzzo/6083865aaeabd104319cf7705c909a00 to your computer and use it in GitHub Desktop.
Understanding arrays using assembly.
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
contract AsmArray { | |
uint256[] public arr; | |
/// @dev Push some dummy data to begin with. | |
constructor() { | |
arr.push(1); | |
arr.push(2); | |
} | |
/// @dev Function used to view the value of `arr[idx]`. | |
/// This function will return 0 if out of bounds `idx` is provided. | |
function viewStorageAtIdx(uint256 idx) external view returns (uint256) { | |
assembly { | |
mstore(0x00, arr.slot) | |
mstore(0x40, sload(add(keccak256(0, 32), idx))) | |
return(0x40, 32) | |
} | |
} | |
/// @dev Function used to push a `val` to `arr`. | |
function pushToArr(uint256 val) external { | |
assembly { | |
/// Reuse array length to avoid double sload. | |
let len := sload(arr.slot) | |
mstore(0x00, arr.slot) | |
/// Store `val` in array at `len`. | |
sstore(add(keccak256(0, 32), len), val) | |
/// Increment value of `arr.slot` which is the length of the array by 1. | |
sstore(arr.slot, add(len, 1)) | |
} | |
} | |
/// @dev Function used to return the current length of `arr`. | |
function getArrLength() external view returns (uint256) { | |
assembly { | |
/// Store the current length of `arr` in memory at fmp. | |
mstore(0x40, sload(arr.slot)) | |
return(0x40, 32) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@atarpara Sorry man, forgot to reply! But I have updated with your fixes. Thanks for the insight. 💪