https://github.com/akkartik/mu/blob/master/apps/tile/int-stack.mu
 1 type int-stack {
 2   data: (handle array int)
 3   top: int
 4 }
 5 
 6 fn initialize-int-stack _self: (addr int-stack), n: int {
 7   var self/esi: (addr int-stack) <- copy _self
 8   var d/edi: (addr handle array int) <- get self, data
 9   populate d, n
10   var top/eax: (addr int) <- get self, top
11   copy-to *top, 0
12 }
13 
14 fn clear-int-stack _self: (addr int-stack) {
15   var self/esi: (addr int-stack) <- copy _self
16   var top/eax: (addr int) <- get self, top
17   copy-to *top, 0
18 }
19 
20 fn push-int-stack _self: (addr int-stack), _val: int {
21   var self/esi: (addr int-stack) <- copy _self
22   var top-addr/ecx: (addr int) <- get self, top
23   var data-ah/edx: (addr handle array int) <- get self, data
24   var data/eax: (addr array int) <- lookup *data-ah
25   var top/edx: int <- copy *top-addr
26   var dest-addr/edx: (addr int) <- index data, top
27   var val/eax: int <- copy _val
28   copy-to *dest-addr, val
29   add-to *top-addr, 1
30 }
31 
32 fn pop-int-stack _self: (addr int-stack) -> val/eax: int {
33 $pop-int-stack:body: {
34   var self/esi: (addr int-stack) <- copy _self
35   var top-addr/ecx: (addr int) <- get self, top
36   {
37     compare *top-addr, 0
38     break-if->
39     val <- copy 0
40     break $pop-int-stack:body
41   }
42   subtract-from *top-addr, 1
43   var data-ah/edx: (addr handle array int) <- get self, data
44   var data/eax: (addr array int) <- lookup *data-ah
45   var top/edx: int <- copy *top-addr
46   var result-addr/eax: (addr int) <- index data, top
47   val <- copy *result-addr
48 }
49 }
50 
51 fn int-stack-empty? _self: (addr int-stack) -> result/eax: boolean {
52 $int-stack-empty?:body: {
53   var self/esi: (addr int-stack) <- copy _self
54   var top-addr/eax: (addr int) <- get self, top
55   compare *top-addr, 0
56   {
57     break-if-!=
58     result <- copy 1  # true
59     break $int-stack-empty?:body
60   }
61   result <- copy 0  # false
62 }
63 }
64 
65 fn int-stack-length _self: (addr int-stack) -> result/eax: int {
66   var self/esi: (addr int-stack) <- copy _self
67   var top-addr/eax: (addr int) <- get self, top
68   result <- copy *top-addr
69 }
70 
71 fn int-stack-max-width _self: (addr int-stack) -> result/eax: int {
72   var self/esi: (addr int-stack) <- copy _self
73   var data-ah/edi: (addr handle array int) <- get self, data
74   var _data/eax: (addr array int) <- lookup *data-ah
75   var data/edi: (addr array int) <- copy _data
76   var top-addr/ecx: (addr int) <- get self, top
77   var i/ebx: int <- copy 0
78   result <- copy 0
79   {
80     compare i, *top-addr
81     break-if->=
82     var g/edx: (addr int) <- index data, i
83     var w/ecx: int <- int-width-decimal *g
84     compare w, result
85     {
86       break-if-<=
87       result <- copy w
88     }
89     i <- increment
90     loop
91   }
92 }