-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path039-assembly-in-solidity.sol
57 lines (42 loc) · 1.23 KB
/
039-assembly-in-solidity.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Assembly in Solidity
*/
contract LearnAssembly {
function addToEVM() external {
uint256 x;
uint256 y;
assembly {
let z := add(x, y)
// load the data for a specific slot
let a := mload(0x40)
// store something temporarily unto memory
mstore(a, 4)
// persistence storage
sstore(a, 100)
}
}
function addToEVM2() public view returns (bool success) {
uint256 size;
address addr;
// checks whether an address contains any bytes of code or not
assembly {
size := extcodesize(addr)
}
if (size > 0) return true;
return false;
}
// exercise
function addToEVM3() external pure {
bytes memory data = new bytes(10);
// we can not convert this into a fixed size
// this conversion will throw an error or will fail
// bytes32 dataB32 = bytes32(data);
bytes32 byteB32;
assembly {
byteB32 := mload(add(data, 32))
}
// but if we care only about the first 32 bytes we can use assembly!
}
}