Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.7 KiB

  1. page ,132
  2. title strncmp - compare first n chars of two strings
  3. ;***
  4. ;strncmp.asm - compare first n characters of two strings
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines strncmp() - compare first n characters of two strings
  10. ; for lexical order.
  11. ;
  12. ;Revision History:
  13. ; 10-26-83 RN initial version
  14. ; 05-18-88 SJM Add model-independent (large model) ifdef
  15. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  16. ; 08-23-88 JCR 386 cleanup
  17. ; 10-26-88 JCR General cleanup for 386-only code
  18. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  19. ; 05-10-91 GJF Back to _cdecl, sigh...
  20. ;
  21. ;*******************************************************************************
  22. .xlist
  23. include cruntime.inc
  24. .list
  25. page
  26. ;***
  27. ;int strncmp(first, last, count) - compare first count chars of strings
  28. ;
  29. ;Purpose:
  30. ; Compares two strings for lexical order. The comparison stops
  31. ; after: (1) a difference between the strings is found, (2) the end
  32. ; of the strings is reached, or (3) count characters have been
  33. ; compared.
  34. ;
  35. ; Algorithm:
  36. ; int
  37. ; strncmp (first, last, count)
  38. ; char *first, *last;
  39. ; unsigned count;
  40. ; {
  41. ; if (!count)
  42. ; return(0);
  43. ; while (--count && *first && *first == *last)
  44. ; {
  45. ; first++;
  46. ; last++;
  47. ; }
  48. ; return(*first - *last);
  49. ; }
  50. ;
  51. ;Entry:
  52. ; char *first, *last - strings to compare
  53. ; unsigned count - maximum number of characters to compare
  54. ;
  55. ;Exit:
  56. ; returns <0 if first < last
  57. ; returns 0 if first == last
  58. ; returns >0 if first > last
  59. ;
  60. ;Uses:
  61. ;
  62. ;Exceptions:
  63. ;
  64. ;*******************************************************************************
  65. CODESEG
  66. public strncmp
  67. strncmp proc \
  68. uses edi esi ebx, \
  69. first:ptr byte, \
  70. last:ptr byte, \
  71. count:IWORD
  72. mov ecx,[count] ; cx=max number of bytes to compare
  73. jecxz short toend ; it's as if strings are equal
  74. mov ebx,ecx ; bx saves count
  75. mov edi,[first] ; di=first pointer (es=segment part)
  76. mov esi,edi ; si saves first pointer
  77. xor eax,eax ; ax=0
  78. repne scasb ; count bytes
  79. neg ecx ; cx=count - strlen
  80. add ecx,ebx ; strlen + count - strlen
  81. okay:
  82. mov edi,esi ; restore first pointer
  83. mov esi,[last] ; si = last pointer
  84. repe cmpsb ; compare strings
  85. mov al,[esi-1]
  86. xor ecx,ecx ; set return value = 0
  87. cmp al,[edi-1] ; last-first
  88. ja short lastbig ; <last is bigger>
  89. je short toend ; <equal>
  90. ;jb short firstbig ; <first is bigger>
  91. firstbig:
  92. dec ecx ; first string is bigger
  93. dec ecx ; make FFFE so 'not' will give 0001
  94. lastbig: ; last string is bigger
  95. not ecx ; return -1
  96. toend:
  97. mov eax,ecx ; return value
  98. ifdef _STDCALL_
  99. ret 2*DPSIZE + ISIZE ; _stdcall return
  100. else
  101. ret ; _cdecl return
  102. endif
  103. strncmp endp
  104. end