home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / APOG / ASM2.ZIP / DECBIN32.ASM < prev    next >
Encoding:
Assembly Source File  |  1987-02-13  |  4.4 KB  |  120 lines

  1.          name      decbin32
  2.          page      55,132
  3.          title     'DECBIN32: ASCII Decimal to Binary Conversion'
  4.          subttl  Modified from DUNCAN.ARC, Downloaded from VOR BBS
  5.  
  6. ;
  7. ; This file contains a routine to convert 
  8. ; decimal ASCII strings into 32-bit integers.
  9. ;
  10. ; Copyright (C) 1984 Ray Duncan
  11. ;
  12.  
  13.     PUBLIC    dec_bin_32
  14.  
  15. cseg     segment para public 'CODE'
  16.  
  17.     assume    cs:cseg,ds:cseg,ss:cseg
  18.  
  19. dec_bin_32 proc near            ;Convert decimal ASCII string
  20.                 ;terminated by zero (null) byte into
  21.                                 ;a 32-bit signed binary integer.
  22.                                 ;Conversion ends on zero byte or the
  23.                                 ;first unconvertable digit.
  24.                                 ;
  25.                                 ;Call with
  26.                 ;DS:SI = addr of ASCII string
  27.  
  28.                                 ;Returns
  29.                 ;CY flag = 1 if illegal input string,
  30.                 ;contents of other registers undefined.
  31.                 ;  or
  32.                 ;CY flag = 0 if legal input string, and
  33.                 ;DX:CX   = signed 32-bit binary integer
  34.                                 ;BL      = number of digits after decimal
  35.                                 ;          place, or -1 if no dec. point
  36.  
  37.                                 ;initialization...
  38.         xor     cx,cx           ;set forming answer to zero
  39.         xor     dx,dx
  40.         mov     bx,-1           ;clear decimal place counter and
  41.                                 ;sign flag.
  42.         mov     al,[si]         ;
  43.         cmp     al,'+'          ;is leading + sign present?
  44.         jne     dec_bin1        ;no, jump.
  45.         inc     si              ;yes, just skip over it.
  46.         jmp     dec_bin2
  47.  
  48. dec_bin1:
  49.         cmp     al,'-'          ;is leading - sign present?
  50.         jne     dec_bin2
  51.         xor     bh,bh           ;yes, set sign flag and skip
  52.         inc     si              ;over the character.
  53.  
  54. dec_bin2:
  55.         lodsb                   ;get next character from input string.
  56.         or      al,al           ;is it null byte?
  57.         jz      dec_bin8        ;yes,finished with string.
  58.         cmp     al,'.'          ;is it decimal point?
  59.         jz      dec_bin4        ;yes,go process it.
  60.         cmp     al,'9'          ;make sure it is legal character 0-9.
  61.         ja      dec_bin7        ;char > '9', exit with error flag.
  62.         cmp     al,'0'          
  63.         jb      dec_bin7        ;char < '0', exit with error flag.
  64.         
  65.         or      bl,bl           ;are we past a decimal point?
  66.         js      dec_bin3        ;no,jump
  67.         inc     bl              ;yes, count digits
  68.  
  69. dec_bin3:            ;add another digit to forming answer.    
  70.     push    ax        ;first save this character
  71.         mov     di,cx           ;make copy of the current answer
  72.         mov     ax,dx
  73.                                 ;multiply current answer by 10
  74.         shl     cx,1
  75.         rcl     dx,1            ;* 2
  76.         shl     cx,1
  77.         rcl     dx,1            ;* 4
  78.         add     cx,di           
  79.         adc     dx,ax           ;* 5
  80.         shl     cx,1
  81.         rcl     dx,1            ;* 10
  82.  
  83.     pop    ax        ;restore new character
  84.         and     ax,0fh          ;isolate binary value 0-9
  85.                                 ;from the ASCII character code
  86.         add     cx,ax           ;and add it to the forming answer.
  87.         adc     dx,0
  88.         jmp     dec_bin2        ;get next character
  89.  
  90. dec_bin4:                       ;decimal point detected.
  91.         or      bl,bl           ;did we find one before?
  92.         jns     dec_bin7        ;yes, exit with error flag.
  93.         xor     bl,bl           ;no, clear decimal point counter
  94.         jmp     dec_bin2        ;and go to next input character.
  95.  
  96. dec_bin7:                       ;illegal input string, 
  97.         stc                     ;return CY flag = 1
  98.         ret
  99.  
  100. dec_bin8:                       ;legal input string, return CY flag = 0
  101.                                 ;DX:CX = signed binary integer,
  102.                                 ;BL = digits after decimal pt.
  103.         or      bh,bh           ;did string start with - sign?
  104.         jnz     dec_bin9        ;no,jump
  105.         not     cx              ;yes, take 2's complement of answer.
  106.         not     dx              ;by inverting it and adding 1.
  107.         add     cx,1
  108.         adc     dx,0    
  109.  
  110. dec_bin9:            ;string successfully converted,
  111.         clc                     ;signal with CY flag = 0
  112.         ret
  113.  
  114. dec_bin_32 endp
  115.  
  116. cseg     ends
  117.  
  118.     end
  119.  
  120.