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.

162 lines
3.0 KiB

  1. page ,132
  2. title strcmp.asm - compare two strings
  3. ;***
  4. ;strcmp.asm - routine to compare two strings (for equal, less, or greater)
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; STRCMP compares two strings and returns an integer
  10. ; to indicate whether the first is less than the second, the two are
  11. ; equal, or whether the first is greater than the second, respectively.
  12. ; Comparison is done byte by byte on an UNSIGNED basis, which is to
  13. ; say that Null (0) is less than any other character (1-255).
  14. ;
  15. ;Revision History:
  16. ; 04-21-87 SKS Module rewritten to be fast and small
  17. ; 05-17-88 SJM Add model-independent (large model) ifdef
  18. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  19. ; 08-23-88 JCR Minor 386 cleanup
  20. ; 10-25-88 JCR General cleanup for 386-only code
  21. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  22. ; 05-10-91 GJF Back to _cdecl, sigh...
  23. ; 05-26-93 GJF Tuned for the 486.
  24. ; 06-14-93 GJF Testing wrong byte of ax against 0 in one case.
  25. ; 06-16-93 GJF Added .FPO directive.
  26. ;
  27. ;*******************************************************************************
  28. .xlist
  29. include cruntime.inc
  30. .list
  31. page
  32. ;***
  33. ;strcmp - compare two strings, returning less than, equal to, or greater than
  34. ;
  35. ;Purpose:
  36. ; Compares two string, determining their lexical order. Unsigned
  37. ; comparison is used.
  38. ;
  39. ; Algorithm:
  40. ; int strcmp ( src , dst )
  41. ; unsigned char *src;
  42. ; unsigned char *dst;
  43. ; {
  44. ; int ret = 0 ;
  45. ;
  46. ; while( ! (ret = *src - *dst) && *dst)
  47. ; ++src, ++dst;
  48. ;
  49. ; if ( ret < 0 )
  50. ; ret = -1 ;
  51. ; else if ( ret > 0 )
  52. ; ret = 1 ;
  53. ;
  54. ; return( ret );
  55. ; }
  56. ;
  57. ;Entry:
  58. ; const char * src - string for left-hand side of comparison
  59. ; const char * dst - string for right-hand side of comparison
  60. ;
  61. ;Exit:
  62. ; AX < 0, 0, or >0, indicating whether the first string is
  63. ; Less than, Equal to, or Greater than the second string.
  64. ;
  65. ;Uses:
  66. ; CX, DX
  67. ;
  68. ;Exceptions:
  69. ;
  70. ;*******************************************************************************
  71. CODESEG
  72. public strcmp
  73. strcmp proc
  74. .FPO ( 0, 2, 0, 0, 0, 0 )
  75. mov edx,[esp + 4] ; edx = src
  76. mov ecx,[esp + 8] ; ecx = dst
  77. test edx,3
  78. jnz short dopartial
  79. align 4
  80. dodwords:
  81. mov eax,[edx]
  82. cmp al,[ecx]
  83. jne short donene
  84. or al,al
  85. jz short doneeq
  86. cmp ah,[ecx + 1]
  87. jne short donene
  88. or ah,ah
  89. jz short doneeq
  90. shr eax,16
  91. cmp al,[ecx + 2]
  92. jne short donene
  93. or al,al
  94. jz short doneeq
  95. cmp ah,[ecx + 3]
  96. jne short donene
  97. add ecx,4
  98. add edx,4
  99. or ah,ah
  100. jnz short dodwords
  101. align 4
  102. doneeq:
  103. xor eax,eax
  104. ret
  105. align 4
  106. donene:
  107. ; The instructions below should place -1 in eax if src < dst,
  108. ; and 1 in eax if src > dst.
  109. sbb eax,eax
  110. sal eax,1
  111. inc eax
  112. ret
  113. align 4
  114. dopartial:
  115. test edx,1
  116. jz short doword
  117. mov al,[edx]
  118. inc edx
  119. cmp al,[ecx]
  120. jne short donene
  121. inc ecx
  122. or al,al
  123. jz short doneeq
  124. test edx,2
  125. jz short dodwords
  126. align 4
  127. doword:
  128. mov ax,[edx]
  129. add edx,2
  130. cmp al,[ecx]
  131. jne short donene
  132. or al,al
  133. jz short doneeq
  134. cmp ah,[ecx + 1]
  135. jne short donene
  136. or ah,ah
  137. jz short doneeq
  138. add ecx,2
  139. jmp short dodwords
  140. strcmp endp
  141. end