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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strrev.c
  3.  *
  4.  * function(s)
  5.  *        strrev - reverses a string
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #pragma  inline
  19. #include <asmrules.h>
  20. #include <string.h>
  21.  
  22. /*---------------------------------------------------------------------*
  23.  
  24. Name            strrev - reverses a string
  25.  
  26. Usage           char *strrev(char * str);
  27.  
  28. Prototype in    string.h
  29.  
  30. Description     strrev reverses all characters in a string (except the
  31.                 terminating null character).
  32.  
  33. Return value    strrev returns a pointer to the reversed string. There is no
  34.         error return.
  35.  
  36. *---------------------------------------------------------------------*/
  37. char *strrev(char *s)
  38. {
  39. asm    cld
  40. #if  defined(__LARGE__) || defined(__COMPACT__)
  41. asm    push    ds
  42. #endif
  43. asm    LDS_    si, s            /* point to string */
  44. asm    mov    dx, si            /* save addr for return */
  45. asm    mov    di, si
  46. asm    push    ds
  47. asm    pop    es            /* DS:SI = ES:DI = string */
  48. asm    xor    al, al
  49. asm    mov    cx,-1
  50. asm    repne scasb            /* find null */
  51. asm    cmp    cx,-2
  52. asm    je    reversed        /* abort if string empty */
  53. asm    dec    di
  54. asm    dec    di            /* ES:DI = last non-null char */
  55. asm    xchg    si, di            /* DS:SI = last, ES:DI = first */
  56.     goto    reverse_entry;
  57. reverse_loop:
  58. asm    mov    al, [di]
  59. asm    xchg    al, [si]
  60. asm    stosb                /* swap bytes, bump di */
  61. asm    dec    si
  62. reverse_entry:
  63. asm    cmp    di, si            /* pointers crossed? */
  64. asm    jb    reverse_loop
  65. reversed:
  66. asm     xchg    ax, dx                  /* return addr of string */
  67. #if LDATA
  68. asm    mov    dx, ds
  69. #endif
  70. #if  defined(__LARGE__) || defined(__COMPACT__)
  71. asm    pop    ds
  72. #endif
  73.  
  74. #if LDATA
  75.         return( (char *)(MK_LONG) );
  76. #else
  77.         return( (char *)_AX );
  78. #endif
  79. }
  80.  
  81.