home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / ATOL.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  4.3 KB  |  168 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. /*|                                                              |*/
  11. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  12. /*|                                                              |*/
  13. /*|                                                              |*/
  14. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  15. /*|     All Rights Reserved.                                     |*/
  16. /*|                                                              |*/
  17. /*[]------------------------------------------------------------[]*/
  18.  
  19. #pragma  inline
  20. #include <asmrules.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23.  
  24. #undef     atoi        /* macro in stdlib */
  25.  
  26.  
  27.  
  28. /*--------------------------------------------------------------------------*
  29.  
  30. Name        atol - converts a string to an integer
  31.  
  32. Usage        long atol(const char *nptr);
  33.  
  34. Prototype in    stdlib.h
  35.  
  36. Description    Convert a string to a long integer.  The syntax of
  37.         the string must be:
  38.  
  39.             long     ::= [isspace]* [sign] digit [digit]*
  40.  
  41.         Only decimal integers are acceptable.
  42.  
  43.         Error handling is poor.  The function will protect
  44.         itself (crash-proof) but there is no defined method
  45.                 to return an error indication to the caller.  The
  46.                 result is undefined if the input string is invalid.
  47.  
  48. Return value    converted long value of the input string.  If the string
  49.         cannot be converted to a long, the return value is 0.
  50.  
  51. *---------------------------------------------------------------------------*/
  52. long  _CType 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. int  atoi (const char *strP)
  164. {
  165.         return (int) atol (strP);
  166. }
  167.  
  168.