Let's take a look at the Wasm text
(module
(func "fib" (param $n i32) (result i32)
(local $a i32) (local $b i32) ;; $a = 0; $b = 0
(set_local $b (i32.const 1)) ;; $b = 1
(loop $exit $next
(br_if $exit
(i32.le_s (get_local $n) (i32.const 0)))
(set_local $b
(i32.add
(get_local $a)
(tee_local $a (get_local $b))))
(set_local $n
(i32.sub
(get_local $n)
(i32.const 1)))
(br $next))
(return (get_local $b))))
Let's take a look at the Wasm text
(module
(func "fib" (param $n i32) (result i32)
(local $a i32) (local $b i32)
(set_local $b (i32.const 1))
(loop $exit $next ;; do {
(br_if $exit
(i32.le_s (get_local $n) (i32.const 0)))
(set_local $b
(i32.add
(get_local $a)
(tee_local $a (get_local $b))))
(set_local $n
(i32.sub
(get_local $n)
(i32.const 1)))
(br $next)) ;; continue; } while(0)
(return (get_local $b))))
Let's take a look at the Wasm text
(module
(func "fib" (param $n i32) (result i32)
(local $a i32) (local $b i32)
(set_local $b (i32.const 1))
(loop $exit $next
(br_if $exit ;; if ($n <= 0) break
(i32.le_s (get_local $n) (i32.const 0)))
(set_local $b
(i32.add
(get_local $a)
(tee_local $a (get_local $b))))
(set_local $n
(i32.sub
(get_local $n)
(i32.const 1)))
(br $next))
(return (get_local $b))))
Let's take a look at the Wasm text
(module
(func "fib" (param $n i32) (result i32)
(local $a i32) (local $b i32)
(set_local $b (i32.const 1))
(loop $exit $next
(br_if $exit
(i32.le_s (get_local $n) (i32.const 0)))
(set_local $b ;; $b = $b + $a
(i32.add
(get_local $a)
(tee_local $a (get_local $b)))) ;; $a = $b
(set_local $n
(i32.sub
(get_local $n)
(i32.const 1)))
(br $next))
(return (get_local $b))))
Let's take a look at the Wasm text
(module
(func "fib" (param $n i32) (result i32)
(local $a i32) (local $b i32)
(set_local $b (i32.const 1))
(loop $exit $next
(br_if $exit
(i32.le_s (get_local $n) (i32.const 0)))
(set_local $b
(i32.add
(get_local $a)
(tee_local $a (get_local $b))))
(set_local $n ;; $n = $n - 1
(i32.sub
(get_local $n)
(i32.const 1)))
(br $next))
(return (get_local $b))))
Let's take a look at the Wasm text
(module
(func "fib" (param $n i32) (result i32)
(local $a i32) (local $b i32)
(set_local $b (i32.const 1))
(loop $exit $next
(br_if $exit
(i32.le_s (get_local $n) (i32.const 0)))
(set_local $b
(i32.add
(get_local $a)
(tee_local $a (get_local $b))))
(set_local $n
(i32.sub
(get_local $n)
(i32.const 1)))
(br $next))
(return (get_local $b)))) ;; return $b
Let's take a look at the Wasm binary
00000000: 0061 736d 0c00 0000 0474 7970 6506 0140 .asm.....type..@
00000010: 0101 0101 0866 756e 6374 696f 6e02 0100 .....function...
00000020: 0665 7870 6f72 7406 0100 0366 6962 0463 .export....fib.c
00000030: 6f64 652b 0129 0102 0110 0115 0201 0214 ode+.)..........
00000040: 0010 0050 0700 0114 0114 0219 0140 1502 ...P.........@..
00000050: 1400 1001 4115 0006 0000 0f0f 1402 09 ....A..........
header
(version 0xc)
Let's take a look at the Wasm binary
00000000: 0061 736d 0c00 0000 0474 7970 6506 0140 .asm.....type..@
00000010: 0101 0101 0866 756e 6374 696f 6e02 0100 .....function...
00000020: 0665 7870 6f72 7406 0100 0366 6962 0463 .export....fib.c
00000030: 6f64 652b 0129 0102 0110 0115 0201 0214 ode+.)..........
00000040: 0010 0050 0700 0114 0114 0219 0140 1502 ...P.........@..
00000050: 1400 1001 4115 0006 0000 0f0f 1402 09 ....A..........
type section
(1 type; i32 -> i32)
Let's take a look at the Wasm binary
00000000: 0061 736d 0c00 0000 0474 7970 6506 0140 .asm.....type..@
00000010: 0101 0101 0866 756e 6374 696f 6e02 0100 .....function...
00000020: 0665 7870 6f72 7406 0100 0366 6962 0463 .export....fib.c
00000030: 6f64 652b 0129 0102 0110 0115 0201 0214 ode+.)..........
00000040: 0010 0050 0700 0114 0114 0219 0140 1502 ...P.........@..
00000050: 1400 1001 4115 0006 0000 0f0f 1402 09 ....A..........
function section
(1 function; using function type 0)
Let's take a look at the Wasm binary
00000000: 0061 736d 0c00 0000 0474 7970 6506 0140 .asm.....type..@
00000010: 0101 0101 0866 756e 6374 696f 6e02 0100 .....function...
00000020: 0665 7870 6f72 7406 0100 0366 6962 0463 .export....fib.c
00000030: 6f64 652b 0129 0102 0110 0115 0201 0214 ode+.)..........
00000040: 0010 0050 0700 0114 0114 0219 0140 1502 ...P.........@..
00000050: 1400 1001 4115 0006 0000 0f0f 1402 09 ....A..........
export section
(1 export; function 0, named "fib")
Let's take a look at the Wasm binary
00000000: 0061 736d 0c00 0000 0474 7970 6506 0140 .asm.....type..@
00000010: 0101 0101 0866 756e 6374 696f 6e02 0100 .....function...
00000020: 0665 7870 6f72 7406 0100 0366 6962 0463 .export....fib.c
00000030: 6f64 652b 0129 0102 0110 0115 0201 0214 ode+.)..........
00000040: 0010 0050 0700 0114 0114 0219 0140 1502 ...P.........@..
00000050: 1400 1001 4115 0006 0000 0f0f 1402 09 ....A..........
code section
(... really?)
oh, why not!
0000036: 01 ; local decl count
0000037: 02 01 ; 2 locals of type i32
0000039: 10 01 ; i32.const 1
000003b: 15 02 ; set_local 2
000003d: 01 ; block
000003e: 02 ; loop
000003f: 14 00 ; get_local 0
0000041: 10 00 ; i32.const 0
0000043: 50 ; i32.le_s
0000044: 07 00 01 ; br_if (arity = 0, depth = 1)
0000047: 14 01 ; get_local 1
0000049: 14 02 ; get_local 2
000004b: 19 01 ; tee_local 1
000004d: 40 ; i32.add
000004e: 15 02 ; set_local 2
0000050: 14 00 ; get_local 0
0000052: 10 01 ; i32.const 1
0000054: 41 ; i32.sub
0000055: 15 00 ; set_local 0
0000057: 06 00 00 ; br (arity = 0, depth = 0)
000005a: 0f ; end
000005b: 0f ; end
000005c: 14 02 ; get_local 2
000005e: 09 ; return
Generated x86-64 assembly
0 REX.W cmpq rsp,[r13+0xb90]
7 jna 62
13 xorl rdx,rdx ; a = 0
15 movl rbx,0x1 ; b = 1
20 nop
31 nop
32 cmpl rax,0x0
35 jle 58
41 leal rdx,[rdx+rbx*1]
44 subl rax,0x1
47 REX.W movq r10,rdx
50 REX.W movq rdx,rbx
53 REX.W movq rbx,r10
56 jmp 32
58 REX.W movq rax,rbx
61 retl
Generated x86-64 assembly
0 REX.W cmpq rsp,[r13+0xb90]
7 jna 62
13 xorl rdx,rdx
15 movl rbx,0x1
20 nop
31 nop
32 cmpl rax,0x0 ; if (n <= 0) break
35 jle 58
41 leal rdx,[rdx+rbx*1]
44 subl rax,0x1
47 REX.W movq r10,rdx
50 REX.W movq rdx,rbx
53 REX.W movq rbx,r10
56 jmp 32
58 REX.W movq rax,rbx
61 retl
Generated x86-64 assembly
0 REX.W cmpq rsp,[r13+0xb90]
7 jna 62
13 xorl rdx,rdx
15 movl rbx,0x1
20 nop
31 nop
32 cmpl rax,0x0
35 jle 58
41 leal rdx,[rdx+rbx*1] ; a = b + a
44 subl rax,0x1
47 REX.W movq r10,rdx
50 REX.W movq rdx,rbx
53 REX.W movq rbx,r10
56 jmp 32
58 REX.W movq rax,rbx
61 retl
Generated x86-64 assembly
0 REX.W cmpq rsp,[r13+0xb90]
7 jna 62
13 xorl rdx,rdx
15 movl rbx,0x1
20 nop
31 nop
32 cmpl rax,0x0
35 jle 58
41 leal rdx,[rdx+rbx*1]
44 subl rax,0x1 ; n = n - 1
47 REX.W movq r10,rdx
50 REX.W movq rdx,rbx
53 REX.W movq rbx,r10
56 jmp 32
58 REX.W movq rax,rbx
61 retl
Generated x86-64 assembly
0 REX.W cmpq rsp,[r13+0xb90]
7 jna 62
13 xorl rdx,rdx
15 movl rbx,0x1
20 nop
31 nop
32 cmpl rax,0x0
35 jle 58
41 leal rdx,[rdx+rbx*1]
44 subl rax,0x1
47 REX.W movq r10,rdx ; t = a
50 REX.W movq rdx,rbx ; a = b
53 REX.W movq rbx,r10 ; b = t
56 jmp 32
58 REX.W movq rax,rbx
61 retl
Generated x86-64 assembly
0 REX.W cmpq rsp,[r13+0xb90]
7 jna 62
13 xorl rdx,rdx
15 movl rbx,0x1
20 nop
31 nop
32 cmpl rax,0x0 ; here:
35 jle 58
41 leal rdx,[rdx+rbx*1]
44 subl rax,0x1
47 REX.W movq r10,rdx
50 REX.W movq rdx,rbx
53 REX.W movq rbx,r10
56 jmp 32 ; goto here
58 REX.W movq rax,rbx
61 retl
Generated x86-64 assembly
0 REX.W cmpq rsp,[r13+0xb90]
7 jna 62
13 xorl rdx,rdx
15 movl rbx,0x1
20 nop
31 nop
32 cmpl rax,0x0
35 jle 58
41 leal rdx,[rdx+rbx*1]
44 subl rax,0x1
47 REX.W movq r10,rdx
50 REX.W movq rdx,rbx
53 REX.W movq rbx,r10
56 jmp 32
58 REX.W movq rax,rbx ; return b
61 retl