Free to LearnSystems fieldbooksCode workshop
Fieldbooks/WebAssembly/Lesson 02
Lesson 02 of 08 · Course index

Lesson 02 / Stack & control

What does the add take?

Build the input check for an add. Picture a stack as a list you fill and empty at the right. The label on a value is its type. In i32 and i64, i means whole number and the number gives its width in bits. In f32 and f64, f means a floating-point number, which can represent fractions. Each add takes two values of its own type.

+ Figure 02
Two decimal inputs for the first addition. A single whole number for the last addition.
Top of stack is on the right

1 · The big idea

Unlike assembly languages that name CPU registers, WebAssembly is a stack machine: values sit on an ordered tray (top at the right). Every value carries an explicit type tag: i32 or i64 for whole integers (32 or 64 bits), and f32 or f64 for floating-point decimals.

Every instruction declares exact input and output types. For example, f32.add pops two f32 values and pushes one f32; drop discards the top item; i32.add pops two i32 values and pushes one i32.

If a reachable instruction asks for an i32 when the stack holds only one item (stack underflow) or holds an f64 (type mismatch), the verifier rejects the entire program before a single line runs.

2 · Worked step-by-step example

Step-by-step worked trace for lesson 2Stack Machine Tray: [f32, f32] -> f32.add -> drop -> [i32] -> i32.add[1.5, 2.25] f32f32.add + drop -> []i32.const 7 -> [7]Only 1 item on trayi32.add needs 2!Underflow -> REJECTPush i32.const 3 after 7 so both pops get i32 (result = 10).
Worked Example: Watching the Stack Through f32.add, drop, i32.add
  1. f32.const 1.5, f32.const 2.25 → stack is [f32, f32].
  2. f32.add pops two f32s and pushes their sum → stack is [f32].
  3. drop removes the top value → stack is empty [].
  4. i32.const 7 pushes one i32 → [i32]. Next, i32.add tries to pop two i32s: 1st pop gets i32, 2nd pop hits empty stack → rejected!
Result: Pushing i32.const 3 gives [i32, i32] so both pops succeed and runtime returns 10.

Read the case

[1.5 f32, 2.25 f32] → f32.add → [3.75 f32]

drop removes 3.75 → []

push 7 → [7 i32] → i32.add needs two

3 · Bytecode stepper (explore before quizzing)

Arity is the number of inputs. An add has arity two. Too few inputs cause stack underflow. Two inputs with the wrong labels cause a type mismatch. Both are errors the verifier can catch before running the code.

Bytecode is the list of machine instructions below. Step through Check types and Run values before answering the prediction below.

Instruction step
Ready0 / 6

Start with an empty stack. Read the instructions in order.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.const1 / 6

Push the type f32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.add

Operand stack top at right

f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.const2 / 6

Push the type f32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.add

Operand stack top at right

f32f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.add3 / 6

Check two f32 inputs. Push one f32 result.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.add

Operand stack top at right

f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

drop4 / 6

Remove one value of any type.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const5 / 6

Push the type i32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.add

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.add6 / 6

Stack underflow: i32 is missing at height 0.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.add

Operand stack before rejection · top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

Types rejected

Execution is blocked. Stack underflow: i32 is missing at height 0.

4 · Make a prediction

The first sum was removed. What happens when i32.add looks for its other input?

Compare your prediction. drop removed the sum. Move to the final add and count what remains on the stack.

Prediction confirmed. The first pop can take 7. The second finds an empty, reachable stack. The runner never starts.

Compare your prediction. There is no value below 7. To add zero here, the program must first put an i32 zero on the stack.

5 · Repair the program

Make the final i32.add return the whole number 10. Choose the value to push after 7.
Inspect this edit. The top value is a 64-bit integer. i32.add asks for a 32-bit integer on each pop.
Types rejectedRun blockedCalls 0Writes 0
Step through this edit
Instruction step
Ready0 / 7

Start with an empty stack. Read the instructions in order.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i64.const 3
  7. 06i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.const1 / 7

Push the type f32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i64.const 3
  7. 06i32.add

Operand stack top at right

f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.const2 / 7

Push the type f32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i64.const 3
  7. 06i32.add

Operand stack top at right

f32f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.add3 / 7

Check two f32 inputs. Push one f32 result.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i64.const 3
  7. 06i32.add

