https://github.com/akkartik/mu/blob/main/linux/parse-int.mu
 1 # parse a decimal int at the commandline
 2 #
 3 # To run:
 4 #   $ ./translate parse-int.mu
 5 #   $ ./a.elf 123
 6 #   $ echo $?
 7 #   123
 8 
 9 fn main _args: (addr array addr array byte) -> _/ebx: int {
10   # if no args, print a message and exit
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   # otherwise parse the first arg as an integer
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     # result *= 10
34     var ten/eax: int <- copy 0xa
35     result <- multiply ten
36     # c = in[i]
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 }