0

I have a special needs, store the mapping with assembly code. thank you very for everyone who can response this question.

for example:

contract assemblyMappingTest{
  mapping(address => uint) public assets;

  function save(address _user, uint _amount) public
  {
     assets[_user] = _amount;
  }

  function saveWithAssembly(address _user, uint _amount) public
  {
     assembly {
        // how to write these codes, so that it can store assets like the function "save"
     }     
  }
}
1

Ok, i reslove by myself.

contract assemblyMappingTest{
  mapping(address => uint) public assets;

  function save(address _user, uint _amount) public
  {
     assets[_user] = _amount;
  }

  function saveWithAssembly(address _user, uint _amount) public
  {
     assembly {
        mstore(0, _user)
        mstore(32, assets.slot)
        let hash := keccak256(0, 64)
        sstore(hash, _amount)
     }     
  }
}

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.