https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/1b.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/edi: int <- copy 0
49 {
50 compare i, numbers-index
51 break-if->=
52
53 var j/edx: int <- copy i
54 j <- increment
55 {
56 compare j, numbers-index
57 break-if->=
58 {
59 compare i, j
60 break-if-=
61 var target/ebx: int <- copy 0x7e4
62 var src/edi: (addr int) <- index numbers, i
63 target <- subtract *src
64 var src2/ecx: (addr int) <- index numbers, j
65 target <- subtract *src2
66 {
67 var found?/eax: boolean <- find-after numbers, j, target
68 compare found?, 0/false
69 break-if-=
70 print-string 0, "found\n"
71 print-int32-decimal 0, *src
72 print-string 0, " "
73 print-int32-decimal 0, *src2
74 print-string 0, " "
75 print-int32-decimal 0, target
76 print-string 0, "\n"
77 target <- multiply *src
78 target <- multiply *src2
79 print-int32-decimal 0, target
80 print-string 0, "\n"
81 return 0/success
82 }
83 }
84 j <- increment
85 loop
86 }
87 i <- increment
88 loop
89 }
90 return 1/not-found
91 }
92
93 fn find-after _numbers: (addr array int), start: int, _target: int -> _/eax: boolean {
94 var numbers/esi: (addr array int) <- copy _numbers
95 var target/edi: int <- copy _target
96 var len/ecx: int <- length numbers
97 var i/eax: int <- copy start
98 i <- increment
99 {
100 compare i, len
101 break-if->=
102 var src/edx: (addr int) <- index numbers, i
103
104 compare *src, target
105 {
106 break-if-!=
107 return 1/true
108 }
109 i <- increment
110 loop
111 }
112 return 0/false
113 }