1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
; Advent of Code 2025 – Day 2
bits 64
default rel
global _start
%define SYS_EXIT 60
%define SYS_READ 0
%define SYS_WRITE 1
%define SYS_OPEN 2
%define SYS_CLOSE 3
%define O_RDONLY 0
%define STDOUT 1
section .data
inputPath db "input/day2.txt", 0
newline db 10
label1 db "Part 1: ", 0
label2 db "Part 2: ", 0
section .bss
buffer resb 4096
lineBuffer resb 65536
lineLen resd 1
sum1 resq 1
sum2 resq 1
numberBuffer resb 32 ; for printing and NumberToString
section .text
_start:
mov rax, SYS_OPEN
mov rdi, inputPath
mov rsi, O_RDONLY
xor rdx, rdx
syscall
mov r12, rax ; file descriptor
mov dword [lineLen], 0
ReadLoop:
mov rax, SYS_READ
mov rdi, r12
mov rsi, buffer
mov rdx, 4096
syscall
cmp rax, 0
jle EOF
mov rdi, buffer
mov rbp, rax
ScanChunk:
cmp rbp, 0
je ReadLoop
mov al, [rdi]
cmp al, 13
je SkipCR
cmp al, 10
je EndLine
mov edx, [lineLen]
cmp edx, 65535
jae SkipStore
mov [lineBuffer+rdx], al
inc edx
mov [lineLen], edx
SkipStore:
inc rdi
dec rbp
jmp ScanChunk
SkipCR:
inc rdi
dec rbp
jmp ScanChunk
EndLine:
mov edx, [lineLen]
mov byte [lineBuffer+rdx], 0
cmp edx, 0
je ClearLine
push rdi
mov rsi, lineBuffer
call HandleLine
pop rdi
ClearLine:
mov dword [lineLen], 0
inc rdi
dec rbp
jmp ScanChunk
EOF:
; process final partial line if needed
mov edx, [lineLen]
cmp edx, 0
jle PrintResults
mov byte [lineBuffer+rdx], 0
mov rsi, lineBuffer
call HandleLine
PrintResults:
mov rcx, label1
call PrintString
mov rax, [sum1]
call PrintInt64
call Newline
mov rcx, label2
call PrintString
mov rax, [sum2]
call PrintInt64
call Newline
mov rax, SYS_CLOSE
mov rdi, r12
syscall
mov rax, SYS_EXIT
xor rdi, rdi
syscall
; parse "A-B,C-D,..."l, RSI = pointer to line
; uses r13 = current ID, r14 = end of range
HandleLine:
NextRange:
; parse start
call ParseInt64
mov r13, rax
cmp byte [rsi], '-'
jne DoneLine
inc rsi
; parse end
call ParseInt64
mov r14, rax
RangeLoop:
cmp r13, r14
jg AfterRange
; Save RSI (line pointer) across the checks,
; because NumberToString / CheckInvalid* use RSI
push rsi
; Part 1: exact AA
mov rax, r13
call CheckInvalid1
test rax, rax
jz .skipP1
add qword [sum1], r13
.skipP1:
; Part 2: any repeating block >= 2
mov rax, r13
call CheckInvalid2
test rax, rax
jz .skipP2
add qword [sum2], r13
.skipP2:
pop rsi ; restore line pointer
inc r13
jmp RangeLoop
AfterRange:
cmp byte [rsi], ','
jne DoneLine
inc rsi
jmp NextRange
DoneLine:
ret
; RSI points at decimal digits; stops on first non-digit
ParseInt64:
xor rax, rax
.parse:
mov dl, [rsi]
cmp dl, '0'
jl .done
cmp dl, '9'
jg .done
sub dl, '0'
movzx rdx, dl
imul rax, rax, 10
add rax, rdx
inc rsi
jmp .parse
.done:
ret
; NumberToString: convert RAX to decimal ASCII, MSB-first
; Output:
; RSI = pointer to first digit
; ECX = length
NumberToString:
mov rdi, numberBuffer+31
mov byte [rdi], 0 ; terminator (for convenience)
dec rdi
cmp rax, 0
jne .conv
; handle zero
mov byte [rdi], '0'
mov rsi, rdi
mov ecx, 1
ret
.conv:
mov rbx, 10
.nextDigit:
xor rdx, rdx
div rbx ; RAX = quotient, RDX = remainder
add dl, '0'
mov [rdi], dl
dec rdi
test rax, rax
jnz .nextDigit
inc rdi ; now RDI points to first digit
mov rsi, rdi
mov rdx, numberBuffer+31
sub rdx, rsi ; RDX = length
mov ecx, edx
ret
; CheckInvalid1: "AA" pattern
; RAX = number
; Returns RAX = 1 if digits == A A, else 0
CheckInvalid1:
push rbx
push rdx
call NumberToString ; RSI=string, ECX=len
mov eax, ecx ; EAX = len
cmp eax, 2
jl .no1
test eax, 1
jnz .no1 ; odd length => cannot be AA
shr eax, 1 ; half = len/2
mov r8d, eax ; half
xor r9d, r9d ; i = 0
.loop1:
cmp r9d, r8d
jge .yes1
; s[i]
mov rdx, r9
movzx eax, byte [rsi + rdx]
; s[i+half]
mov rdx, r9
add rdx, r8
movzx ebx, byte [rsi + rdx]
cmp eax, ebx
jne .no1
inc r9d
jmp .loop1
.yes1:
mov rax, 1
jmp .ret1
.no1:
xor rax, rax
.ret1:
pop rdx
pop rbx
ret
; CheckInvalid2: any repeating block >= 2
; RAX = number
; Returns RAX = 1 if string is some A repeated k>=2 times
CheckInvalid2:
push rbx
push rdx
call NumberToString ; RSI=string, ECX=len
mov r11d, ecx ; n = length
cmp r11d, 2
jb .no2
mov r8d, 1 ; m = 1 (candidate period)
.outer:
cmp r8d, r11d
jge .no2
; if n % m != 0 => cannot tile
mov eax, r11d
xor edx, edx
div r8d ; EAX=q, EDX=r
test edx, edx
jnz .next_m
mov r9d, r11d
sub r9d, r8d ; limit = n - m
xor r10d, r10d ; i = 0
.inner:
cmp r10d, r9d
jge .yes2 ; all positions checked
; s[i]
mov rdx, r10
movzx eax, byte [rsi + rdx]
; s[i+m]
mov rdx, r10
add rdx, r8
movzx ebx, byte [rsi + rdx]
cmp eax, ebx
jne .next_m
inc r10d
jmp .inner
.yes2:
mov rax, 1
jmp .ret2
.next_m:
inc r8d
jmp .outer
.no2:
xor rax, rax
.ret2:
pop rdx
pop rbx
ret
Newline:
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, newline
mov rdx, 1
syscall
ret
PrintInt64:
mov rdi, numberBuffer+31
mov byte [rdi], 0
dec rdi
cmp rax, 0
jne .conv
mov byte [rdi], '0'
jmp .done
.conv:
mov rbx, 10
.next:
xor rdx, rdx
div rbx
add dl, '0'
mov [rdi], dl
dec rdi
test rax, rax
jnz .next
inc rdi
.done:
mov rax, SYS_WRITE
mov rsi, rdi
mov rdi, STDOUT
mov rdx, numberBuffer+31
sub rdx, rsi
syscall
ret
PrintString:
push rcx
push rdx
mov rdx, 0
.length:
cmp byte [rcx+rdx], 0
je .submit
inc rdx
jmp .length
.submit:
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, rcx
syscall
pop rdx
pop rcx
ret
|