Wasm Introduction (Part 2): Instruction Set & Operand Stack

Wasm Instruction Set

Like the real machine code, the code in the Wasm binary file is also composed of instructions. Similarly, the Wasm instruction also contains two pieces of information: the opcode and operands. The Wasm opcode is fixed to one byte, so it can represent up to 256 instructions, which is the same as the Java bytecode. The Wasml.0 specification defines a total of 172 instructions. These instructions can be divided into 5 categories by function:

  • Control Instructions, a total of 13.
  • Parametric Instructions, a total of 2.
  • Variable Instructions, a total of 5.
  • Memory Instructions, a total of 25.
  • Numeric Instructions, a total of 127.

Parametric Instructions

There are two parametric instructions: drop (whose opcode is 0x1A) and select (whose opcode is 0x1B).

drop

The drop instruction pops an operand of any type from the top of the stack. There is no immediate arguments for the drop instruction. Its schematic diagram is shown as below:

select

The select instruction successively pops 3 operands from the top of the stack. If the first popped operand is zero, the second popped operand is pushed onto the stack, otherwise the third popped operand is pushed onto the stack. The select instruction does not have immediate arguments either. Here is a diagram of it:

Numeric Instructions

Numeric instructions can be divided into four groups of i32, i64, f32, and f64 according to the operand type. Each group of instructions can be further divided into five groups according to the operation.

  • Constant Instructions
  • Test Instructions
  • Comparison Instructions
  • Arithmetic Instructions
  • Unary Arithmetic Instructions
  • Binary Arithmetic Instructions
  • Conversion Instructions

Constant Instructions

The constant instruction pushes the immediate argument to the top of the stack. Take the i32.const instruction (opcode 0x41) as an example. The following is its diagram:

Test Instructions

The test instruction pops an operand from the top of the stack and tests whether it is 0. If it is, it pushes 1 (as i32) onto the stack; otherwise it pushes 0 (as i32) onto the stack. There are only two test instructions: i32.eqz (opcode 0x45) and i64.eqz (opcode 0x50). Take the i64.eqz instruction as an example. Here is a diagram of it:

Comparison Instructions

The comparison instruction pops two operands of the same type from the top of the stack, compares them, and then pushes the result onto the stack. Like the test instruction, the result of the comparison instruction is also a boolean value (i.e., i32 type). Take the i64.lt_s instruction (Opcode 0x53) as an example. The following is its diagram:

Unary Arithmetic Instructions

Unary arithmetic instructions pop an operand from the top of the stack, perform calculations, and then push the result to the top of the stack. Take f32.neg (opcode 0x8C) instruction as an example. The following is a schematic diagram of it:

Binary Arithmetic Instructions

Binary arithmetic instructions pop two operands from the top of the stack, perform calculations, and then push the result to the top of the stack. Take f32.sub instruction (Opcode 0x93) as an example. Its schematic diagram is shown as below:

Conversion Instruction

The conversion instruction pops an operand from the top of the stack, performs type conversion, and then pushes the result to the top of the stack. If the type of the operand before conversion is t, the type after conversion is t', and the conversion operation is conv, then the mnemonic of the instruction is t'.conv_t. Take the i32.wrap_i64 (opcode 0xA7) instruction as an example. The following is a schematic diagram of it:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CoinEx Smart Chain

CoinEx Smart Chain

A public chain built for the decentralized exchange. Website: www.coinex.org Telegram: t.me/CoinExChain