home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / asm / MATHASM.ZIP / BIN32.ASM < prev    next >
Encoding:
Assembly Source File  |  1985-12-03  |  4.1 KB  |  86 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
  45.      JNZ      BIN32I          ;is > or = 2^32-overflow 
  46.          OR       CX,CX           ;if CX sign bit set, product
  47.          JS       BIN32I          ;is > or = 2^31-overflow 
  48. ;                            add in this digit 
  49.          XCHG     CX,BP           ;digit & COUNT in CX 
  50.          MOV      AL,CH           ;this digit in DX:AX 
  51.          ADD      AX,BX           ;add NUM*10 from CX:BX 
  52.          ADC      DX,BP 
  53.          JC       BIN32I          ;check for overflow again
  54. ;                            get next ASCII character 
  55. BIN32D:  INC      SI              ;point to next char 
  56.          CMP      SI,DI           ;is there another one? 
  57.          JB       BIN32C          ;if so, go get it 
  58. ;                            set final decimal place count 
  59. BIN32E:  XCHG     AX,CX           ;convert count in CL 
  60.          CBW                      ;to word in AX 
  61.          XCHG     AX,CX           ;put count back in CX 
  62.          OR       CX,CX           ;there are no decimals 
  63.          JS       BIN32F          ;if count in CX is positive,
  64.          SUB      CX,CX           ;so make CX zero 
  65. ;                            restore sign 
  66. BIN32F:  POP      BX 
  67.          OR       BX,BX 
  68.          JNS      BIN32G           ;if sign was negative 
  69.          NOT      AX               ;negate NUM 
  70.          NOT      DX 
  71.          ADD      AX,1 
  72.          ADC      DX,0 
  73. ;                            wrap up 
  74. BIN32G:  SUB      BX,BX            ;BX=0  status ok 
  75.          POP      BP 
  76.          RET 
  77. ;                            found non-numeric character 
  78. BIN32H:  CMP      CH,-2            ;is it decimal point (46-48)?
  79.          JNE      BIN32E           ;if not, finish 
  80.          SUB      CL,CL            ;decimal so,set COUNT to
  81. zero 
  82.          JMP      BIN32D           ;and get next char 
  83. ;                            overflow 
  84. BIN32I:  POP      BX               ;remove sign from stack 
  85.          POP      BP               ;restore BP 
  86.          MOV      BX,1             ;BX=1 overflow flag 
  87.          RET 
  88. BIN32    ENDP 
  89.