zombie-lib/atoi.asm

57 lines
536 B
NASM
Raw Normal View History

2021-10-02 20:58:37 -04:00
extern libz_strlen
global libz_atoi
section .text
libz_atoi:
; mov r11, rdi ;saveing it for later
2021-10-02 20:58:37 -04:00
push rdi
2021-10-02 20:58:37 -04:00
call libz_strlen
mov r9, rax
pop rdi
2021-10-04 15:14:11 -04:00
xor r10, r10
2021-10-02 20:58:37 -04:00
mov rcx, 1
add r9, rdi
dec r9
2021-10-02 20:58:37 -04:00
_loop:
2021-10-02 20:58:37 -04:00
xor rax, rax
mov al, [r9]
2021-10-02 20:58:37 -04:00
cmp rax, 48
jb _nan
cmp rax, 57 ;if its not a number then return 0 then exit
ja _nan
2021-10-02 20:58:37 -04:00
; push rax
sub rax, 48
mul rcx
add r10, rax
; pop rax
cmp r9, rdi
je _end
dec r9
; push rax
mov rax, 10
mul rcx
mov rcx, rax
; pop rax
2021-10-02 20:58:37 -04:00
jmp _loop
2021-10-02 20:58:37 -04:00
_nan:
xor r10 r10
2021-10-02 20:58:37 -04:00
_end:
mov rax, r10
2021-10-02 20:58:37 -04:00
ret