Skip to main content
Share Your Experience: Take the 2024 Developer Survey
7 votes
Accepted

Confusion about mload opcode

There are 2 points that you should clearly understand in assembly context : 1 . All variables are value type in assembly There is no such thing as a "reference type" in assembly, for ...
hroussille's user avatar
  • 7,721
6 votes
Accepted

Yul/Inline Assembly: Revert with a custom error message

This kind of code: require(_amountToRaise > 0, "Amount to raise smaller than 0"); Produces revert with bytes equivalent to calldata of the following method: function Error(string memory ...
k06a's user avatar
  • 3,036
5 votes

Why does mload(0x40) result in 0x20?

Your reasoning is correct, but only for values of type bytes32 or uint256. The next code will return 0x80 function mloadTest1() public pure returns(bytes32) { bytes32 a; assembly { a :=...
Alexey Smirnov's user avatar
4 votes
Accepted

sstore struct within mapping, inline assembly

getValues() is an example of how I'm able to read something from storage. I can read the first two values using and() and shr(). But how do I read the next values within struct? The storage layout of ...
hroussille's user avatar
  • 7,721
4 votes

Set data for call(), delegatecall(), etc, in Yul / inline assembly?

There are several issues with your code : You are not handling memory correctly You are not encoding your parameters correctly You are not computing the data size correctly The delegatecall opcode ...
hroussille's user avatar
  • 7,721
4 votes
Accepted

Is it possible to compile to YUL?

Yep, you can use the compiler option solc --ir a.sol to see the Yul output.
Leonardo Alt's user avatar
4 votes

Yul/Inline Assembly: Revert with a custom error message

First I think you mixed some hexadecimal notation with some decimal ones, the correct decomposition should be : if iszero(gt(_amountToRaise, 0)) { mstore(0x80, shl(229, 4594637)) mstore(0x84, ...
hroussille's user avatar
  • 7,721
4 votes
Accepted

Why is constant variable value changing?

You don't change the constant, but you write to the storage slot v = 5 and read from it. As can be read in the Assembly Docs, the sstore command takes the arguments p and v, where p is the pointer to ...
Markus Schick's user avatar
4 votes

How do I return a string in a pure Yul contract?

Okay figured it out. Here is how: Basic example of how the function would work (with hardcoded string, point is to show how the memory should be prepared) - I am not showing the surrounding code, just ...
Jesper - jtk.eth's user avatar
4 votes
Accepted

How does `shr` read from storage?

You're right,shr is only a logical right shift operator in Solidity's inline assembly. The shr operation in the code provided is not reading the address argument from storage, but rather the code line ...
Ahmed Ihsan Tawfeeq's user avatar
4 votes
Accepted

Why does mload(0x40) result in 0x20?

Unlike value types like bytesN or uintN reference types (denoted by their memory, calldata or storage annotation) are merely pointers to their underlying data. When you use inline-assembly to set a ...
Philogy's user avatar
  • 627
3 votes

Is it safe to enable the IR flag in the current version of Solidity?

The decision to enable the IR flag in the Solidity compiler depends on the specific context of your project and your risk tolerance. While the IR flag may generate more optimized bytecode, it is still ...
Talha Sajid Chaudhary's user avatar
3 votes
Accepted

Does `delegatecall` create a new stack frame?

Yes, delegatecall creates a new stack frame.
Patrick Collins's user avatar
3 votes

Calling private functions using Yul ( Solidity )

tl;dr, to answer your question: Is there any way at all to call private functions using inline assembly? Yes, it is possible, but not with the call() instruction. Long answer + extras: If you're ...
BonisTech's user avatar
  • 664
2 votes
Accepted

Is it possible for the Solidity compiler to remove an external call?

Nothing is impossible but if the CALL in your example actually got removed, it would be a bug in the compiler. The only case I'm aware of where the compiler (I mean specifically the code generator) ...
cameel's user avatar
  • 2,241
2 votes

Why do I still get stack too deep error?

Looks like it's still pretty experimental, not default, and you have to make some other changes too: https://twitter.com/solidity_lang/status/1506215112507633668 The important part: You can enable ...
thefett's user avatar
  • 3,890
2 votes

Right Shift not working in inline assembly

Let's take a look at the definition of the shr Yul instruction: shr(x, y): logical shift right y by x bits Your function always returns 0 because you are shifting by x (your variable x) bits, not by ...
Paul Razvan Berg's user avatar
2 votes
Accepted

Q: `extcodesize` is non-zero after `selfdestruct`

I was able to reproduce your issue with the following code on Remix (You can check the different behavior between calling extcodesize in the same tx or in a subsequent one) pragma solidity ^0.8.10; ...
hroussille's user avatar
  • 7,721
2 votes
Accepted

how to get the data after mstore in yul?

The fn should change the last 32bytes in data, but the function returns the data unchanged. So how to get the actually changed data ? There is a depper issue with your code. The provided data seems ...
hroussille's user avatar
  • 7,721
2 votes

Why would I not want to use intermediate representation (IR) in Solidity v0.8.13 and above?

It takes much longer to compile with Yul.
thorstenhirsch's user avatar
2 votes
Accepted

meaning of `slot` property in Yul?

A slot is a word used to describe the location of stack and storage in Yul. You can take a look at this resource to learn more about how slot works: https://docs.soliditylang.org/en/v0.8.13/assembly....
Yongjian P.'s user avatar
  • 4,200
2 votes
Accepted

How does console.logging work (e.g. Foundry's Console.sol)?

The 0x000000000000000000636F6e736F6c652e6c6f67 address is for hardhat local blockchain that we use for testing our contracts locally, in the dev invironment. So, under the hoods, hardhat is sending ...
Jeremy Then's user avatar
  • 4,609
2 votes
Accepted

What is the best practice for memory allocation in EVM assembly?

Their code wants probably to keep the first two slots as scratch space, so they start the free memory at 0x60. ptr := mload(0x40) is 0 the first time allocate is called. So without this line if iszero(...
0xSanson's user avatar
  • 3,249
2 votes
Accepted

Is there any difference between the "new Contract()" syntax and the assembly "create" opcode?

The only meaningful difference is that the first version checks that the deployment was successful. The address returned from create can indeed be address(0), and in this case it means that the ...
0xSanson's user avatar
  • 3,249
2 votes

Unknown key "viaIR"

viaIR: true may require version 0.8.13 and higher.
EthanOK's user avatar
  • 451
2 votes
Accepted

Assembly 'create2' and 'new' with salt not determining the same address

"0x4" is interpreted as a string, so the two salts are different. bytes32("0x4") != 4
0xSanson's user avatar
  • 3,249
2 votes

Calling private functions using Yul ( Solidity )

When compiling internal and private functions, selectors are not created on them. This means that they cannot be called by passing the address of the function selector. Theoretically, you could find ...
Alexey Smirnov's user avatar
2 votes

Calling private functions using Yul ( Solidity )

This is not possible. As Alexey said, a function selector is not created for private variables. Whenever someone calls a solidity smart contract, the first thing that contract does is figure out where ...
Patrick Collins's user avatar
1 vote
Accepted

Can someone explain to me how returns work under the hood in solidity?

i've found the answer to the question https://twitter.com/AmadiMichaels/status/1619284675146686464?s=20
Malik Aremu's user avatar
1 vote

Why does storing values at non-32 multiple memory locations spill over to next location?

I think it works exactly as described by the EVM bytecode MSTORE: MSTORE (uint256 offset, uint256 value) -> memory[offset:offset+32] = value EVM uses big-endian, your value is 0x0443455243 = ...
minhhn2910's user avatar
  • 1,760

Only top scored, non community-wiki answers of a minimum length are eligible