From 054d372a716bca4f628674e524e3d027f6824019 Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 6 Sep 2026 04:12:25 +0200 Subject: refactor: clean up repository layout --- utils/io.asm | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/parse.asm | 22 ++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 utils/io.asm create mode 100644 utils/parse.asm (limited to 'utils') diff --git a/utils/io.asm b/utils/io.asm new file mode 100644 index 0000000..2081671 --- /dev/null +++ b/utils/io.asm @@ -0,0 +1,107 @@ +; utils/io.asm - shared syscall constants, file reading, and output helpers. +; This file is %included (not linked) by each day's solution. + +%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 + newline db 10 + +section .bss + number_buffer resb 32 + +section .text + +; read_file: open the path in RDI read-only, read up to RDX bytes into the +; buffer in RSI, close, and return the number of bytes read in RAX. +read_file: + mov r8, rsi ; buffer base + mov r9, rdx ; capacity + mov rax, SYS_OPEN + mov rsi, O_RDONLY + xor rdx, rdx + syscall + mov r10, rax ; fd + xor r11, r11 ; bytes read so far +.loop: + mov rax, SYS_READ + mov rdi, r10 + lea rsi, [r8 + r11] + mov rdx, r9 + sub rdx, r11 + syscall + cmp rax, 0 + jle .done + add r11, rax + jmp .loop +.done: + mov rax, SYS_CLOSE + mov rdi, r10 + syscall + mov rax, r11 + ret + +print_newline: + mov rax, SYS_WRITE + mov rdi, STDOUT + mov rsi, newline + mov rdx, 1 + syscall + ret + +; print_int64: print the unsigned integer in RAX in decimal. +print_int64: + mov rdi, number_buffer+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, number_buffer+31 + sub rdx, rsi + syscall + ret + +; print_string: print the NUL-terminated string pointed to by RCX. +print_string: + 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 diff --git a/utils/parse.asm b/utils/parse.asm new file mode 100644 index 0000000..833cb4e --- /dev/null +++ b/utils/parse.asm @@ -0,0 +1,22 @@ +; utils/parse.asm - decimal parsing helper. + +section .text + +; parse_u64: read decimal digits at [RSI] into RAX, advancing RSI past them. +; Stops on the first non-digit. Clobbers RAX, RDX. +parse_u64: + xor rax, rax +.digit: + 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 .digit +.done: + ret -- cgit v1.3