Overview
Opcode:0x58
Introduced: Frontier (EVM genesis)
PC pushes the current program counter value onto the stack. The program counter represents the position of the currently executing instruction in the bytecode.
This opcode enables position-aware bytecode, dynamic jump calculations, and self-referential code patterns.
Specification
Stack Input: None Stack Output:Behavior
PC provides the current execution position:- Consumes 2 gas (GasQuickStep)
- Pushes current PC value onto stack
- Increments PC to next instruction
0x02 onto the stack.
Examples
Basic PC Usage
Calculate Relative Position
Position-Aware Code
Dynamic Jump Table
Gas Cost
Cost: 2 gas (GasQuickStep) Comparison:- PC: 2 gas (read position)
- PUSH1: 3 gas (push constant)
- JUMP: 8 gas
- JUMPI: 10 gas
Edge Cases
PC at Start
PC at End
Stack Overflow
Multiple PC Calls
Common Usage
Relative Addressing
Calculate addresses relative to current position:Position Verification
Code Size Calculation
Dynamic Dispatch Base
Implementation
- TypeScript
Testing
Test Coverage
Security
Position-Dependent Code
PC enables position-dependent bytecode, which can be fragile:- Compiler optimizations can change positions
- Adding code shifts all offsets
- Hard to maintain and debug
Code Obfuscation
PC can be used for code obfuscation:- Makes auditing difficult
- Hides control flow
- Red flag for malicious code
- Avoid in production contracts
Limited Practical Use
PC has few legitimate use cases in modern Solidity: Not useful for:- Function dispatch (compiler handles this)
- Relative jumps (labels are better)
- Code size (codesize opcode exists)
- Gas optimization (avoid PUSH for position)
- Self-modifying code patterns (advanced, rare)
- Position verification in tests
Compiler Behavior
Solidity Avoids PC
Modern Solidity rarely generates PC instructions:Labels vs PC
Old pattern (PC-based):Optimization
Compilers can optimize PC away:Comparison with Other Chains
EVM (Ethereum)
PC returns bytecode position, 0-indexed.WASM
No direct equivalent. WASM uses structured control flow (blocks, loops) rather than position-based jumps.x86 Assembly
EIP (Extended Instruction Pointer) register similar to PC, but:- Hardware register, not stack
- 64-bit on x64, 32-bit on x86
- Can be read/written directly
JVM
No equivalent. JVM uses structured bytecode with exception tables rather than position-based control flow.Historical Context
PC was included in original EVM for:- Position-aware code patterns
- Relative addressing calculations
- Dynamic jump table construction
- Rarely needed in practice
- Compilers use labels instead
- Maintained for compatibility
- Occasionally useful for gas optimization
References
- Yellow Paper - Section 9.4.1 (PC instruction)
- EVM Codes - PC
- Solidity Docs - Assembly

