Leaked source code of windows server 2003
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.9 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. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  21. ;
  22. ;*******************************************************************************
  23. .xlist
  24. include cruntime.inc
  25. .list
  26. page
  27. ;***
  28. ;int strncmp(first, last, count) - compare first count chars of strings
  29. ;
  30. ;Purpose:
  31. ; Compares two strings for lexical order. The comparison stops
  32. ; after: (1) a difference between the strings is found, (2) the end
  33. ; of the strings is reached, or (3) count characters have been
  34. ; compared.
  35. ;
  36. ; Algorithm:
  37. ; int
  38. ; strncmp (first, last, count)
  39. ; char *first, *last;
  40. ; unsigned count;
  41. ; {
  42. ; if (!count)
  43. ; return(0);
  44. ; while (--count && *first && *first == *last)
  45. ; {
  46. ; first++;
  47. ; last++;
  48. ; }
  49. ; return(*first - *last);
  50. ; }
  51. ;
  52. ;Entry:
  53. ; char *first, *last - strings to compare
  54. ; unsigned count - maximum number of characters to compare
  55. ;
  56. ;Exit:
  57. ; returns <0 if first < last
  58. ; returns 0 if first == last
  59. ; returns >0 if first > last
  60. ;
  61. ;Uses:
  62. ;
  63. ;Exceptions:
  64. ;
  65. ;*******************************************************************************
  66. CODESEG
  67. public strncmp
  68. strncmp proc \
  69. uses edi esi ebx, \
  70. first:ptr byte, \
  71. last:ptr byte, \
  72. count:IWORD
  73. mov ecx,[count] ; cx=max number of bytes to compare
  74. jecxz short toend ; it's as if strings are equal
  75. mov ebx,ecx ; bx saves count
  76. mov edi,[first] ; di=first pointer (es=segment part)
  77. mov esi,edi ; si saves first pointer
  78. xor eax,eax ; ax=0
  79. repne scasb ; count bytes
  80. neg ecx ; cx=count - strlen
  81. add ecx,ebx ; strlen + count - strlen
  82. okay:
  83. mov edi,esi ; restore first pointer
  84. mov esi,[last] ; si = last pointer
  85. repe cmpsb ; compare strings
  86. mov al,[esi-1]
  87. xor ecx,ecx ; set return value = 0
  88. cmp al,[edi-1] ; last-first
  89. ja short lastbig ; <last is bigger>
  90. je short toend ; <equal>
  91. ;jb short firstbig ; <first is bigger>
  92. firstbig: ; first string is bigger
  93. sub ecx,2 ; 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