https://github.com/akkartik/mu/blob/main/linux/parse-int.mu
1
2
3
4
5
6
7
8
9 fn main _args: (addr array addr array byte) -> _/ebx: int {
10
11 var args/esi: (addr array addr array byte) <- copy _args
12 var n/ecx: int <- length args
13 compare n, 1
14 {
15 break-if->
16 print-string 0/screen, "usage: parse-int <integer>\n"
17 return 1
18 }
19
20 var in/ecx: (addr addr array byte) <- index args, 1
21 var out/eax: int <- parse-int *in
22 return out
23 }
24
25 fn parse-int _in: (addr array byte) -> _/eax: int {
26 var in/esi: (addr array byte) <- copy _in
27 var len/edx: int <- length in
28 var i/ecx: int <- copy 0
29 var result/edi: int <- copy 0
30 {
31 compare i, len
32 break-if->=
33
34 var ten/eax: int <- copy 0xa
35 result <- multiply ten
36
37 var tmp/ebx: (addr byte) <- index in, i
38 var c/eax: byte <- copy-byte *tmp
39
40 var g/eax: grapheme <- copy c
41 var digit/eax: int <- to-decimal-digit g
42 result <- add digit
43 i <- increment
44 loop
45 }
46 return result
47 }