BitcoinMachine

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.

0 / 4 sections

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.

Script
OP_1
OP_2
OP_3
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1Pushes the number 1 onto the stack.
0 stepsReady
The top of the stack is always the most recently pushed item. When an opcode pops something, it always takes from the top.
Real Bitcoin

Why no variables?

Every Bitcoin node re-executes your script independently. Variables would require state shared across nodes. The stack is the only state — it's created fresh per validation and discarded when done.

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

Script
OP_5
OP_7
OP_11
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
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_DUP

Duplicates the top item. The original stays; a copy is pushed on top. P2PKH uses this to keep the public key after hashing.

Script
OP_7
OP_DUP
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_7Pushes the number 7 onto the stack.
0 stepsReady
OP_DROP

Discards the top item. Useful for cleaning up intermediate values the script no longer needs.

Script
OP_1
OP_2
OP_3
OP_DROP
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1Pushes the number 1 onto the stack.
0 stepsReady
OP_SWAP

Exchanges the top two items. Before: [a, b] (b on top). After: [b, a] (a on top).

Script
OP_1
OP_2
OP_SWAP
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1Pushes the number 1 onto the stack.
0 stepsReady
OP_OVER

Copies the second item to the top without removing it. Before: [a, b]. After: [a, b, a].

Script
OP_3
OP_9
OP_OVER
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_3Pushes the number 3 onto the stack.
0 stepsReady
DUP copies the top, DROP removes it, SWAP flips the top two, OVER copies the second to top. These four cover the vast majority of real scripts.

04

Your turn

Apply what you've learned. Solve the challenge to unlock Module 2.

Challenge

Double seven

Write a script that leaves exactly two 7s on the stack — one on the bottom, one on top. Two opcodes only.

Ctrl+Enter to check

← Home