diff options
| author | Bond_009 <bond.009@outlook.com> | 2022-12-01 22:30:22 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2022-12-01 22:30:22 +0100 |
| commit | baf4910870a6e8999802b9a4a22eabd4142a34e3 (patch) | |
| tree | 2d11443dc21e53bd0d99d015cf789937d6d95862 /utils/utils.asm | |
| parent | 49d0c908f24b2c193c9deed1716fe36061ba26a1 (diff) | |
Move all Advent of Codes into one repo
Diffstat (limited to 'utils/utils.asm')
| -rw-r--r-- | utils/utils.asm | 43 |
1 files changed, 43 insertions, 0 deletions
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 |
