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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - longtoa.cas
  3.  *
  4.  * function(s)
  5.  *        __longtoa - converts a long to a character string
  6.  *        __utoa            - converts an unsigned int to a decimal string
  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 <_printf.h>
  22.  
  23. /*-----------------------------------------------------------------------*
  24.  
  25. Name            __longtoa - converts a long to a character string
  26.  
  27. Usage           char *pascal __longtoa (long value, char *strP, int radix,
  28.                                         char maybeSigned, char hexStyle);
  29.  
  30. Prototype in    _printf.h
  31.  
  32. Description     This function converts a long value to a  null-terminated string
  33.                 and  stores the result in  string strP.
  34.  
  35.                 radix specifies the base to be used in converting value. it
  36.                 must be between  2 and 36 (inclusive).
  37.  
  38.                 maybeSigned is treated as a boolean. If false then value is
  39.                 treated  as unsigned  long and  no sign  will be  placed in
  40.                 *strP.
  41.  
  42.                 hexStyle  may take  the values  'a' or  'A' and  determines
  43.                 whether lower or  upper case alphabetics are used  when the
  44.                 radix is 11 or greater.
  45.  
  46.                 Note: The space  allocated for string must be  large enough
  47.                 to hold the returned  string including the terminating null
  48.                 character (\0).  itoa can return  up to 17  bytes; ltoa and
  49.                 ultoa, up to 33 bytes.
  50.  
  51. Return value    pointer to the string
  52.  
  53. *------------------------------------------------------------------------*/
  54. char *pascal near __longtoa (long value, char *strP, int radix,
  55.                         char maybeSigned, char hexStyle)
  56. {
  57.         char    buf [34];
  58.  
  59.         SaveSI
  60.         SaveDI
  61.  
  62. asm     push    ES
  63.  
  64. asm     LES_    di, strP
  65. #if (! LDATA)
  66. asm     push    DS
  67. asm     pop     ES
  68. #endif
  69.  
  70. /* If the request is invalid, generate an empty result */
  71.  
  72. asm     mov     bx, radix
  73. asm     cmp     bx, 36
  74. asm     ja      lta_end
  75. asm     cmp     bl, 2
  76. asm     jb      lta_end
  77.  
  78. asm     mov     ax, W0 (value)
  79. asm     mov     cx, W1 (value)
  80.  
  81. asm     or      cx, cx
  82. asm     jnl     lta_notSigned
  83.  
  84. asm     cmp     BY0 (maybeSigned), 0
  85. asm     jz      lta_notSigned           /* Is the value signed or unsigned ? */
  86.  
  87. asm     mov     BY0 (ES_ [di]), '-'
  88. asm     inc     di
  89. asm     neg     cx
  90. asm     neg     ax
  91. asm     sbb     cx, 0           /* negate CX:AX */
  92.  
  93.  
  94. /* Now loop, taking each digit as modulo radix, and reducing the value
  95.         by dividing by radix, until the value is zeroed.  Note that
  96.         at least one loop occurs even if the value begins as 0,
  97.         since we want "0" to be generated rather than "".
  98. */
  99.  
  100. lta_notSigned:                          /*  BX = radix,  CX:AX = value */
  101. asm     lea     si, buf
  102. asm     jcxz    lta_shortLoop
  103.  
  104. lta_longLoop:
  105. asm     xchg    cx, ax
  106. asm     sub     dx, dx
  107. asm     div     bx
  108. asm     xchg    ax, cx
  109. asm     div     bx
  110. asm     mov     SS_ [si], dl
  111. asm     inc     si
  112. asm     jcxz    lta_shortTest           /* does the value fit in 16 bits ? */
  113. asm     jmp     short   lta_longLoop
  114.  
  115. lta_shortLoop:
  116. asm     sub     dx, dx
  117. asm     div     bx
  118. asm     mov     SS_ [si], dl
  119. asm     inc     si
  120. lta_shortTest:
  121. asm     or      ax, ax
  122. asm     jnz     lta_shortLoop
  123.  
  124. /* The value has now been reduced to zero and the digits are in the buffer. */
  125. asm     lea     cx, buf
  126. asm     neg     cx
  127. asm     add     cx, si          /* CX = length of numeral */
  128.  
  129. /* The digits in the buffer must now be copied in reverse order into
  130.    the target string, translating to ASCII as they are moved.
  131. */
  132. lta_copy:
  133. asm     cld
  134.  
  135. lta_copyLoop:
  136. asm     dec     si
  137. asm     mov     al, SS_ [si]
  138. asm     sub     al, 10
  139. asm     jae     lta_alphaDigit
  140. asm     add     al, 10 + '0'
  141. asm     jmp     short   lta_storeDigit
  142.  
  143. lta_alphaDigit:
  144. asm     add     al, hexStyle
  145.  
  146. lta_storeDigit:
  147. asm     stosb
  148. asm     loop    lta_copyLoop
  149.  
  150. /* terminate the output string with a zero. */
  151.  
  152. lta_end:
  153. asm     mov     al, 0
  154. asm     stosb
  155.  
  156. asm     pop     ES
  157.         return  strP;           /* return a pointer to the string */
  158. }
  159.  
  160. /*-----------------------------------------------------------------------*
  161.  
  162. Name            __utoa - converts an unsigned int to a decimal string
  163.  
  164. Usage           char *__utoa(unsigned value, char *buf)
  165.  
  166. Prototype in    _printf.h
  167.  
  168. Description     see __longtoa above.
  169.  
  170. *------------------------------------------------------------------------*/
  171. char * pascal near __utoa(unsigned value, char *buf)
  172. {
  173.     return  __longtoa (((long)value) & 0xffffL, buf, 10, 0, 'a');
  174. }
  175.