// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract MemoryAssembly {
function AssemblyUsage() public pure returns (uint256) {
assembly {
// Add 100 and 200, store the result in `addresult`
let addresult := add(100,200)
// Store the result of addition at memory address 0x0
mstore(0x0, addresult)
// Load the value stored at memory address 0x0 into `y`
let y := mload(0x0)
// Add 200 to `y` and store the result at memory address 0x20
mstore(add(0x0, 0x20), add(y,200))
// Return the value stored at memory address 0x20, with a size of 32 bytes
return(0x20, 32)
}
}
function AssemblyUsage1() public pure returns (uint256) {
assembly {
// Add 100 and 200, store the result in `addresult`
let addresult := add(100,200)
// Store the result of addition at memory address 0x40
mstore(0x40, addresult)
// Load the value stored at memory address 0x40 into `y`
let y := mload(0x40)
// Add 200 to `y` and store the result at memory address 0x60
mstore(add(0x40, 0x20), add(y,200))
// Return the value stored at the beginning of the allocated memory (0x0), with a size of 32 bytes
return(0x0, 32)
}
}
}