Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Fibonacci-sequence/WebAssembly/fibonacci-sequence.wasm
Normal file
46
Task/Fibonacci-sequence/WebAssembly/fibonacci-sequence.wasm
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
(func $fibonacci_nth (param $n i32) (result i32)
|
||||
|
||||
;;Declare some local registers
|
||||
(local $i i32)
|
||||
(local $a i32)
|
||||
(local $b i32)
|
||||
|
||||
;;Handle first 2 numbers as special cases
|
||||
(if (i32.eq (get_local $n) (i32.const 0))
|
||||
(return (i32.const 0))
|
||||
)
|
||||
(if (i32.eq (get_local $n) (i32.const 1))
|
||||
(return (i32.const 1))
|
||||
)
|
||||
|
||||
;;Initialize first two values
|
||||
(set_local $i (i32.const 1))
|
||||
(set_local $a (i32.const 0))
|
||||
(set_local $b (i32.const 1))
|
||||
|
||||
(block
|
||||
(loop
|
||||
;;Add two previous numbers and store the result
|
||||
local.get $a
|
||||
local.get $b
|
||||
i32.add
|
||||
(set_local $a (get_local $b))
|
||||
set_local $b
|
||||
|
||||
;;Increment counter i by one
|
||||
(set_local $i
|
||||
(i32.add
|
||||
(get_local $i)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
|
||||
;;Check if loop is done
|
||||
(br_if 1 (i32.ge_u (get_local $i) (get_local $n)))
|
||||
(br 0)
|
||||
)
|
||||
)
|
||||
|
||||
;;The result is stored in b, so push that to the stack
|
||||
get_local $b
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue