home *** CD-ROM | disk | FTP | other *** search
- name ascbin
- page 55,132
- title 'ASCBIN: ASCII to Binary Conversion'
-
- ;
- ; This file contains a pair of routines to
- ; convert decimal ASCII or hexadecimal ASCII
- ; strings into 32-bit integers.
- ;
- ; Copyright 1984 Ray Duncan
- ;
- cseg segment para public 'CODE'
-
- assume cs:cseg,ds:cseg,ss:cseg
-
- dec_bin 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 endp
-
-
- hex_bin proc near ;Convert hexadecimal ASCII string
- ;terminated by zero (null) byte into
- ;a 32-bit binary integer.
- ;Conversion ends on zero byte or the
- ;first unconvertable digit.
- ;
- ;Call with
- ;DS:SI = addr of hex ASCII string
- ;Returns
- ;CY flag = 1 input string illegal
- ; or
- ;CY flag = 0 input string legal, and
- ;DX:CX = signed 32-bit integer.
-
- ;initialization...
- xor cx,cx ;set forming answer to zero
- xor dx,dx
-
- hex_bin1:
- lodsb ;get next character from input string.
- or al,al ;is it null byte?
- jz hex_bin8 ;yes,finished with string.
- cmp al,'0' ;no, make sure char. is legal.
- jb hex_bin7 ;char < '0', exit with error flag.
- cmp al,'9'
- jbe hex_bin3 ;char < '9', proceed
- ;if char > '9',
- hex_bin2: ;check for char. 'A'-'F' or 'a' - 'f'.
- or al,20h ;first fold character to lower case.
- cmp al,'f'
- ja hex_bin7 ;char > 'F' or 'f', exit with error flag.
- cmp al,'a'
- jb hex_bin7 ;char < 'A' or 'a', exit with error flag.
- add al,9 ;add fudge factor so we'll get
- ;correct binary value later.
-
- hex_bin3: ;add another digit to forming answer.
- shl cx,1 ;first shift current answer left 4 bits.
- rcl dx,1
- shl cx,1
- rcl dx,1
- shl cx,1
- rcl dx,1
- shl cx,1
- rcl dx,1
- and ax,0fh ;isolate binary value 0-A
- ;from the ASCII character code
- or cx,ax ;and add it to the forming answer.
- jmp hex_bin1 ;get next character
-
- hex_bin7: ;illegal input string,
- stc ;return CY flag = 1
- ret
-
- hex_bin8: ;legal input string, return
- clc ;DX:CX = 32-bit unsigned integer,
- ret ;signal success by CY flag = 0
-
- hex_bin endp
-
- cseg ends
-
- end