hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/aoc-2025/day2.asm
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-09-05 19:32:30 +0200
committerhachem <im@hachem.wtf>2026-09-05 19:32:30 +0200
commit3c276e5263f7994b546b65716f0911d9c828c198 (patch)
treec1cccc2c58cb8b42b96c6c8c17ba1df7f24180cb /aoc-2025/day2.asm
parent9c9e26a5cab8f12f949be3ab6fea824f3d64a6ee (diff)
refactor: add day-4-8
Diffstat (limited to 'aoc-2025/day2.asm')
-rw-r--r--aoc-2025/day2.asm192
1 files changed, 96 insertions, 96 deletions
diff --git a/aoc-2025/day2.asm b/aoc-2025/day2.asm
index dfbfb38..c1ae232 100644
--- a/aoc-2025/day2.asm
+++ b/aoc-2025/day2.asm
@@ -15,7 +15,7 @@ global _start
%define STDOUT 1
section .data
- inputPath db "input/day2.txt", 0
+ input_path db "input/day2.txt", 0
newline db 10
label1 db "Part 1: ", 0
@@ -23,27 +23,27 @@ section .data
section .bss
buffer resb 4096
- lineBuffer resb 65536
- lineLen resd 1
+ line_buffer resb 65536
+ line_len resd 1
sum1 resq 1
sum2 resq 1
- numberBuffer resb 32 ; for printing and NumberToString
+ number_buffer resb 32 ; for printing and number_to_string
section .text
_start:
mov rax, SYS_OPEN
- mov rdi, inputPath
+ mov rdi, input_path
mov rsi, O_RDONLY
xor rdx, rdx
syscall
- mov r12, rax ; file descriptor
- mov dword [lineLen], 0
+ mov r12, rax ; file descriptor
+ mov dword [line_len], 0
-ReadLoop:
+read_loop:
mov rax, SYS_READ
mov rdi, r12
mov rsi, buffer
@@ -51,80 +51,80 @@ ReadLoop:
syscall
cmp rax, 0
- jle EOF
+ jle eof
mov rdi, buffer
mov rbp, rax
-ScanChunk:
+scan_chunk:
cmp rbp, 0
- je ReadLoop
+ je read_loop
mov al, [rdi]
cmp al, 13
- je SkipCR
+ je skip_cr
cmp al, 10
- je EndLine
+ je end_line
- mov edx, [lineLen]
+ mov edx, [line_len]
cmp edx, 65535
- jae SkipStore
- mov [lineBuffer+rdx], al
+ jae skip_store
+ mov [line_buffer+rdx], al
inc edx
- mov [lineLen], edx
+ mov [line_len], edx
-SkipStore:
+skip_store:
inc rdi
dec rbp
- jmp ScanChunk
+ jmp scan_chunk
-SkipCR:
+skip_cr:
inc rdi
dec rbp
- jmp ScanChunk
+ jmp scan_chunk
-EndLine:
- mov edx, [lineLen]
- mov byte [lineBuffer+rdx], 0
+end_line:
+ mov edx, [line_len]
+ mov byte [line_buffer+rdx], 0
cmp edx, 0
- je ClearLine
+ je clear_line
push rdi
- mov rsi, lineBuffer
- call HandleLine
+ mov rsi, line_buffer
+ call handle_line
pop rdi
-ClearLine:
- mov dword [lineLen], 0
+clear_line:
+ mov dword [line_len], 0
inc rdi
dec rbp
- jmp ScanChunk
+ jmp scan_chunk
-EOF:
+eof:
; process final partial line if needed
- mov edx, [lineLen]
+ mov edx, [line_len]
cmp edx, 0
- jle PrintResults
+ jle print_results
- mov byte [lineBuffer+rdx], 0
- mov rsi, lineBuffer
- call HandleLine
+ mov byte [line_buffer+rdx], 0
+ mov rsi, line_buffer
+ call handle_line
-PrintResults:
+print_results:
mov rcx, label1
- call PrintString
+ call print_string
mov rax, [sum1]
- call PrintInt64
- call Newline
+ call print_int64
+ call print_newline
mov rcx, label2
- call PrintString
+ call print_string
mov rax, [sum2]
- call PrintInt64
- call Newline
+ call print_int64
+ call print_newline
mov rax, SYS_CLOSE
mov rdi, r12
@@ -136,58 +136,58 @@ PrintResults:
; parse "A-B,C-D,..."l, RSI = pointer to line
; uses r13 = current ID, r14 = end of range
-HandleLine:
-NextRange:
+handle_line:
+next_range:
; parse start
- call ParseInt64
+ call parse_int64
mov r13, rax
cmp byte [rsi], '-'
- jne DoneLine
+ jne done_line
inc rsi
; parse end
- call ParseInt64
+ call parse_int64
mov r14, rax
-RangeLoop:
+range_loop:
cmp r13, r14
- jg AfterRange
+ jg after_range
; Save RSI (line pointer) across the checks,
- ; because NumberToString / CheckInvalid* use RSI
+ ; because number_to_string / check_invalid* use RSI
push rsi
; Part 1: exact AA
mov rax, r13
- call CheckInvalid1
+ call check_invalid1
test rax, rax
- jz .skipP1
+ jz .skip_p1
add qword [sum1], r13
-.skipP1:
+.skip_p1:
; Part 2: any repeating block >= 2
mov rax, r13
- call CheckInvalid2
+ call check_invalid2
test rax, rax
- jz .skipP2
+ jz .skip_p2
add qword [sum2], r13
-.skipP2:
- pop rsi ; restore line pointer
+.skip_p2:
+ pop rsi ; restore line pointer
inc r13
- jmp RangeLoop
+ jmp range_loop
-AfterRange:
+after_range:
cmp byte [rsi], ','
- jne DoneLine
+ jne done_line
inc rsi
- jmp NextRange
+ jmp next_range
-DoneLine:
+done_line:
ret
; RSI points at decimal digits; stops on first non-digit
-ParseInt64:
+parse_int64:
xor rax, rax
.parse:
mov dl, [rsi]
@@ -206,13 +206,13 @@ ParseInt64:
.done:
ret
-; NumberToString: convert RAX to decimal ASCII, MSB-first
+; number_to_string: 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)
+number_to_string:
+ mov rdi, number_buffer+31
+ mov byte [rdi], 0 ; terminator (for convenience)
dec rdi
cmp rax, 0
@@ -226,41 +226,41 @@ NumberToString:
.conv:
mov rbx, 10
-.nextDigit:
+.next_digit:
xor rdx, rdx
- div rbx ; RAX = quotient, RDX = remainder
+ div rbx ; RAX = quotient, RDX = remainder
add dl, '0'
mov [rdi], dl
dec rdi
test rax, rax
- jnz .nextDigit
+ jnz .next_digit
- inc rdi ; now RDI points to first digit
+ inc rdi ; now RDI points to first digit
mov rsi, rdi
- mov rdx, numberBuffer+31
- sub rdx, rsi ; RDX = length
+ mov rdx, number_buffer+31
+ sub rdx, rsi ; RDX = length
mov ecx, edx
ret
-; CheckInvalid1: "AA" pattern
+; check_invalid1: "AA" pattern
; RAX = number
; Returns RAX = 1 if digits == A A, else 0
-CheckInvalid1:
+check_invalid1:
push rbx
push rdx
- call NumberToString ; RSI=string, ECX=len
- mov eax, ecx ; EAX = len
+ call number_to_string ; RSI=string, ECX=len
+ mov eax, ecx ; EAX = len
cmp eax, 2
jl .no1
test eax, 1
- jnz .no1 ; odd length => cannot be AA
+ jnz .no1 ; odd length => cannot be AA
- shr eax, 1 ; half = len/2
- mov r8d, eax ; half
- xor r9d, r9d ; i = 0
+ shr eax, 1 ; half = len/2
+ mov r8d, eax ; half
+ xor r9d, r9d ; i = 0
.loop1:
cmp r9d, r8d
@@ -293,20 +293,20 @@ CheckInvalid1:
pop rbx
ret
-; CheckInvalid2: any repeating block >= 2
+; check_invalid2: any repeating block >= 2
; RAX = number
; Returns RAX = 1 if string is some A repeated k>=2 times
-CheckInvalid2:
+check_invalid2:
push rbx
push rdx
- call NumberToString ; RSI=string, ECX=len
- mov r11d, ecx ; n = length
+ call number_to_string ; RSI=string, ECX=len
+ mov r11d, ecx ; n = length
cmp r11d, 2
jb .no2
- mov r8d, 1 ; m = 1 (candidate period)
+ mov r8d, 1 ; m = 1 (candidate period)
.outer:
cmp r8d, r11d
jge .no2
@@ -314,16 +314,16 @@ CheckInvalid2:
; if n % m != 0 => cannot tile
mov eax, r11d
xor edx, edx
- div r8d ; EAX=q, EDX=r
+ 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
+ sub r9d, r8d ; limit = n - m
+ xor r10d, r10d ; i = 0
.inner:
cmp r10d, r9d
- jge .yes2 ; all positions checked
+ jge .yes2 ; all positions checked
; s[i]
mov rdx, r10
@@ -352,7 +352,7 @@ CheckInvalid2:
pop rbx
ret
-Newline:
+print_newline:
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, newline
@@ -360,8 +360,8 @@ Newline:
syscall
ret
-PrintInt64:
- mov rdi, numberBuffer+31
+print_int64:
+ mov rdi, number_buffer+31
mov byte [rdi], 0
dec rdi
@@ -386,12 +386,12 @@ PrintInt64:
mov rax, SYS_WRITE
mov rsi, rdi
mov rdi, STDOUT
- mov rdx, numberBuffer+31
+ mov rdx, number_buffer+31
sub rdx, rsi
syscall
ret
-PrintString:
+print_string:
push rcx
push rdx
mov rdx, 0