WebAssembly Operators

What are Operators in WebAssembly?

In WebAssembly, operators are instructions executed on values pushed to the stack. Since WebAssembly is a stack-based language, all operations take their operands from the stack and push the results back onto the stack.

Operators are divided into several categories based on their functionality:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Memory Operators
  6. Control Flow Operators

1. Arithmetic Operators

Arithmetic operators perform basic mathematical calculations like addition, subtraction, multiplication, and division. They are type-specific (i32, i64, f32, f64).

Common Arithmetic Operators:

OperatorDescriptionExample in WebAssembly
i32.addAdds two 32-bit integers5 + 3 = 8
i64.subSubtracts two 64-bit integers10 – 7 = 3
f32.mulMultiplies two 32-bit floats4.5 * 2.0 = 9.0
f64.divDivides two 64-bit floats8.0 – 2.0 = 4.0

Example:

(module
(func $addNumbers (param $a i32) (param $b i32) (result i32)
local.get $a ;; Push $a onto the stack
local.get $b ;; Push $b onto the stack
i32.add ;; Add the two values
)
(export "addNumbers" (func $addNumbers))
)

2. Comparison Operators

Comparison operators evaluate two values and return a boolean result (0 for false, 1 for true). They are used for equality and relational checks.

Common Comparison Operators:

OperatorDescriptionExample in WebAssembly
i32.EQChecks if two values are equal5 == 5 -> 1
i32.neChecks if two values are not equal4 != 5 -> 1
i32.it_sChecks if the first is less than the second (signed)3 < 5 -> 1
i64.gt_uChecks if the first is greater than the second (unsigned)7 > 3 -> 1

Example:

(module
(func $compareValues (param $x i32) (param $y i32) (result i32)
local.get $x ;; Push $x onto the stack
local.get $y ;; Push $y onto the stack
i32.eq ;; Compare $x and $y for equality
)
(export "compareValues" (func $compareValues))
)

3. Logical Operators

Logical operators are used for Boolean logic operations like AND, OR and NOT.

Common Logical Operators:

OperatorDescriptionExample in WebAssembly
i32.andLogical AND1 AND 0 -> 0
i32.orLogical OR1 OR 0 -> 1
i32.xorLogical XOR (exclusive OR)1 XOR 1 -> 0

Example:

(module
(func $logicalAnd (param $a i32) (param $b i32) (result i32)
local.get $a ;; Push $a onto the stack
local.get $b ;; Push $b onto the stack
i32.and ;; Perform AND operation
)
(export "logicalAnd" (func $logicalAnd))
)

4. Bitwise Operators

Bitwise operators manipulate individual bits of integer values.

Common Bitwise Operators:

OperatorDescriptionExample in WebAssembly
i32.shlLeft shift4 << 1 -> 8
i32.shr_uRight shift (unsigned)8 >> 1 -> 4
i32.shr_sRight shift (signed)-8 >> 1 -> -4

Example:

(module
(func $bitwiseShift (param $x i32) (result i32)
local.get $x ;; Push $x onto the stack
i32.const 1 ;; Push the shift count
i32.shl ;; Perform left shift
)
(export "bitwiseShift" (func $bitwiseShift))
)

5. Memory Operators

Memory operators interact with the linear memory of the WebAssembly module.

Common Memory Operators:

OperatorDescriptionExample
i32.loadLoads a 32-bit value from memoryload from memory
i32.storeStores a 32-bit value into memoryStore to memory

Example:

(module
(memory $mem 1) ;; Define a memory block
(func $storeValue
i32.const 0 ;; Push the memory offset
i32.const 42 ;; Push the value to store
i32.store ;; Store the value in memory
)
(func $loadValue (result i32)
i32.const 0 ;; Push the memory offset
i32.load ;; Load the value from memory
)
(export "storeValue" (func $storeValue))
(export "loadValue" (func $loadValue))
)

6. Control Flow Operators

Control flow operators manage the execution flow of the program, such as loops and conditionals.

Common Control Flow Operators:

OperatorDescription
blockDefines a block of instructions
loopDefines a loop
ifExecutes instructions conditionally
brBreaks out of a block or loop

Example:

(module
(func $conditionalCheck (param $x i32) (result i32)
local.get $x
i32.const 10
i32.lt_s
if (result i32)
i32.const 1 ;; Return 1 if $x < 10
else
i32.const 0 ;; Return 0 otherwise
end
)
(export "conditionalCheck" (func $conditionalCheck))
)

Leave a Comment