Skip to content

Instantly share code, notes, and snippets.

@DrFrankenstein
Created December 21, 2017 22:51
Show Gist options
  • Save DrFrankenstein/9db1a10a82ac5aaf8dc1cf59d390c75f to your computer and use it in GitHub Desktop.
Save DrFrankenstein/9db1a10a82ac5aaf8dc1cf59d390c75f to your computer and use it in GitHub Desktop.
(module
(import "console" "log" (func $console_log (param $val i32)))
(export "pe1" (func $pe1))
(func $pe1
(result i32)
(local $n i32)
(local $sum i32)
(set_local $sum (i32.const 0))
(set_local $n (i32.const 3))
loop $ml
block $bad
block $good
;; goto $good if n % 3 == 0
(br_if $good
(i32.eq
(i32.rem_u (get_local $n) (i32.const 3))
(i32.const 0)
)
)
;; goto $good if n % 5 == 0
(br_if $good
(i32.eq
(i32.rem_u (get_local $n) (i32.const 5))
(i32.const 0)
)
)
;; no match, goto $bad
br $bad
end $good
;; inc sum
(set_local $sum
(i32.add (get_local $sum) (i32.const 1))
)
end $bad
;; inc n and maybe loop
(tee_local $n
(i32.add (get_local $n) (i32.const 1))
)
(br_if $ml (i32.lt_u (i32.const 10000)))
end $ml
(call $console_log (get_local $sum))
get_local $sum
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment