include	clib.inc
ifndef __386__
 include math.inc
endif

_TEXT	SEGMENT

atol proc dist pascal public uses ds si di string:dword
	lds si,string
	cld
    atol_start:
	lodsb
	cmp al,' '
	je atol_start
	mov bh,al
	cmp al,'-'
	je atol_inc
	cmp al,'+'
	jne atol_set
    atol_inc:
	lodsb
    atol_set:
  ifdef __386__
  	xor ecx,ecx
  else
	xor cx,cx
	mov di,cx
  endif
    atol_loop:
	cmp al,'0'
	jb atol_break
	cmp al,'9'
	ja atol_break
	mov bl,al
  ifdef __386__
	mov eax,10
	mul ecx
	mov ecx,eax
	movzx eax,bl
	sub al,'0'
	add ecx,eax
  else
	xchg bx,di
	xor dx,dx
	mov ax,10
	call MUL32
	mov bx,di
	mov bh,0
	sub bl,'0'
	add ax,bx
	adc dx,0
	mov cx,dx
	mov di,ax
  endif
	lodsb
	jmp atol_loop
    atol_break:
	cmp bh,'-'
	je atol_neg
    atol_end:
  ifdef __386__
  	push ecx
        pop ax
        pop dx
        ret
    atol_neg:
  	not ecx
  else
	mov ax,di
	mov dx,cx
	ret
    atol_neg:
	neg cx
	neg di
	sbb cx,0
  endif
	jmp atol_end
atol endp

_TEXT	ENDS

	END
