From 3c276e5263f7994b546b65716f0911d9c828c198 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 5 Sep 2026 19:32:30 +0200 Subject: refactor: add day-4-8 --- aoc-2025/day1.asm | 178 +++++++++++++++++++++++++++--------------------------- 1 file changed, 89 insertions(+), 89 deletions(-) (limited to 'aoc-2025/day1.asm') diff --git a/aoc-2025/day1.asm b/aoc-2025/day1.asm index 8ba8f0e..f130675 100644 --- a/aoc-2025/day1.asm +++ b/aoc-2025/day1.asm @@ -15,7 +15,7 @@ global _start %define STDOUT 1 section .data - inputPath db "input/day1.txt", 0 + input_path db "input/day1.txt", 0 newline db 10 label1 db "Password 1: ", 0 @@ -29,24 +29,24 @@ section .data section .bss buffer resb 4096 - lineBuffer resb 256 - numberBuffer resb 16 - lineLen resd 1 + line_buffer resb 256 + number_buffer resb 16 + line_len resd 1 section .text _start: ; open file mov rax, SYS_OPEN - mov rdi, inputPath + mov rdi, input_path mov rsi, O_RDONLY xor rdx, rdx syscall - mov r12, rax ; fd in R12 - mov dword [lineLen], 0 ; no chars in current line yet + mov r12, rax ; fd in R12 + mov dword [line_len], 0 ; no chars in current line yet -ReadLoop: +read_loop: ; read up to 4096 bytes mov rax, SYS_READ mov rdi, r12 @@ -55,80 +55,80 @@ ReadLoop: syscall cmp rax, 0 - jle EOF ; <=0 -> EOF or error + jle eof ; <=0 -> EOF or error - mov rdi, buffer ; scan pointer - mov rbp, rax ; bytes remaining in this chunk + mov rdi, buffer ; scan pointer + mov rbp, rax ; bytes remaining in this chunk -ScanChunk: +scan_chunk: cmp rbp, 0 - je ReadLoop ; chunk done, read next + je read_loop ; chunk done, read next mov al, [rdi] ; windows can be a fucking bitch ; handle (CR) -> just skip cmp al, 13 - je SkipCR + je skip_cr ; handle LF (10) -> end of line cmp al, 10 - je EndOfLine + je end_of_line - ; normal character -> append to lineBuffer if space left - mov edx, [lineLen] - cmp edx, 255 ; leave 1 byte for NULL - jae SkipStore ; ignore extra characters if too long - mov [lineBuffer+rdx], al + ; normal character -> append to line_buffer if space left + mov edx, [line_len] + cmp edx, 255 ; leave 1 byte for NULL + jae skip_store ; ignore extra characters if too long + 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 -EndOfLine: +end_of_line: ; terminate current line - mov edx, [lineLen] - mov byte [lineBuffer+rdx], 0 + mov edx, [line_len] + mov byte [line_buffer+rdx], 0 ; if line isn't empty, process it cmp edx, 0 - je LineDone + je line_done push rdi - mov rsi, lineBuffer - call HandleLine + mov rsi, line_buffer + call handle_line pop rdi -LineDone: - mov dword [lineLen], 0 ; reset line length +line_done: + mov dword [line_len], 0 ; reset line length inc rdi dec rbp - jmp ScanChunk + jmp scan_chunk ; process last line if not newline-terminated, then print result -EOF: +eof: ; if there is a partial line at EOF, process it - mov edx, [lineLen] + mov edx, [line_len] cmp edx, 0 - jle PrintResults - mov byte [lineBuffer+rdx], 0 + jle print_results + mov byte [line_buffer+rdx], 0 - mov rsi, lineBuffer - call HandleLine + mov rsi, line_buffer + call handle_line -PrintResults: +print_results: mov rcx, label1 - call PrintString + call print_string mov eax, [password1] - call PrintInt + call print_int ; newline mov rax, SYS_WRITE @@ -138,9 +138,9 @@ PrintResults: syscall mov rcx, label2 - call PrintString + call print_string mov eax, [password2] - call PrintInt + call print_int ; new linhe mov rax, SYS_WRITE @@ -157,67 +157,67 @@ PrintResults: syscall ; Handle one line: RSI -> "L123" or "R45" -HandleLine: +handle_line: mov al, [rsi] cmp al, 'L' je .left - mov ebx, 1 ; direction = +1 for 'R' or anything else + mov ebx, 1 ; direction = +1 for 'R' or anything else jmp .after .left: mov ebx, -1 ; direction = -1 for 'L' .after: - inc rsi ; skip 'L' or 'R' - call ParseInt ; EAX = steps + inc rsi ; skip 'L' or 'R' + call parse_int ; EAX = steps mov ecx, eax ; Part 1 -P1_Start: +p1_start: mov eax, ecx imul eax, ebx add eax, [angle1] - call NormalizeAngle + call normalize_angle mov [angle1], eax cmp eax, 0 - jne P2_Start + jne p2_start mov rdi, password1 mov edx, [rdi] inc edx mov [rdi], edx ; Part 2 -P2_Start: +p2_start: mov rdi, password2 .loop: test ecx, ecx - jle P2_Exit + jle p2_exit dec ecx mov eax, [angle2] - call UpdateAngleCount + call update_angle_count mov [angle2], eax jmp .loop -P2_Exit: +p2_exit: ret ; normalize eax to 0..99 -NormalizeAngle: +normalize_angle: cmp eax, 0 jl .add cmp eax, 100 jl .done sub eax, 100 - jmp NormalizeAngle + jmp normalize_angle .add: add eax, 100 - jmp NormalizeAngle + jmp normalize_angle .done: ret ; eax = angle, ebx = direction, rdi = password counter addr -UpdateAngleCount: +update_angle_count: add eax, ebx - call NormalizeAngle + call normalize_angle cmp eax, 0 jne .done @@ -228,29 +228,29 @@ UpdateAngleCount: ret ; parse ASCII integer into EAX -ParseInt: - xor eax, eax ; result = 0 -.parseLoop: +parse_int: + xor eax, eax ; result = 0 +.parse_loop: mov dl, [rsi] cmp dl, '0' jl .done cmp dl, '9' jg .done - sub dl, '0' ; digit value 0..9 + sub dl, '0' ; digit value 0..9 movzx edx, dl - imul eax, eax, 10 ; result *= 10 - add eax, edx ; result += digit + imul eax, eax, 10 ; result *= 10 + add eax, edx ; result += digit inc rsi - jmp .parseLoop + jmp .parse_loop .done: ret -PrintInt: - mov edi, numberBuffer+15 ; write digits from the end - mov byte [rdi], 0 ; terminator +print_int: + mov edi, number_buffer+15 ; write digits from the end + mov byte [rdi], 0 ; terminator dec rdi cmp eax, 0 @@ -262,48 +262,48 @@ PrintInt: ; q = eax / 10 ; r = eax % 10 ; using only subtraction -.nextDigit: - mov edx, eax ; tmp = current value - mov ecx, 0 ; q = 0 +.next_digit: + mov edx, eax ; tmp = current value + mov ecx, 0 ; q = 0 - cmp edx, 10 ; if < 0, q=0, r=edx - jb .lastDigit -.subLoop: + cmp edx, 10 ; if < 0, q=0, r=edx + jb .last_digit +.sub_loop: sub edx, 10 - inc ecx ; q++ + inc ecx ; q++ cmp edx, 10 - jae .subLoop ; while tmp >= 10 -.lastDigit: + jae .sub_loop ; while tmp >= 10 +.last_digit: ; now: ; ecx = quotient ; edx = remainder (0..9) add dl, '0' mov [rdi], dl ; store digit dec rdi - mov eax, ecx ; eax = quotient + mov eax, ecx ; eax = quotient test eax, eax - jnz .nextDigit ; more digits to extract? + jnz .next_digit ; more digits to extract? - inc rdi ; move to first digit + inc rdi ; move to first digit .finish: mov rax, SYS_WRITE - mov rsi, rdi ; buffer pointer + mov rsi, rdi ; buffer pointer mov rdi, STDOUT - mov rdx, numberBuffer+15 - sub rdx, rsi ; length + mov rdx, number_buffer+15 + sub rdx, rsi ; length syscall ret -PrintString: +print_string: push rcx push rdx mov rdx, 0 .count: cmp byte [rcx+rdx], 0 - je .doneCount + je .done_count inc rdx jmp .count -.doneCount: +.done_count: mov rax, SYS_WRITE mov rdi, STDOUT mov rsi, rcx -- cgit v1.3