Module 01
The Stack
Bitcoin Script has no variables, no heap, no registers. There is one data structure: a stack. Every opcode either pushes something onto it or pops something off. That's the entire machine.
What you'll learn
- →Understand what a stack is and why Bitcoin Script uses one
- →Execute OP_DUP, OP_DROP, OP_SWAP, OP_OVER from memory
- →Predict the stack state after any sequence of push/pop operations
- →Explain why there are no variables or loops in Bitcoin Script
01
Last in, first out
Picture a stack of plates. You can only put a plate on top, and you can only take a plate from the top. The last plate you added is the first one you remove. Computer scientists call this LIFO — last in, first out.
In Bitcoin Script every piece of data is a plate. Step through the script below and watch the stack grow.
↑ top of stack
nothing here yet
press Step or Run to push an item
Why no variables?
02
Pushing values
Bitcoin Script has dedicated push opcodes for 0–16 (OP_0 through OP_16) plus OP_1NEGATE for −1. For larger numbers write them directly (e.g. 100 or 0xdeadbeef).
Try it — edit the script and press Run.
Ctrl+Enter to run
↑ top of stack
nothing here yet
press Step or Run to push an item
OP_0 and OP_FALSE are the same opcode. So are OP_1 and OP_TRUE.03
Moving things around
Pushing values is only half the story. Here are the four manipulation opcodes you'll use most.
OP_DUPDuplicates the top item. The original stays; a copy is pushed on top. P2PKH uses this to keep the public key after hashing.
↑ top of stack
nothing here yet
press Step or Run to push an item
OP_DROPDiscards the top item. Useful for cleaning up intermediate values the script no longer needs.
↑ top of stack
nothing here yet
press Step or Run to push an item
OP_SWAPExchanges the top two items. Before: [a, b] (b on top). After: [b, a] (a on top).
↑ top of stack
nothing here yet
press Step or Run to push an item
OP_OVERCopies the second item to the top without removing it. Before: [a, b]. After: [a, b, a].
↑ top of stack
nothing here yet
press Step or Run to push an item
04
Your turn
Apply what you've learned. Solve the challenge to unlock Module 2.
Double seven
Ctrl+Enter to check