3

1- Solidity:

function name() external view returns(string memory) { 
 return _name;                       // No pointer slot
}

2- Assembly:

function name() external view returns(string memory) {

 uint256 len = bytes(_name).length;

 assembly {
   let mp := mload(0x40)
   mstore(mp, 0x20)                  // Pointer
   mstore(add(mp, 0x20), len)        // Length

   switch lt(len, 32)         
   case 1 {
     mstore(add(mp, 0x40), sload(3)) // Data
   }
   default {
     // Ignore for now
   }

   return(mp, 0x60)                  // 3 Words
 }

}

When I debugged memory on Remix, the first one had no pointer slot but only length and data. The second one had pointer, length and data slot.

My questions are:

  1. Why the solidity return has no pointer slot for the string in memory.
  2. Why remix is unable to decode if I remove the pointer slot from second one.
  3. What is the significance of pointer slot in this case ? When to use, when not to use ?

Thanks !

1 Answer 1

1

Return data should be ABI-encoded, same way as arguments, but excluding 4-byte selector. Solidity compiler handles ABI-encoding on its own - in assembly it’s your responsibility.

By the way revert usually contains ABI encoded result with selector of Error(string) or custom Solidity error selector.

ABI encoded parameters of variable length as strings and dynamic arrays are always presented as relative offsets to data containing length prefix.

3
  • Does that mean, solidity compiler keeps the pointer outside of EVM memory allocation when using solidity's return ?
    – Xirexor
    Commented Sep 13, 2023 at 20:46
  • @Xirexor nope, I would say solidity compiler allocates new memory for abi encoding and copy memory string with offset and length
    – k06a
    Commented Sep 21, 2023 at 18:02
  • Is the offset relative incase of calldata arguments too ?
    – Xirexor
    Commented Nov 21, 2023 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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