home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / math / bin32.asm < prev    next >
Encoding:
Assembly Source File  |  1986-01-17  |  4.1 KB  |  85 lines

  1. BIN32    PROC     NEAR 
  2. ;*************************************************************
  3. ;                  converts an ASCII decimal string to a 
  4. ;                  signed 32-bit binary integer in DX:AX 
  5. ;                  input--DI points to end of string 
  6. ;                         SI points to start 
  7. ;                  output--CX if negative, number of digits
  8. ;                          right of decimal point 
  9. ;                          SI points to next unconverted char
  10. ;*************************************************************
  11.          PUSH     BP 
  12.          SUB      DX,DX 
  13. ;                            get sign 
  14.          MOV      CX,DX           ;assume positive sign 
  15.          MOV      AL,[SI]         ;put first char in AL 
  16.          CMP      AL," "          ;is it blank? positive sign
  17.          JE       BIN32A 
  18.          CMP      AL,"+"          ;is it plus sign? 
  19.          JE       BIN32A 
  20.          CMP      AL,"-"          ;is it minus sign? 
  21.          JNE      BIN32B 
  22.          DEC      CX              ;sign negative-set flag in
  23. CX 
  24. BIN32A:  INC      SI              ;point to next char 
  25. BIN32B:  PUSH     CX              ;save sign 
  26. ;                            convert string 
  27.          MOV      AX,DX           ;0-initial NUMber in DX:AX
  28.          MOV      CL,127          ;max 127 places left of decimal
  29.                                   ;CL=COUNT places right of
  30. decimal 
  31. ;                            get ASCII digit 
  32. BIN32C:  MOV      CH,[SI]         ;next char in CH 
  33.          CMP      CH,"9"          ;if CH > "9" not a digit 
  34.          JA       BIN32H 
  35.          SUB      CH,48           ;convert to binary 
  36.          JB       BIN32H          ;if CH < "0" not a digit 
  37.          DEC      CL              ;found digit--decrement COUNT
  38. ;                            multiply NUM * 10 
  39.          MOV      BP,CX           ;save CX in BP 
  40.          SUB      CX,CX           ;decimal 10 in CX:BX 
  41.          MOV      BX,10 
  42.          CALL     MUL32           ;MUL32 returns DX:AX:CX:BX
  43. ;                            check for overflow--over 2^31-1
  44.          OR       AX,AX           ;if AL not zero, product ▄j▄î         JNZ      BIN32I          ;is > or = 2^32-overflow 
  45.          OR       CX,CX           ;if CX sign bit set, product
  46.          JS       BIN32I          ;is > or = 2^31-overflow 
  47. ;                            add in this digit 
  48.          XCHG     CX,BP           ;digit & COUNT in CX 
  49.          MOV      AL,CH           ;this digit in DX:AX 
  50.          ADD      AX,BX           ;add NUM*10 from CX:BX 
  51.          ADC      DX,BP 
  52.          JC       BIN32I          ;check for overflow again
  53. ;                            get next ASCII character 
  54. BIN32D:  INC      SI              ;point to next char 
  55.          CMP      SI,DI           ;is there another one? 
  56.          JB       BIN32C          ;if so, go get it 
  57. ;                            set final decimal place count 
  58. BIN32E:  XCHG     AX,CX           ;convert count in CL 
  59.          CBW                      ;to word in AX 
  60.          XCHG     AX,CX           ;put count back in CX 
  61.          OR       CX,CX           ;there are no decimals 
  62.          JS       BIN32F          ;if count in CX is positive,
  63.          SUB      CX,CX           ;so make CX zero 
  64. ;                            restore sign 
  65. BIN32F:  POP      BX 
  66.          OR       BX,BX 
  67.          JNS      BIN32G           ;if sign was negative 
  68.          NOT      AX               ;negate NUM 
  69.          NOT      DX 
  70.          ADD      AX,1 
  71.          ADC      DX,0 
  72. ;                            wrap up 
  73. BIN32G:  SUB      BX,BX            ;BX=0  status ok 
  74.          POP      BP 
  75.          RET 
  76. ;                            found non-numeric character 
  77. BIN32H:  CMP      CH,-2            ;is it decimal point (46-48)?
  78.          JNE      BIN32E           ;if not, finish 
  79.          SUB      CL,CL            ;decimal so,set COUNT to
  80. zero 
  81.          JMP      BIN32D           ;and get next char 
  82. ;                            overflow 
  83. BIN32I:  POP      BX               ;remove sign from stack 
  84.          POP      BP               ;restore BP 
  85.          MOV      BX,1             ;BX=1 overflow flag 
  86.          RET 
  87. BIN32    ENDP 
  88.