home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / LTOA.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  7.5 KB  |  235 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - ltoa.cas
  3.  *
  4.  * function(s)
  5.  *        itoa      - converts an integer to a string
  6.  *        ltoa      - converts a long to a string
  7.  *        ultoa     - converts an unsigned long to a string
  8.  *        __longtoa - converts a long to a character string
  9.  *-----------------------------------------------------------------------*/
  10.  
  11. /*[]------------------------------------------------------------[]*/
  12. /*|                                                              |*/
  13. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  14. /*|                                                              |*/
  15. /*|                                                              |*/
  16. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  17. /*|     All Rights Reserved.                                     |*/
  18. /*|                                                              |*/
  19. /*[]------------------------------------------------------------[]*/
  20.  
  21. #pragma  inline
  22. #include <asmrules.h>
  23. #include <stdlib.h>
  24. #include <_printf.h>
  25.  
  26. /*-----------------------------------------------------------------------*
  27.  
  28. Name            __longtoa - converts a long to a character string
  29.  
  30. Usage           char *pascal __longtoa (long value, char *strP, int radix,
  31.                                         char maybeSigned, char hexStyle);
  32.  
  33. Prototype in    _printf.h
  34.  
  35. Description     converts a long to a character string. see itoa below.
  36.  
  37. Return value    pointer to the string
  38.  
  39. *------------------------------------------------------------------------*/
  40. char *pascal near __longtoa (long value, char *strP, int radix,
  41.                         char maybeSigned, char hexStyle)
  42. {
  43.         char    buf [34];
  44.  
  45.         SaveSI
  46.         SaveDI
  47.  
  48. asm     push    ES
  49.  
  50. asm     LES_    di, strP
  51. #if (! LDATA)
  52. asm     push    DS
  53. asm     pop     ES
  54. #endif
  55.  
  56. /* If the request is invalid, generate an empty result */
  57.  
  58. asm     mov     bx, radix
  59. asm     cmp     bx, 36
  60. asm     ja      lta_end
  61. asm     cmp     bl, 2
  62. asm     jb      lta_end
  63.  
  64. asm     mov     ax, W0 (value)
  65. asm     mov     cx, W1 (value)
  66.  
  67. asm     or      cx, cx
  68. asm     jnl     lta_notSigned
  69.  
  70. asm     cmp     BY0 (maybeSigned), 0
  71. asm     jz      lta_notSigned           /* Is the value signed or unsigned ? */
  72.  
  73. asm     mov     BY0 (ES_ [di]), '-'
  74. asm     inc     di
  75. asm     neg     cx
  76. asm     neg     ax
  77. asm     sbb     cx, 0           /* negate CX:AX */
  78.  
  79.  
  80. /* Now loop, taking each digit as modulo radix, and reducing the value
  81.         by dividing by radix, until the value is zeroed.  Note that
  82.         at least one loop occurs even if the value begins as 0,
  83.         since we want "0" to be generated rather than "".
  84. */
  85.  
  86. lta_notSigned:                          /*  BX = radix,  CX:AX = value */
  87. asm     lea     si, buf
  88. asm     jcxz    lta_shortLoop
  89.  
  90. lta_longLoop:
  91. asm     xchg    cx, ax
  92. asm     sub     dx, dx
  93. asm     div     bx
  94. asm     xchg    ax, cx
  95. asm     div     bx
  96. asm     mov     SS_ [si], dl
  97. asm     inc     si
  98. asm     jcxz    lta_shortTest           /* does the value fit in 16 bits ? */
  99. asm     jmp     short   lta_longLoop
  100.  
  101. lta_shortLoop:
  102. asm     sub     dx, dx
  103. asm     div     bx
  104. asm     mov     SS_ [si], dl
  105. asm     inc     si
  106. lta_shortTest:
  107. asm     or      ax, ax
  108. asm     jnz     lta_shortLoop
  109.  
  110. /* The value has now been reduced to zero and the digits are in the buffer. */
  111. asm     lea     cx, buf
  112. asm     neg     cx
  113. asm     add     cx, si          /* CX = length of numeral */
  114.  
  115. /* The digits in the buffer must now be copied in reverse order into
  116.    the target string, translating to ASCII as they are moved.
  117. */
  118. lta_copy:
  119. asm     cld
  120.  
  121. lta_copyLoop:
  122. asm     dec     si
  123. asm     mov     al, SS_ [si]
  124. asm     sub     al, 10
  125. asm     jae     lta_alphaDigit
  126. asm     add     al, 10 + '0'
  127. asm     jmp     short   lta_storeDigit
  128.  
  129. lta_alphaDigit:
  130. asm     add     al, hexStyle
  131.  
  132. lta_storeDigit:
  133. asm     stosb
  134. asm     loop    lta_copyLoop
  135.  
  136. /* terminate the output string with a zero. */
  137.  
  138. lta_end:
  139. asm     mov     al, 0
  140. asm     stosb
  141.  
  142. asm     pop     ES
  143.         return  strP;           /* return a pointer to the string */
  144. }
  145.  
  146.  
  147. /*-----------------------------------------------------------------------*
  148.  
  149. Name            itoa  - converts an integer to a string
  150.                 ltoa  - converts a long to a string
  151.                 ultoa - converts an unsigned long to a string
  152.  
  153. Usage           char *itoa(int value, char *strP, int radix);
  154.                 char *ltoa(long value, char *strP, int radix);
  155.                 char *ultoa(unsigned long value, char *strP, int radix);
  156.                 char *pascal __longtoa (long value, char *strP, int radix,
  157.                                         char maybeSigned, char hexStyle)
  158.  
  159. Prototype in    stdlib.h
  160.                 _printf.h for __longtoa
  161.  
  162. Description     These functions  convert value to a  null-terminated string
  163.                 and  store the  result in  string. With  itoa, value  is an
  164.                 integer;  with ltoa  it is  a  long;  with ultoa  it is  an
  165.                 unsigned long.  __longtoa is the  internal routine used for
  166.                 all these conversions to ASCII.
  167.  
  168.                 radix specifies the base to be used in converting value. it
  169.                 must be between  2 and 36 (inclusive). With  itoa and ltoa,
  170.                 if value is negative, and  radix is 10, the first character
  171.                 of string is  the minus sign (-). This does  not occur with
  172.                 ultoa. Also, ultoa performs no overflow checking.
  173.  
  174.  
  175.                 maybeSigned is treated as a boolean. If false then value is
  176.                 treated  as unsigned  long and  no sign  will be  placed in
  177.                 *strP.
  178.  
  179.                 hexStyle  may take  the values  'a' or  'A' and  determines
  180.                 whether lower or  upper case alphabetics are used  when the
  181.                 radix is 11 or greater.
  182.  
  183.                 Note: The space  allocated for string must be  large enough
  184.                 to hold the returned  string including the terminating null
  185.                 character (\0).  itoa can return  up to 17  bytes; ltoa and
  186.                 ultoa, up to 33 bytes.
  187.  
  188. Return value    All these functions return a pointer to string. There is no
  189.                 error return.
  190.  
  191. *------------------------------------------------------------------------*/
  192. char *_CType itoa( int value, char *strP, int radix )
  193. {
  194. #define dword   unsigned long
  195.  
  196.         return  __longtoa ((radix == 10) ? (long) value :
  197.                            (dword)((unsigned)value), strP, radix, 1, 'a');
  198. }
  199.  
  200.  
  201.  
  202. /*-----------------------------------------------------------------------*
  203.  
  204. Name            ultoa - converts an unsigned long to a string
  205.  
  206. Usage           char *ultoa(unsigned long value, char *string, int radix);
  207.  
  208. Prototype in    stdlib.h
  209.  
  210. Description     see itoa
  211.  
  212. *------------------------------------------------------------------------*/
  213. char  *ultoa  (unsigned long value, char *strP, int radix)
  214. {
  215.         return  __longtoa (value, strP, radix, 0, 'a');
  216. }
  217.  
  218.  
  219.  
  220. /*-----------------------------------------------------------------------*
  221.  
  222. Name            ltoa - converts a long to a string
  223.  
  224. Usage           char *ltoa(long value, char *string, int radix);
  225.  
  226. Prototype in    stdlib.h
  227.  
  228. Description     see itoa
  229.  
  230. *------------------------------------------------------------------------*/
  231. char  *ltoa  (long value, char *strP, int radix)
  232. {
  233.         return  __longtoa (value, strP, radix, (radix == 10), 'a');
  234. }
  235.