TeachMeBitcoin

OP_OVER - Copying Second Item to Top

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

8. OP_OVER — Copying Second Item to Top

Overview

OP_OVER copies the second-from-top item on the stack and pushes that copy to the top. The original item remains in place. This is useful when a value that is currently second on the stack is needed for an operation but must remain available for use again afterward.

Opcode Reference

Opcode:     OP_OVER
Hex:        0x79
Word:       OVER
Input:      x1 x2
Output:     x1 x2 x1
Stack Before: [ ..., x1, x2 ]
Stack After:  [ ..., x1, x2, x1 ]   ← copy of x1 placed on top

Execution Trace

Script: OP_5 OP_8 OP_OVER

Step 1: Stack: [ 5 ]

Step 2: Stack: [ 5, 8 ]

Step 3 — OP_OVER: Stack: [ 5, 8, 5 ]   ← 5 is copied to top

Key Insight: OVER vs DUP

OP_DUP:  copies the TOP item      → [ x1, x2 ] → [ x1, x2, x2 ]
OP_OVER: copies the SECOND item   → [ x1, x2 ] → [ x1, x2, x1 ]

Practical Use: Re-checking a Value

OP_OVER is used when a value already on the stack must be used in a computation while also being preserved for a later operation:

Script: <a> <b> OP_OVER OP_ADD OP_OVER OP_SUB

Start:     Stack: [ a, b ]
OP_OVER:   Stack: [ a, b, a ]   ← copy of a
OP_ADD:    Stack: [ a, (b+a) ]
OP_OVER:   Stack: [ a, (b+a), a ]
OP_SUB:    Stack: [ a, (b+a-a) ] = [ a, b ]

This demonstrates how OP_OVER enables non-destructive access to the second stack item.

Use in Script Constructs

In some custom Bitcoin scripts and sidechains that allow arithmetic, OP_OVER is used to implement comparison patterns:

<value1> <value2>
OP_OVER         ← copy value1 to top
OP_GREATERTHAN  ← compare

Failure Conditions

Summary

OP_OVER provides non-destructive read access to the second stack element, enabling compact script patterns that reuse values without requiring redundant push operations.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!