The explanation is quite simple:
- In Solidity, bytes is a dynamically-sized byte array:
Variables of type bytes and string are special arrays. A bytes is
similar to byte[], but it is packed tightly in calldata. string is
equal to bytes but does not allow length or index access (for now).
In Solidity Assembly, variables are pointers to memory addresses.
Memory and storage are managed in chunks of 32 bytes.
The first chunk of memory of an Array stores the length of that array.
Now let's look at your code:
b = new bytes(32);
This creates a variable named b of the type bytes.
By 1, we know now the b will be treated as an array.
By 2, we know that b will point to its memory address.
By 3 and 4 we now know that b will point to the length of it, and only 32 bytes after is the memory address where the value for b will start.
We know that add(b, 32)
will return the memory address of b added by 32 (in other words, the memory address where to store the value for b).
So, finally we know why mstore(add(b, 32), x)
makesb = x
.