// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
// Define a contract named StorageAssembly
contract StorageAssembly {
// A state variable to hold a uint256 value
uint256 StateVariable;
// A function that demonstrates the use of inline assembly to directly manipulate storage
function assemblyUsage() public returns (uint256 newstatevariable, uint256 newderivedvariable) {
assembly {
// Directly store the value 100 at storage slot 0
sstore(0x0, 100)
// Load the value from storage slot 0 into the variable `newstatevariable`
newstatevariable := sload(0x0)
// Directly store the value 200 at storage slot 1
sstore(0x1, 200)
// Load the value from storage slot 1 into the variable `newderivedvariable`
newderivedvariable := sload(0x1)
}
}
// A public view function to get the current value of StateVariable
function getStateVariable() public view returns(uint256) {
return StateVariable;
}
// A function to retrieve the value stored at storage slot 1 using inline assembly
function getNewDerivedVariable() public view returns(uint256 slot1) {
assembly {
// Load the value from storage slot 1 into the variable `slot1`
slot1 := sload(0x1)
}
}
// A function to update the state variable by adding a given value to the value at storage slot 1,
// and then storing the result back into the state variable.
function updateStateVariable(uint256 intValue) public returns (uint newValue){
assembly {
// Add `intValue` to the value at storage slot 1 and store the result in `newValue`
newValue := add(intValue, sload(0x1))
// Update the state variable `StateVariable` with `newValue`
sstore(0, newValue)
}
}
}