https://github.com/akkartik/mu/blob/master/apps/parse-int.mu
 1 # parse a decimal int at the commandline
 2 #
 3 # To run:
 4 #   $ ./translate_mu apps/parse-int.mu
 5 #   $ ./a.elf 123
 6 #   $ echo $?
 7 #   123
 8 
 9 fn main _args: (addr array (addr array byte)) -> exit-status/ebx: int {
10 $main-body: {
11   # if no args, print a message and exit
12   var args/esi: (addr array (addr array byte)) <- copy _args
13   var n/ecx: int <- length args
14   compare n, 1
15   {
16     break-if->
17     print-string 0, "usage: parse-int <integer>\n"
18     exit-status <- copy 1
19     break $main-body
20   }
21   # otherwise parse the first arg as an integer
22   var in/ecx: (addr addr array byte) <- index args, 1
23   var out/eax: int <- parse-int *in
24   exit-status <- copy out
25 }
26 }
27 
28 fn parse-int _in: (addr array byte) -> result/eax: int {
29   var in/esi: (addr array byte) <- copy _in
30   var len/edx: int <- length in
31   var i/ecx: int <- copy 0
32   var out/edi: int <- copy 0
33   {
34     compare i, len
35     break-if->=
36     # out *= 10
37     var ten/eax: int <- copy 0xa
38     out <- multiply ten
39     # c = in[i]
40     var tmp/ebx: (addr byte) <- index in, i
41     var c/eax: byte <- copy 0
42     c <- copy-byte *tmp
43     #
44     var digit/eax: int <- to-decimal-digit c
45     out <- add digit
46     i <- increment
47     loop
48   }
49   result <- copy out
50 }