Operand stack top at right

f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

drop4 / 7

Remove one value of any type.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i64.const 3
  7. 06i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const5 / 7

Push the type i32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i64.const 3
  7. 06i32.add

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i64.const6 / 7

Push the type i64.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i64.const 3
  7. 06i32.add

Operand stack top at right

i32i64

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.add7 / 7

Type mismatch: expected i32, found i64.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i64.const 3
  7. 06i32.add

Operand stack before rejection · top at right

i32i64

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

Types rejected

Execution is blocked. Type mismatch: expected i32, found i64.

Inspect this edit. Three can be represented as a floating-point value. Its f64 label still fails the i32 input check.
Types rejectedRun blockedCalls 0Writes 0
Step through this edit
Instruction step
Ready0 / 7

Start with an empty stack. Read the instructions in order.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05f64.const 3
  7. 06i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.const1 / 7

Push the type f32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05f64.const 3
  7. 06i32.add

Operand stack top at right

f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.const2 / 7

Push the type f32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05f64.const 3
  7. 06i32.add

Operand stack top at right

f32f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.add3 / 7

Check two f32 inputs. Push one f32 result.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05f64.const 3
  7. 06i32.add

Operand stack top at right

f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

drop4 / 7

Remove one value of any type.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05f64.const 3
  7. 06i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const5 / 7

Push the type i32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05f64.const 3
  7. 06i32.add

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f64.const6 / 7

Push the type f64.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05f64.const 3
  7. 06i32.add

Operand stack top at right

i32f64

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.add7 / 7

Type mismatch: expected i32, found f64.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05f64.const 3
  7. 06i32.add

Operand stack before rejection · top at right

i32f64

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

Types rejected

Execution is blocked. Type mismatch: expected i32, found f64.

Repair confirmed. Both pops now find i32. Inspect the last runner step for the i32 result 10.
Types acceptedRun finishedCalls 0Writes 0
Step through this edit
Instruction step
Ready0 / 7

Start with an empty stack. Read the instructions in order.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.const1 / 7

Push the type f32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.const2 / 7

Push the type f32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

f32f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

f32.add3 / 7

Check two f32 inputs. Push one f32 result.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

f32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

drop4 / 7

Remove one value of any type.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const5 / 7

Push the type i32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const6 / 7

Push the type i32.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

i32i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.add7 / 7

Check two i32 inputs. Push one i32 result.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

Types accepted
Instruction step
Ready0 / 7

Start with an empty stack and one page of zero bytes.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

f32.const1 / 7

Push the value 1.5.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

f32 1.5

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

f32.const2 / 7

Push the value 2.25.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

f32 1.5f32 2.25

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

f32.add3 / 7

Add 1.5 and 2.25.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

f32 3.75

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

drop4 / 7

Discard the top value.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const5 / 7

Push the value 7.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

i32 7

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const6 / 7

Push the value 3.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

i32 7i32 3

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.add7 / 7

Add 7 and 3.

Bytecode instruction address

  1. 00f32.const 1.5
  2. 01f32.const 2.25
  3. 02f32.add
  4. 03drop
  5. 04i32.const 7
  6. 05i32.const 3
  7. 06i32.add

Operand stack top at right

i32 10

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

Run finished

Code workshop

Implement take in my-rules.mjs. Remove a value of the expected type. Report an empty stack or a mismatched type. The first command makes your own copy of the starter if needed. Edit the named function, then run the second command.

cp -n workshop/starter.mjs my-rules.mjs
node workshop/check.mjs 2 ./my-rules.mjs
Optional worked example in a separate file

Add a type check to lab.mjs. Call it twice for an add. Which call fails with only one i32?

function take(stack, want) {
  if (!stack.length) throw Error("underflow")
  if (stack.pop() !== want) throw Error("type mismatch")
}
const inputs = ["i32"]
take(inputs, "i32")
take(inputs, "i32")

The second call throws. Start with two i32 labels and both calls pass. Change the top label to i64 to exercise the other error. Remove the throwing calls before the next trial.

Key Takeaway & Next Up

Stack typing catches missing operands (underflow) and wrong bit-widths/types (like f64 vs. i32) statically before execution.

Next: Next, we trace how jumps (br) count nested blocks and loops outward.