Yul Exercises
In-memory
contract Question1 {
function iterateEachElementInArrayAndReturnTheSum(uint256[] calldata array) external pure returns (uint256) {
// TODO: Iterate each element in the array using only assembly
// creates a new in-memory copy of the input array
uint256[] memory arrayM = array;
uint256 sum = 0;
for (uint i = 0; i < arrayM.length; ++i) {
assembly {
// 0x20 needs to be added to an array because the first slot contains the array length.
sum := add(sum, mload( add(add(arrayM, 0x20), mul(i, 0x20)) ))
}
}
return sum;
}
}working with calldata
Last updated