home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / DOS_HELP / ADVMSDOS.ZIP / ASCBIN.ASM next >
Encoding:
Assembly Source File  |  1986-06-19  |  6.6 KB  |  177 lines

  1.          name      ascbin 
  2.          page      55,132
  3.          title     'ASCBIN: ASCII to Binary Conversion'
  4.  
  5. ;
  6. ; This file contains a pair of routines to
  7. ; convert decimal ASCII or hexadecimal ASCII
  8. ; strings into 32-bit integers.
  9. ;
  10. ; Copyright 1984 Ray Duncan
  11. ;
  12. cseg     segment para public 'CODE'
  13.  
  14.     assume    cs:cseg,ds:cseg,ss:cseg
  15.  
  16. dec_bin proc    near            ;Convert decimal ASCII string
  17.                 ;terminated by zero (null) byte into
  18.                                 ;a 32-bit signed binary integer.
  19.                                 ;Conversion ends on zero byte or the
  20.                                 ;first unconvertable digit.
  21.                                 ;
  22.                                 ;Call with
  23.                 ;DS:SI = addr of ASCII string
  24.  
  25.                                 ;Returns
  26.                 ;CY flag = 1 if illegal input string,
  27.                 ;contents of other registers undefined.
  28.                 ;  or
  29.                 ;CY flag = 0 if legal input string, and
  30.                 ;DX:CX   = signed 32-bit binary integer
  31.                                 ;BL      = number of digits after decimal
  32.                                 ;          place, or -1 if no dec. point
  33.  
  34.                                 ;initialization...
  35.         xor     cx,cx           ;set forming answer to zero
  36.         xor     dx,dx
  37.         mov     bx,-1           ;clear decimal place counter and
  38.                                 ;sign flag.
  39.         mov     al,[si]         ;
  40.         cmp     al,'+'          ;is leading + sign present?
  41.         jne     dec_bin1        ;no, jump.
  42.         inc     si              ;yes, just skip over it.
  43.         jmp     dec_bin2
  44.  
  45. dec_bin1:
  46.         cmp     al,'-'          ;is leading - sign present?
  47.         jne     dec_bin2
  48.         xor     bh,bh           ;yes, set sign flag and skip
  49.         inc     si              ;over the character.
  50.  
  51. dec_bin2:
  52.         lodsb                   ;get next character from input string.
  53.         or      al,al           ;is it null byte?
  54.         jz      dec_bin8        ;yes,finished with string.
  55.         cmp     al,'.'          ;is it decimal point?
  56.         jz      dec_bin4        ;yes,go process it.
  57.         cmp     al,'9'          ;make sure it is legal character 0-9.
  58.         ja      dec_bin7        ;char > '9', exit with error flag.
  59.         cmp     al,'0'          
  60.         jb      dec_bin7        ;char < '0', exit with error flag.
  61.         
  62.         or      bl,bl           ;are we past a decimal point?
  63.         js      dec_bin3        ;no,jump
  64.         inc     bl              ;yes, count digits
  65.  
  66. dec_bin3:            ;add another digit to forming answer.    
  67.     push    ax        ;first save this character
  68.         mov     di,cx           ;make copy of the current answer
  69.         mov     ax,dx
  70.                                 ;multiply current answer by 10
  71.         shl     cx,1
  72.         rcl     dx,1            ;* 2
  73.         shl     cx,1
  74.         rcl     dx,1            ;* 4
  75.         add     cx,di           
  76.         adc     dx,ax           ;* 5
  77.         shl     cx,1
  78.         rcl     dx,1            ;* 10
  79.  
  80.     pop    ax        ;restore new character
  81.         and     ax,0fh          ;isolate binary value 0-9
  82.                                 ;from the ASCII character code
  83.         add     cx,ax           ;and add it to the forming answer.
  84.         adc     dx,0
  85.         jmp     dec_bin2        ;get next character
  86.  
  87. dec_bin4:                       ;decimal point detected.
  88.         or      bl,bl           ;did we find one before?
  89.         jns     dec_bin7        ;yes, exit with error flag.
  90.         xor     bl,bl           ;no, clear decimal point counter
  91.         jmp     dec_bin2        ;and go to next input character.
  92.  
  93. dec_bin7:                       ;illegal input string, 
  94.         stc                     ;return CY flag = 1
  95.         ret
  96.  
  97. dec_bin8:                       ;legal input string, return CY flag = 0
  98.                                 ;DX:CX = signed binary integer,
  99.                                 ;BL = digits after decimal pt.
  100.         or      bh,bh           ;did string start with - sign?
  101.         jnz     dec_bin9        ;no,jump
  102.         not     cx              ;yes, take 2's complement of answer.
  103.         not     dx              ;by inverting it and adding 1.
  104.         add     cx,1
  105.         adc     dx,0    
  106.  
  107. dec_bin9:            ;string successfully converted,
  108.         clc                     ;signal with CY flag = 0
  109.         ret
  110.  
  111. dec_bin endp
  112.  
  113.  
  114. hex_bin proc    near            ;Convert hexadecimal ASCII string
  115.                 ;terminated by zero (null) byte into
  116.                                 ;a 32-bit binary integer.
  117.                                 ;Conversion ends on zero byte or the
  118.                                 ;first unconvertable digit.
  119.                                 ;
  120.                                 ;Call with 
  121.                 ;DS:SI = addr of hex ASCII string
  122.                                 ;Returns
  123.                 ;CY flag = 1 input string illegal
  124.                 ;  or
  125.                 ;CY flag = 0 input string legal, and
  126.                 ;DX:CX = signed 32-bit integer.
  127.  
  128.                                 ;initialization...
  129.         xor     cx,cx           ;set forming answer to zero
  130.         xor     dx,dx
  131.  
  132. hex_bin1:
  133.         lodsb                   ;get next character from input string.
  134.         or      al,al           ;is it null byte?
  135.         jz      hex_bin8        ;yes,finished with string.
  136.         cmp     al,'0'          ;no, make sure char. is legal.
  137.         jb      hex_bin7        ;char < '0', exit with error flag.
  138.     cmp    al,'9'
  139.     jbe    hex_bin3    ;char < '9', proceed
  140.                 ;if char > '9',
  141. hex_bin2:            ;check for char. 'A'-'F' or 'a' - 'f'.
  142.     or    al,20h        ;first fold character to lower case.
  143.     cmp    al,'f'
  144.     ja    hex_bin7    ;char > 'F' or 'f', exit with error flag.    
  145.     cmp    al,'a'
  146.     jb    hex_bin7    ;char < 'A' or 'a', exit with error flag.
  147.     add    al,9        ;add fudge factor so we'll get
  148.                 ;correct binary value later.
  149.  
  150. hex_bin3:            ;add another digit to forming answer.
  151.         shl     cx,1        ;first shift current answer left 4 bits.
  152.         rcl     dx,1            
  153.         shl     cx,1
  154.         rcl     dx,1            
  155.         shl     cx,1
  156.         rcl     dx,1            
  157.         shl     cx,1
  158.         rcl     dx,1            
  159.         and     ax,0fh          ;isolate binary value 0-A
  160.                                 ;from the ASCII character code
  161.         or     cx,ax           ;and add it to the forming answer.
  162.         jmp     hex_bin1        ;get next character
  163.  
  164. hex_bin7:                       ;illegal input string, 
  165.         stc                     ;return CY flag = 1
  166.         ret
  167.  
  168. hex_bin8:                       ;legal input string, return 
  169.         clc                     ;DX:CX = 32-bit unsigned integer,
  170.         ret                     ;signal success by CY flag = 0
  171.  
  172. hex_bin endp
  173.  
  174. cseg     ends
  175.  
  176.     end
  177.