https://github.com/akkartik/mu/blob/main/linux/advent2020/1a.mu
1
2
3
4
5
6
7
8
9
10
11
12
13
14 fn main -> _/ebx: int {
15
16 var numbers-storage: (array int 0x100)
17 var numbers/esi: (addr array int) <- address numbers-storage
18 var numbers-index/ecx: int <- copy 0
19
20 {
21 var line-storage: (stream byte 0x100)
22 var line/edx: (addr stream byte) <- address line-storage
23 {
24
25
26 clear-stream line
27 read-line-from-real-keyboard line
28
29 var done?/eax: boolean <- stream-empty? line
30 compare done?, 0/false
31 break-if-!=
32
33
34 var n/eax: int <- parse-decimal-int-from-stream line
35
36
37 var dest/ebx: (addr int) <- index numbers, numbers-index
38 copy-to *dest, n
39 numbers-index <- increment
40
41
42
43 loop
44 }
45 }
46
47
48 var i/eax: int <- copy 0
49 {
50 compare i, numbers-index
51 break-if->=
52 var src/ebx: (addr int) <- index numbers, i
53
54
55 var target/ecx: int <- copy 0x7e4
56 target <- subtract *src
57 {
58 var found?/eax: boolean <- find-after numbers, i, target
59 compare found?, 0/false
60 break-if-=
61 print-string 0, "found\n"
62 print-int32-decimal 0, *src
63 print-string 0, " "
64 print-int32-decimal 0, target
65 print-string 0, "\n"
66 target <- multiply *src
67 print-int32-decimal 0, target
68 print-string 0, "\n"
69 return 0/success
70 }
71 i <- increment
72 loop
73 }
74 return 1/not-found
75 }
76
77 fn find-after _numbers: (addr array int), start: int, _target: int -> _/eax: boolean {
78 var numbers/esi: (addr array int) <- copy _numbers
79 var target/edi: int <- copy _target
80 var len/ecx: int <- length numbers
81 var i/eax: int <- copy start
82 i <- increment
83 {
84 compare i, len
85 break-if->=
86 var src/edx: (addr int) <- index numbers, i
87
88 compare *src, target
89 {
90 break-if-!=
91 return 1/true
92 }
93 i <- increment
94 loop
95 }
96 return 0/false
97 }