diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-03-02 08:20:20 -0800 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-03-02 08:20:20 -0800 |
commit | 189d34dcff2ee968d4373254e208bb945adad234 (patch) | |
tree | 079a8898de450a36707810b5c0d1ab08f2d6c1ad /apps | |
parent | 2d4d1440e11b68687fe0aeb7b550d5d889e622ae (diff) | |
download | mu-189d34dcff2ee968d4373254e208bb945adad234.tar.gz |
6076
Diffstat (limited to 'apps')
-rw-r--r-- | apps/factorial.mu | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/apps/factorial.mu b/apps/factorial.mu index de5bcf54..fe435a53 100644 --- a/apps/factorial.mu +++ b/apps/factorial.mu @@ -2,6 +2,27 @@ # ./translate_mu apps/factorial.mu # ./a.elf test # to run tests # ./a.elf # to run factorial(5) + +fn factorial n: int -> result/eax: int { + compare n 1 + { + break-if-> + result <- copy 1 + } + { + break-if-<= + var tmp/ecx: int <- copy n + tmp <- decrement + result <- factorial tmp + result <- multiply n + } +} + +fn test-factorial { + var result/eax: int <- factorial 5 + check-ints-equal result 0x78 "F - test-factorial" +} + fn main args: (addr array kernel-string) -> exit-status/ebx: int { var a/eax: (addr array kernel-string) <- copy args var tmp/ecx: int <- length a @@ -26,23 +47,3 @@ fn main args: (addr array kernel-string) -> exit-status/ebx: int { } } } - -fn factorial n: int -> result/eax: int { - compare n 1 - { - break-if-> - result <- copy 1 - } - { - break-if-<= - var tmp/ecx: int <- copy n - tmp <- decrement - result <- factorial tmp - result <- multiply n - } -} - -fn test-factorial { - var result/eax: int <- factorial 5 - check-ints-equal result 0x78 "F - test-factorial" -} |