home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / STRCMP.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  2.7 KB  |  92 lines

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