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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strcmp.cas
  3.  *
  4.  * function(s)
  5.  *        strcmp - compare one string to another
  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.  
  19. #pragma inline
  20. #include <asmrules.h>
  21. #include <string.h>
  22.  
  23. /*-----------------------------------------------------------------------*
  24.  
  25. Name        strcmp - compare one string to another
  26.  
  27. Usage        int strcmp(const char *str1, const char str2);
  28.  
  29. Prototype in    string.h
  30.  
  31. Description    Compare *str1  with *str2, returning  a negative, zero,  or
  32.         positive integer  according to whether *str1  is less than,
  33.         equal, or greater than *str2, respectively.
  34.  
  35. Return value    strcmp return an integer value such as:
  36.             < 0    if str1 is less than str2
  37.             = 0    if str1 is the same as str2
  38.             > 0    if str2 is greater than str2
  39.  
  40. *------------------------------------------------------------------------*/
  41. #undef strcmp                  /* not an intrinsic */
  42. int _CType strcmp(const char *str1, const char *str2)
  43. {
  44. #if  defined(__LARGE__) || defined(__COMPACT__)
  45. asm    mov    dx, ds
  46. #endif
  47. #if !(LDATA)
  48. asm    mov    ax, ds
  49. asm    mov    es, ax
  50. #endif
  51. asm    cld
  52.  
  53. /*   Its handy to have AH & BH zero later for the final subtraction. */
  54. asm    xor    ax, ax
  55. asm    mov    bx, ax
  56.  
  57. /*   Determine size of 2nd source string. */
  58. asm    LES_    di, str2
  59. asm    mov    si, di
  60. asm    xor    al, al
  61. asm    mov    cx, -1
  62. asm    repne    scasb
  63. asm    not    cx
  64.  
  65. asm    mov    di, si
  66. asm    LDS_    si, str1
  67.  
  68. /*
  69. Scan until either *s2 terminates or a difference is found.  Note that it is
  70. sufficient to check only for right termination, since if the left terminates
  71. before the right then that difference will also terminate the scan.
  72. */
  73. asm    repe    cmpsb
  74. /*
  75. The result is the signed difference of the final character pair, be they
  76. equal or different. A simple byte subtract and CBW doesn't work here because
  77. it does the wrong thing when the characters are 'ff' and '7f'.  In that case
  78. 255 would be reported as less than 127. ie '80' sign extends to 'ff80' which
  79. is a negative number.  Remember AH, BH are zero from above.
  80. */
  81. asm    mov    al, [si-1]
  82. asm    mov    bl, ES_ [di-1]
  83. asm    sub    ax, bx
  84. #if  defined(__LARGE__) || defined(__COMPACT__)
  85. asm    mov    ds, dx
  86. #endif
  87.     return _AX;
  88. }
  89.