2021-10-02 20:58:37 -04:00
|
|
|
extern libz_strlen
|
|
|
|
|
|
|
|
global libz_atoi
|
|
|
|
|
|
|
|
section .text
|
|
|
|
|
|
|
|
libz_atoi:
|
|
|
|
|
|
|
|
|
2021-10-04 16:34:02 -04:00
|
|
|
; mov r11, rdi ;saveing it for later
|
2021-10-02 20:58:37 -04:00
|
|
|
|
2021-10-04 16:34:02 -04:00
|
|
|
push rdi
|
2021-10-02 20:58:37 -04:00
|
|
|
call libz_strlen
|
2021-10-04 16:34:02 -04:00
|
|
|
mov r9, rax
|
|
|
|
pop rdi
|
2021-10-04 15:14:11 -04:00
|
|
|
|
2021-10-04 16:34:02 -04:00
|
|
|
xor r10, r10
|
2021-10-02 20:58:37 -04:00
|
|
|
|
|
|
|
mov rcx, 1
|
2021-10-04 16:34:02 -04:00
|
|
|
add r9, rdi
|
|
|
|
dec r9
|
2021-10-02 20:58:37 -04:00
|
|
|
|
2021-10-04 16:34:02 -04:00
|
|
|
_loop:
|
2021-10-02 20:58:37 -04:00
|
|
|
|
2021-10-04 16:34:02 -04:00
|
|
|
xor rax, rax
|
|
|
|
mov al, [r9]
|
2021-10-02 20:58:37 -04:00
|
|
|
|
2021-11-21 03:21:48 -05: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
|
|
|
|
2021-10-04 16:34:02 -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
|
|
|
|
2021-10-04 16:34:02 -04:00
|
|
|
jmp _loop
|
2021-10-02 20:58:37 -04:00
|
|
|
|
2021-11-21 03:21:48 -05:00
|
|
|
_nan:
|
|
|
|
xor r10 r10
|
2021-10-02 20:58:37 -04:00
|
|
|
|
2021-10-04 16:34:02 -04:00
|
|
|
_end:
|
|
|
|
mov rax, r10
|
2021-10-02 20:58:37 -04:00
|
|
|
ret
|
|
|
|
|