0

I was reading about the EVM dialect in the docs, and saw this:

Instruction Explanation
pc() Current position in code

I went to Remix to try it out:

function read_pc() external view returns (uint256 result) {
    assembly {
        result := pc()
    }
}

But I was given this error:

SyntaxError. PC instruction is a low-level EVM feature. Because of that PC is disallowed in strict assembly.

If that doesn't work, is there another way to read the current program counter (PC) in either Solidity or Yul?

2

The ability to use the PC opcode directly in Solidity was removed in Solidity 0.7. However, you may be able to use PC again in the future via the verbatim assembly builtin. Currently, you can only use verbatim in contracts written in yul. If the Solidity team decides to expose verbatim for normal Solidity contracts, it will allow you to inject arbitrary bytecode into your contract to get the PC as follows:

function read_pc() public view returns (uint256 result) {
  assembly {
    result := verbatim_0i_1o(hex"58")
  }
}

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.