home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS - Coast to Coast
/
simteldosarchivecoasttocoast.iso
/
pcmag
/
vol7n03.zip
/
PP703.ZIP
/
ATOI.ASM
next >
Wrap
Assembly Source File
|
1987-12-15
|
3KB
|
88 lines
name atoi
page 55,132
title ATOI - ASCII to integer
;
; ATOI.ASM - convert ASCII string to
; 16-bit decimal integer.
;
; Copyright 1987 Ziff Communications Co.
; Ray Duncan
;
; Call with: DS:SI = address of string
;
; where 'string' is in the form
;
; [whitespace][sign][digits]
;
; Returns: AX = result
; DS:SI = address+1 of terminator
;
; other registers preserved
;
; Like the C library 'atoi', this routine gives no
; warning in the event of overflow, and terminates
; on the first invalid character.
;
blank equ 20h ; ASCII blank character
tab equ 09h ; ASCII tab character
_TEXT segment word public 'CODE'
assume cs:_TEXT
public atoi
atoi proc near ; ASCII to 16-bit integer
push bx ; save registers
push cx
push dx
xor bx,bx ; initialize forming answer
xor cx,cx ; initialize sign flag
atoi1: lodsb ; scan off whitespace
cmp al,blank ; ignore leading blanks
je atoi1
cmp al,tab ; ignore leading tabs
je atoi1
cmp al,'+' ; if + sign proceed
je atoi2
cmp al,'-' ; is it - sign?
jne atoi3 ; no, test if numeric
dec cx ; was - sign, set flag
; for negative result
atoi2: lodsb ; get next character
atoi3: cmp al,'0' ; is character valid?
jb atoi4 ; jump if not '0' to '9'
cmp al,'9'
ja atoi4 ; jump if not '0' to '9'
and ax,0fh ; isolate lower four bits
xchg bx,ax ; multiply answer x 10
mov dx,10
mul dx
add bx,ax ; add this digit
jmp atoi2 ; convert next digit
atoi4: mov ax,bx ; result into AX
jcxz atoi5 ; jump if sign flag clear
neg ax ; make result negative
atoi5: pop dx ; restore registers
pop cx
pop bx
ret ; back to caller
atoi endp
_TEXT ends
end