home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CLIBSRC3.ZIP / ATOL.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  4.6 KB  |  169 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - atol.cas
  3.  *
  4.  * function(s)
  5.  *        atol  - converts a string to a long
  6.  *        atoi  - converts a string to an int
  7.  *--------------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #pragma  inline
  19. #include <asmrules.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22.  
  23. #undef   atoi           /* macro in stdlib */
  24.  
  25.  
  26.  
  27. /*--------------------------------------------------------------------------*
  28.  
  29. Name            atol - converts a string to an integer
  30.  
  31. Usage           long atol(const char *nptr);
  32.  
  33. Prototype in    stdlib.h
  34.  
  35. Description     Convert a string to a long integer.  The syntax of
  36.                 the string must be:
  37.  
  38.                         long     ::= [isspace]* [sign] digit [digit]*
  39.  
  40.                 Only decimal integers are acceptable.
  41.  
  42.                 Error handling is poor.  The function will protect
  43.                 itself (crash-proof) but there is no defined method
  44.                 to return an error indication to the caller.  The
  45.                 result is undefined if the input string is invalid.
  46.  
  47. Return value    converted long value of the input string.  If the string
  48.                 cannot be converted to a long, the return value is 0.
  49.  
  50. *---------------------------------------------------------------------------*/
  51.  
  52. long _CType _FARFUNC atol(const char *strP)
  53. {
  54. #if LDATA
  55. asm     push    ES
  56. #endif
  57. asm     push    bp
  58.  
  59. asm     LES_    si, strP
  60.  
  61. #ifdef __HUGE__
  62. asm     mov     ax, seg _ctype
  63. asm     mov     DS, ax
  64. #endif
  65.  
  66. asm     cld
  67. asm     sub     ax, ax
  68. asm     cwd                     /* default result in DX:AX = 0:0.       */
  69.  
  70. asm     mov     cx, 10          /* decimal radix        */
  71.  
  72. /*      Skip any isspace (ctype) characters at the start. */
  73. asm     mov     bh, 0
  74. asm     mov     di, 1 + offset (ES_ _ctype)     /* avoid assuming DS    */
  75. atl_skipSpace:
  76. asm     mov     bl, ES_ [si]
  77. asm     inc     si
  78. asm     test    BY0 ([bx+di]), _IS_SP           /* (1 + _ctype) [bx]    */
  79. asm     jnz     atl_skipSpace
  80.  
  81. /*      Remember if a negative sign is encountered */
  82. asm     mov     bp, 0           /* BP holds sign, 0 = positive  */
  83. asm     cmp     bl, '+'
  84. asm     je      atl_signSeen
  85. asm     cmp     bl, '-'
  86. asm     jne     atl_inspectDigit
  87. asm     inc     bp              /*               1 = negative   */
  88. atl_signSeen:
  89.  
  90. /*      accumulate digits in AX until more than 16 bits. */
  91. atl_digitOnWord:
  92. asm     mov     bl, ES_ [si]
  93. asm     inc     si
  94.  
  95. atl_inspectDigit:
  96. asm     cmp     bl, '9'
  97. asm     ja      atl_end
  98. asm     sub     bl, '0'
  99. asm     jb      atl_end
  100.  
  101. asm     mul     cx
  102. asm     add     ax, bx
  103. asm     adc     dl, dh
  104. asm     jz      atl_digitOnWord
  105. asm     jmp     short   atl_nextDigit
  106.  
  107. /*      Accumulate digits in DX:AX, overflow may occur but is ignored
  108.         (as per SVID norm).
  109. */
  110. atl_digitOnLong:
  111. asm     mov     di, dx          /* copy DX to safety in DI      */
  112. asm     mov     cx, 10
  113. asm     mul     cx
  114. asm     xchg    ax, di
  115. asm     xchg    dx, cx
  116. asm     mul     dx
  117. asm     xchg    dx, ax
  118. asm     xchg    ax, di          /* function result should be in DX:AX   */
  119. asm     add     ax, bx
  120. asm     adc     dx, cx
  121. /*      overflows are discarded: result is undefined */
  122. atl_nextDigit:
  123. asm     mov     bl, ES_ [si]
  124. asm     inc     si
  125. asm     cmp     bl, '9'
  126. asm     ja      atl_end
  127. asm     sub     bl, '0'
  128. asm     jnb     atl_digitOnLong
  129.  
  130. /*      the result may be signed. */
  131. atl_end:
  132. asm     dec     bp              /* was '-' negative seen ?      */
  133. asm     jl      atl_exit
  134. asm     neg     dx
  135. asm     neg     ax
  136. asm     sbb     dx, 0           /* negate (DX:AX)       */
  137.  
  138. atl_exit:
  139. asm     pop     bp
  140. #if LDATA
  141. asm     pop     ES
  142. #endif
  143.     return( MK_LONG );
  144. }
  145.  
  146. /*--------------------------------------------------------------------------*
  147.  
  148. Name            atoi - converts a string to an integer
  149.  
  150. Usage           int atoi(char *nptr);
  151.  
  152. Prototype in    stdlib.h
  153.  
  154. Description     Convert ASCII string to word integer.
  155.  
  156.                 The only difference between this and the atol
  157.                 function is whether the result is truncated.
  158.  
  159. Return value    converted long value of the input string.  If the string
  160.                 cannot be converted to an int, the return value is 0.
  161.  
  162. *---------------------------------------------------------------------------*/
  163.  
  164. int _CType _FARFUNC atoi (const char *strP)
  165. {
  166.         return (int) atol (strP);
  167. }
  168.  
  169.