1
struct S { 
   uint128 a; 
   uint128 b; 
}
S test1;
function assemblyStorage() public returns (uint a, uint b, uint c, uint d, uint f, uint g){
  test1 = S(5,10);
  assembly {
     a:=sload(0)
  }
}

As we can see, test1 now will occupy the first slot. and not the second one since variables can be packed...

Now, why does sload(0) returns something like this 3402823669209384634633746074317682114565 and how can I return 5 and 10 ?

1

As you said the struct is packed. Since sload(0) returns 32 bytes it is returning the whole struct.

To access the indiviual parts of the struct use bit shifts and masks.

assembly {
    let w := sload(0)
    a := and(w, 0xffffffffffffffffffffffffffffffff)
    b := shr(128, w)
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.