1

Why does mstore(c, p) not copy p to c?

function test()returns (bytes32) {
    bytes32 c;
    bytes32 p = 0xFF;
    assembly {
        mstore(c, p)
    }
    return c;
}
0

enter image description here

The mstore instruction "Save a word to memory". Essentially, it stores at address mu_s[0] (the value of c) of the memory the value of the variable p. So with your assembly instruction, you are storing at address 0 of memory the value 0xff.

The problem is that c is not stored in memory (and in particular not at this address), but rather it is stored in Stack (according to this link).

So to copy the value of p in c, you have to write this assembly code (I know it is less cool, but it works):

 assembly {
    c := p
 }

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.