> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# PUSH3 (0x62)

> Push 3-byte immediate value onto stack

<Warning>
  **This page is a placeholder.** All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
</Warning>

## Overview

**Opcode:** `0x62`
**Introduced:** Frontier (EVM genesis)

PUSH3 pushes a 3-byte immediate value from the bytecode onto the stack. The 3 bytes immediately following the opcode are read and zero-padded to 256 bits.

## Specification

**Stack Input:**

```
[]
```

**Stack Output:**

```
value (uint256, 3 bytes from bytecode)
```

**Gas Cost:** 3 (GasFastestStep)

**Bytecode:** 1 byte opcode + 3 bytes immediate data

**Operation:**

```
value = read_bytes(pc + 1, 3)  // Big-endian
stack.push(value)
pc += 4
```

## Behavior

PUSH3 reads 3 bytes from bytecode starting at position `pc + 1`, interprets them as a big-endian unsigned integer, and pushes the result onto the stack.

Key characteristics:

* Reads exactly 3 bytes following opcode
* Big-endian byte order (most significant byte first)
* Zero-padded to 256 bits if less than 32 bytes
* InvalidOpcode if insufficient bytecode remaining
* PC advances by 4 (opcode + data)

## Examples

### Basic Usage

```typescript theme={null}
import { handler_0x62_PUSH3 } from '@tevm/voltaire/evm/stack/handlers';
import { createFrame } from '@tevm/voltaire/evm/Frame';

// Bytecode with PUSH3
const bytecode = new Uint8Array([
  0x62,  // PUSH3
  0x01, 0x02, 0x03   // 3 bytes: 010203
]);

const frame = createFrame({
  bytecode,
  pc: 0,
  stack: [],
  gasRemaining: 1000n
});

const err = handler_0x62_PUSH3(frame);

console.log(frame.stack); // [0x0102030000000000000000000000000000000000000000000000000000000000n]
console.log(frame.pc); // 4
console.log(frame.gasRemaining); // 997n (3 gas consumed)
```

### Solidity Compilation

```solidity theme={null}
contract Example {
    // Function selectors use PUSH4
    function transfer() public {
        // PUSH4 0xa9059cbb  (4-byte selector)
    }
}
```

### Assembly Usage

```solidity theme={null}
assembly {
    // Push 3-byte value
    push3 0xffffff
    
    // Example: 3-byte constant
}
```

## Gas Cost

**Cost:** 3 gas (GasFastestStep)

All PUSH1-32 instructions cost the same despite different data sizes. Bytecode size impact:

* PUSH3: 4 bytes (1 opcode + 3 data)
* PUSH32: 33 bytes (1 opcode + 32 data)

**Comparison:**

| Opcode | Gas | Bytes | Use Case                  |
| ------ | --- | ----- | ------------------------- |
| PUSH0  | 2   | 1     | Zero constant (Shanghai+) |
| PUSH1  | 3   | 2     | Small numbers (0-255)     |

## Common Usage

### 3-Byte Constants

```solidity theme={null}
assembly {
    // 3-byte literal
    push3 0xababab
}
```

### Big-Endian Encoding

```typescript theme={null}
// Bytecode: PUSH3 01 02 03
// Reads as: 0x010203

// Most significant byte first
// Byte 0: 0x01 (highest significance)
// Byte 2: 0x03 (lowest significance)
```

## Implementation

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    /**
     * Read immediate data from bytecode for PUSH operations
     */
    function readImmediate(bytecode: Uint8Array, pc: number, size: number): bigint | null {
      if (pc + 1 + size > bytecode.length) {
        return null;
      }

      let result = 0n;
      for (let i = 0; i < size; i++) {
        result = (result << 8n) | BigInt(bytecode[pc + 1 + i]);
      }
      return result;
    }

    /**
     * PUSH3 opcode (0x62) - Push 3 bytes onto stack
     *
     * Stack: [] => [value]
     * Gas: 3 (GasFastestStep)
     */
    export function handler_0x62_PUSH3(frame: FrameType): EvmError | null {
      const gasErr = consumeGas(frame, FastestStep);
      if (gasErr) return gasErr;

      const value = readImmediate(frame.bytecode, frame.pc, 3);
      if (value === null) {
        return { type: "InvalidOpcode" };
      }

      const pushErr = pushStack(frame, value);
      if (pushErr) return pushErr;

      frame.pc += 4;
      return null;
    }
    ```
  </Tab>
</Tabs>

## Edge Cases

### Insufficient Bytecode

```typescript theme={null}
// Bytecode ends before 3 bytes read
const bytecode = new Uint8Array([0x62, 0x01]); // Only 1 byte instead of 3
const frame = createFrame({ bytecode, pc: 0 });

const err = handler_0x62_PUSH3(frame);
console.log(err); // { type: "InvalidOpcode" }
```

### Stack Overflow

```typescript theme={null}
// Stack at maximum capacity
const frame = createFrame({
  stack: new Array(1024).fill(0n),
  bytecode: new Uint8Array([0x62, 0x00, 0x00, 0x00])
});

const err = handler_0x62_PUSH3(frame);
console.log(err); // { type: "StackOverflow" }
```

### Out of Gas

```typescript theme={null}
// Insufficient gas
const frame = createFrame({
  gasRemaining: 2n,  // Need 3 gas
  bytecode: new Uint8Array([0x62, 0xff, 0xff, 0xff])
});

const err = handler_0x62_PUSH3(frame);
console.log(err); // { type: "OutOfGas" }
```

### Maximum Value

```typescript theme={null}
// All bytes 0xFF
const bytecode = new Uint8Array([0x62, 0xff, 0xff, 0xff]);
const frame = createFrame({ bytecode, pc: 0 });

handler_0x62_PUSH3(frame);
console.log(frame.stack[0]); // 0xffffff0000000000000000000000000000000000000000000000000000000000n
```

## References

* [Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf) - Section 9.4.1 (PUSH)
* [EVM Codes - PUSH3](https://www.evm.codes/#62?fork=cancun)
* [Solidity Assembly - push3](https://docs.soliditylang.org/en/latest/yul.html#evm-opcodes)
