hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/aoc-2025/day1.asm
diff options
context:
space:
mode:
Diffstat (limited to 'aoc-2025/day1.asm')
-rw-r--r--aoc-2025/day1.asm96
1 files changed, 5 insertions, 91 deletions
diff --git a/aoc-2025/day1.asm b/aoc-2025/day1.asm
index f130675..928e7a5 100644
--- a/aoc-2025/day1.asm
+++ b/aoc-2025/day1.asm
@@ -5,18 +5,10 @@ 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
+%include "utils/io.asm"
section .data
input_path db "input/day1.txt", 0
- newline db 10
label1 db "Password 1: ", 0
label2 db "Password 2: ", 0
@@ -30,7 +22,6 @@ section .data
section .bss
buffer resb 4096
line_buffer resb 256
- number_buffer resb 16
line_len resd 1
section .text
@@ -128,26 +119,14 @@ print_results:
mov rcx, label1
call print_string
mov eax, [password1]
- call print_int
-
- ; newline
- mov rax, SYS_WRITE
- mov rdi, STDOUT
- mov rsi, newline
- mov rdx, 1
- syscall
+ call print_int64
+ call print_newline
mov rcx, label2
call print_string
mov eax, [password2]
- call print_int
-
- ; new linhe
- mov rax, SYS_WRITE
- mov rdi, STDOUT
- mov rsi, newline
- mov rdx, 1
- syscall
+ call print_int64
+ call print_newline
mov rax, SYS_CLOSE
mov rdi, r12
@@ -247,68 +226,3 @@ parse_int:
jmp .parse_loop
.done:
ret
-
-print_int:
- mov edi, number_buffer+15 ; write digits from the end
- mov byte [rdi], 0 ; terminator
- dec rdi
-
- cmp eax, 0
- jne .convert
- mov byte [rdi], '0'
- jmp .finish
-.convert:
- ; we will repeatedly compute:
- ; q = eax / 10
- ; r = eax % 10
- ; using only subtraction
-.next_digit:
- mov edx, eax ; tmp = current value
- mov ecx, 0 ; q = 0
-
- cmp edx, 10 ; if < 0, q=0, r=edx
- jb .last_digit
-.sub_loop:
- sub edx, 10
- inc ecx ; q++
- cmp edx, 10
- 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
- test eax, eax
- jnz .next_digit ; more digits to extract?
-
- inc rdi ; move to first digit
-.finish:
- mov rax, SYS_WRITE
- mov rsi, rdi ; buffer pointer
- mov rdi, STDOUT
- mov rdx, number_buffer+15
- sub rdx, rsi ; length
- syscall
- ret
-
-print_string:
- push rcx
- push rdx
- mov rdx, 0
-.count:
- cmp byte [rcx+rdx], 0
- je .done_count
- inc rdx
- jmp .count
-.done_count:
- mov rax, SYS_WRITE
- mov rdi, STDOUT
- mov rsi, rcx
- syscall
-
- pop rdx
- pop rcx
- ret