“Unlike hardware CPUs with physical registers (x86 RAX/RBX, ARM r0/r1), WebAssembly is a virtual Stack Machine. Instructions push values (i32, i64, f32, f64) onto an implicit evaluation stack and pop operands for mathematical and logical operations.”
How the evaluation stack pushes operands, pops arguments, and computes results.
(module
(func $calc (result i32)
i32.const 100
i32.const 20
i32.div_s ;; 100 / 20 = 5
i32.const 7
i32.mul ;; 5 * 7 = 35
)
(export "calc" (func $calc))
)const { instance } = await WebAssembly.instantiate(wasmBytes);
console.log('Computed value:', instance.exports.calc()); // 35Instruction i32.const 10 pushes 10 onto the evaluation stack: [10]
Instruction i32.const 25 pushes 25 onto the evaluation stack: [10, 25]
Instruction i32.add pops 25 and 10, calculates 10 + 25 = 35
i32.add pushes result 35 onto the stack: [35]
Function returns the remaining top-of-stack value to the caller
Single-pass register allocation converts abstract stack bytecode into optimal register-to-register assembly instructions (e.g. ADD EAX, EBX) during compilation.