home *** CD-ROM | disk | FTP | other *** search
- name decbin32
- page 55,132
- title 'DECBIN32: ASCII Decimal to Binary Conversion'
- subttl Modified from DUNCAN.ARC, Downloaded from VOR BBS
-
- ;
- ; This file contains a routine to convert
- ; decimal ASCII strings into 32-bit integers.
- ;
- ; Copyright (C) 1984 Ray Duncan
- ;
-
- PUBLIC dec_bin_32
-
- cseg segment para public 'CODE'
-
- assume cs:cseg,ds:cseg,ss:cseg
-
- dec_bin_32 proc near ;Convert decimal ASCII string
- ;terminated by zero (null) byte into
- ;a 32-bit signed binary integer.
- ;Conversion ends on zero byte or the
- ;first unconvertable digit.
- ;
- ;Call with
- ;DS:SI = addr of ASCII string
-
- ;Returns
- ;CY flag = 1 if illegal input string,
- ;contents of other registers undefined.
- ; or
- ;CY flag = 0 if legal input string, and
- ;DX:CX = signed 32-bit binary integer
- ;BL = number of digits after decimal
- ; place, or -1 if no dec. point
-
- ;initialization...
- xor cx,cx ;set forming answer to zero
- xor dx,dx
- mov bx,-1 ;clear decimal place counter and
- ;sign flag.
- mov al,[si] ;
- cmp al,'+' ;is leading + sign present?
- jne dec_bin1 ;no, jump.
- inc si ;yes, just skip over it.
- jmp dec_bin2
-
- dec_bin1:
- cmp al,'-' ;is leading - sign present?
- jne dec_bin2
- xor bh,bh ;yes, set sign flag and skip
- inc si ;over the character.
-
- dec_bin2:
- lodsb ;get next character from input string.
- or al,al ;is it null byte?
- jz dec_bin8 ;yes,finished with string.
- cmp al,'.' ;is it decimal point?
- jz dec_bin4 ;yes,go process it.
- cmp al,'9' ;make sure it is legal character 0-9.
- ja dec_bin7 ;char > '9', exit with error flag.
- cmp al,'0'
- jb dec_bin7 ;char < '0', exit with error flag.
-
- or bl,bl ;are we past a decimal point?
- js dec_bin3 ;no,jump
- inc bl ;yes, count digits
-
- dec_bin3: ;add another digit to forming answer.
- push ax ;first save this character
- mov di,cx ;make copy of the current answer
- mov ax,dx
- ;multiply current answer by 10
- shl cx,1
- rcl dx,1 ;* 2
- shl cx,1
- rcl dx,1 ;* 4
- add cx,di
- adc dx,ax ;* 5
- shl cx,1
- rcl dx,1 ;* 10
-
- pop ax ;restore new character
- and ax,0fh ;isolate binary value 0-9
- ;from the ASCII character code
- add cx,ax ;and add it to the forming answer.
- adc dx,0
- jmp dec_bin2 ;get next character
-
- dec_bin4: ;decimal point detected.
- or bl,bl ;did we find one before?
- jns dec_bin7 ;yes, exit with error flag.
- xor bl,bl ;no, clear decimal point counter
- jmp dec_bin2 ;and go to next input character.
-
- dec_bin7: ;illegal input string,
- stc ;return CY flag = 1
- ret
-
- dec_bin8: ;legal input string, return CY flag = 0
- ;DX:CX = signed binary integer,
- ;BL = digits after decimal pt.
- or bh,bh ;did string start with - sign?
- jnz dec_bin9 ;no,jump
- not cx ;yes, take 2's complement of answer.
- not dx ;by inverting it and adding 1.
- add cx,1
- adc dx,0
-
- dec_bin9: ;string successfully converted,
- clc ;signal with CY flag = 0
- ret
-
- dec_bin_32 endp
-
- cseg ends
-
- end
-