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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strncmp.cas
  3.  *
  4.  * function(s)
  5.  *        strncmp - 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            strncmp - compare one string to another
  24.  
  25. Usage           int strncmp(const char *str1, const char *str2, size_t maxlen);
  26.  
  27. Prototype in    string.h
  28.  
  29.  
  30. Description     Compare *str1  with *str2, returning  a negative, zero,  or
  31.                 positive integer  according to whether *str1  is less than,
  32.                 equal, or greater than *str2, respectively.
  33.  
  34.                 At most "maxlen" bytes will be compared. A "maxlen" of zero
  35.                 results in an equal compare, i.e. returns a zero.
  36.  
  37. Return value    strncmp return an integer value such as:
  38.                     < 0 if str1 is less than str2
  39.                     = 0 if str1 is the same as str2
  40.                     > 0 if str2 is greater than str2
  41.  
  42. *------------------------------------------------------------------------*/
  43. #undef strncmp                  /* not an intrinsic */
  44.  
  45. #if defined(__FARFUNCS__)
  46. #include <_farfunc.h>
  47. #endif
  48.  
  49. int _CType _FARFUNC strncmp(const char _FAR *str1, const char _FAR *str2, size_t maxlen)
  50. {
  51. #if defined(__LARGE__) || defined(__COMPACT__)
  52. asm     mov     dx, ds
  53. #endif
  54. #if !(LDATA)
  55. asm     mov     ax, ds      /* ES = DS */
  56. asm     mov     es, ax
  57. #endif
  58. asm     cld
  59.  
  60. /*   Determine size of 2nd source string. */
  61. asm     LES_    di, str2
  62. asm     mov     si, di
  63. asm     mov     ax, maxlen
  64. asm     mov     cx, ax
  65. asm     jcxz    ncm_end
  66. asm     mov     bx, ax
  67. asm     xor     al, al
  68. asm     repne   scasb
  69. asm     sub     bx, cx
  70. asm     mov     cx, bx
  71.  
  72. asm     mov     di, si
  73. asm     LDS_    si, str1
  74.  
  75. /*
  76. Scan until either *s2 terminates, a difference is found, or "limit" is
  77. reached.  Note that it is sufficient to check only for right termination,
  78. since if the left terminates before the right then that difference will
  79. also terminate the scan.
  80. */
  81. asm     repe    cmpsb
  82.  
  83. /*
  84.   The result is the signed difference of the final character pair, be they
  85.   equal or different.
  86.   We need to do the full word subtract here because ANSI requires unsigned
  87.   comparisons. A simple byte subtract with a CBW would produce the wrong
  88.   result in some cases.
  89. */
  90. asm     mov     al, [si-1]
  91. asm     mov     bl, ES_ [di-1]
  92. asm     xor     ah, ah  /* zero out high order bytes so the subtraction */
  93. asm     mov     bh, ah  /* will work.                                   */
  94. asm     sub     ax, bx
  95.  
  96. ncm_end:
  97. #if defined(__LARGE__) || defined(__COMPACT__)
  98. asm     mov     ds, dx
  99. #endif
  100.         return _AX;
  101. }
  102.