In the first example, I tried to change the second slot of the structure but failed
mapping(uint256 => MyStruct) public myStruct; struct MyStruct { uint128 slot1; uint128 slot2; //fixed 32-bytes, 256bit } function writeTest(uint256 key) external { assembly { mstore(0x0, key) mstore(0x20, myStruct.slot) let hash := keccak256(0, 0x40) let s:= sload(hash) sstore( hash, or( and(s, not(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), and(12, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) ) ) // clear first 128 bit and write new 128 bit (12) let slot2 := div( and( s, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000 ), 0x100000000000000000000000000000000 ) // or slot2 := shr(128, s) // how i can clean and rewrite bit in slot 2 } }
In the second example, I tried to create a 32-byte struct in memory and then save it in storage, in this case only the first slot of the string changes.
function writeTest2(uint256 key) external { MyStruct memory test2 = MyStruct(10, 20); // 32 bytes assembly { mstore(0x0, key) mstore(0x20, myStruct.slot) let hash := keccak256(0, 0x40) sstore(hash, mload(test2)) // check map and get me wrong value, just slot 1 changed } }
how I can write, read or shift a,b,c in mstore
for one 32-byte pack
function testPackingMemory(uint64 a,uint64 b,uint128 c) external { assembly { mstore(0x40,some_value) // how i can write or read a,b,c in mstore for one 32byte } }