-
Notifications
You must be signed in to change notification settings - Fork 1
/
math.wat
32 lines (32 loc) · 1.14 KB
/
math.wat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(module
;; The string after the $ symbol specifies the name of the function.
;; Given a number compute its square.
;; Input
;; param i32
;;
;; Output
;; result i32
(func $square (param i32) (result i32)
local.get 0 ;; pull the input value and push it to the stack
local.get 0 ;; square function needs the same number twice
i32.mul ;; mul operation expects two numbers from the stack
;; Evaluation of arithmetic expressions using a stack
;;
;; (2 * 2)
;; would be written as
;;
;; 2 2 *
;; and evaluated like this
;;
;; Contents of the stack Program being evaluated
;;
;; [ ] | 2 2 *
;; [2] | 2 *
;; [2, 2] | *
;; [4] |
;;
;; The return value will be the final content of the stack after the execution.
)
;; export the previous function so javascript can run it.
(export "square" (func $square))
)