From baf4910870a6e8999802b9a4a22eabd4142a34e3 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Thu, 1 Dec 2022 22:30:22 +0100 Subject: Move all Advent of Codes into one repo --- utils/utils.asm | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 utils/utils.asm (limited to 'utils/utils.asm') diff --git a/utils/utils.asm b/utils/utils.asm new file mode 100644 index 0000000..5b65f15 --- /dev/null +++ b/utils/utils.asm @@ -0,0 +1,43 @@ +global atoi, write_ulong + +%include "unistd.asm" + +section .text +atoi: + mov ecx, 10 + xor eax, eax +.loop: + movzx r8d, byte [rdi] + xor r8d, '0' + cmp r8d, ecx ; minor optimization, comparing with a register generates smaller machine code then comparing with an immediate + jnl .return + xor edx, edx + mul rcx + add eax, r8d + inc rdi ; Move ptr to next character + jmp .loop +.return: + ret + +write_ulong: + push rdi ; Push fd onto stack + mov rax, rsi + lea rcx, [rsp - 1] + mov byte [rcx], 0 + mov rsi, rcx + mov edi, 10 ; Divisor +.loop: + xor edx, edx + div rdi + xor edx, '0' + dec rsi + mov byte [rsi], dl + test rax, rax + jnz .loop + + mov rax, SYS_write + pop rdi ; Pop fd from stack + mov rdx, rcx + sub rdx, rsi + syscall + ret -- cgit v1.2.3