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

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