-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem_array_asm.sol
42 lines (35 loc) · 1.02 KB
/
mem_array_asm.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// SPDX-License-Identifier: MIT
// to answer https://ethereum.stackexchange.com/questions/113066/how-does-the-storage-work-with-an-array-of-length-2256-1
pragma solidity ^0.7.6;
contract TestArray {
uint256[] abc;
constructor() {
abc.push(1);
abc.push(2);
abc.push(3);
abc.push(4);
abc.push(5); // length = 5
}
function getLength() external view returns (uint256 lgth) {
assembly {
let ptr := abc.slot
lgth := sload(ptr)
}
}
function newLength() external {
assembly {
let ptr := abc.slot
sstore(abc.slot, 2)
}
}
function getElt(uint256 idx) external view returns (uint256) {
return abc[idx];
}
// returns the value at the index idx - low level
function getStorage(uint256 idx) external view returns (uint256 res) {
assembly {
let ptr := abc.slot
res := sload(add(keccak256(abc.slot, 32), idx))
}
}
}