sload. assemblaggio con struct - non funziona in solidità

Oct 02 2020
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)
  }
}

Come possiamo vedere, test1ora occuperà il primo slot. e non il secondo dato che le variabili possono essere impacchettate ...

Ora, perché sload(0)restituisce qualcosa di simile 3402823669209384634633746074317682114565e come posso restituire 5 e 10?

Risposte

1 Ismael Oct 02 2020 at 21:15

Come hai detto, la struttura è impacchettata. Poiché sload(0)restituisce 32 byte, restituisce l'intera struttura.

Per accedere alle singole parti della struttura utilizzare turni di bit e maschere.

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