https://github.com/akkartik/mu/blob/master/apps/factorial.mu
1
2
3
4
5 fn main args: (addr array kernel-string) -> exit-status/ebx: int {
6 var a/eax: (addr array kernel-string) <- copy args
7 var tmp/ecx: int <- length a
8 $main-body: {
9 compare tmp, 1
10
11 {
12 break-if-!=
13 var tmp/eax: int <- factorial 5
14 exit-status <- copy tmp
15 break $main-body
16 }
17
18 var tmp2/ecx: int <- copy 1
19 var tmp3/ecx: (addr kernel-string) <- index a, tmp2
20 var tmp4/eax: boolean <- kernel-string-equal? *tmp3, "test"
21 compare tmp4, 0
22 {
23 break-if-=
24 run-tests
25 exit-status <- copy 0
26 }
27 }
28 }
29
30 fn factorial n: int -> result/eax: int {
31 compare n 1
32 {
33 break-if->
34 result <- copy 1
35 }
36 {
37 break-if-<=
38 var tmp/ecx: int <- copy n
39 tmp <- decrement
40 result <- factorial tmp
41 result <- multiply n
42 }
43 }
44
45 fn test-factorial {
46 var result/eax: int <- factorial 5
47 check-ints-equal result 0x78 "F - test-factorial"
48 }