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.

138 lines
3.4 KiB

  1. page ,132
  2. title strlen - return the length of a null-terminated string
  3. ;***
  4. ;strlen.asm - contains strlen() routine
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; strlen returns the length of a null-terminated string,
  10. ; not including the null byte itself.
  11. ;
  12. ;Revision History:
  13. ; 04-21-87 SKS Rewritten to be fast and small, added file header
  14. ; 05-18-88 SJM Add model-independent (large model) ifdef
  15. ; 08-02-88 SJM Add 32 bit code, use cruntime vs cmacros
  16. ; 08-23-88 JCR 386 cleanup
  17. ; 10-05-88 GJF Fixed off-by-2 error.
  18. ; 10-10-88 JCR Minor improvement
  19. ; 10-25-88 JCR General cleanup for 386-only code
  20. ; 10-26-88 JCR Re-arrange regs to avoid push/pop ebx
  21. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  22. ; 05-10-91 GJF Back to _cdecl, sigh...
  23. ; 04-23-93 GJF Tuned for the 486.
  24. ; 06-16-93 GJF Added .FPO directive.
  25. ; 11-28-94 GJF New, faster version from Intel.
  26. ; 11-28-95 GJF Align main_loop on para boundary for 486 and P6
  27. ;
  28. ;*******************************************************************************
  29. .xlist
  30. include cruntime.inc
  31. .list
  32. page
  33. ;***
  34. ;strlen - return the length of a null-terminated string
  35. ;
  36. ;Purpose:
  37. ; Finds the length in bytes of the given string, not including
  38. ; the final null character.
  39. ;
  40. ; Algorithm:
  41. ; int strlen (const char * str)
  42. ; {
  43. ; int length = 0;
  44. ;
  45. ; while( *str++ )
  46. ; ++length;
  47. ;
  48. ; return( length );
  49. ; }
  50. ;
  51. ;Entry:
  52. ; const char * str - string whose length is to be computed
  53. ;
  54. ;Exit:
  55. ; EAX = length of the string "str", exclusive of the final null byte
  56. ;
  57. ;Uses:
  58. ; EAX, ECX, EDX
  59. ;
  60. ;Exceptions:
  61. ;
  62. ;*******************************************************************************
  63. CODESEG
  64. public strlen
  65. strlen proc
  66. .FPO ( 0, 1, 0, 0, 0, 0 )
  67. string equ [esp + 4]
  68. mov ecx,string ; ecx -> string
  69. test ecx,3 ; test if string is aligned on 32 bits
  70. je short main_loop
  71. str_misaligned:
  72. ; simple byte loop until string is aligned
  73. mov al,byte ptr [ecx]
  74. inc ecx
  75. test al,al
  76. je short byte_3
  77. test ecx,3
  78. jne short str_misaligned
  79. add eax,dword ptr 0 ; 5 byte nop to align label below
  80. align 16 ; should be redundant
  81. main_loop:
  82. mov eax,dword ptr [ecx] ; read 4 bytes
  83. mov edx,7efefeffh
  84. add edx,eax
  85. xor eax,-1
  86. xor eax,edx
  87. add ecx,4
  88. test eax,81010100h
  89. je short main_loop
  90. ; found zero byte in the loop
  91. mov eax,[ecx - 4]
  92. test al,al ; is it byte 0
  93. je short byte_0
  94. test ah,ah ; is it byte 1
  95. je short byte_1
  96. test eax,00ff0000h ; is it byte 2
  97. je short byte_2
  98. test eax,0ff000000h ; is it byte 3
  99. je short byte_3
  100. jmp short main_loop ; taken if bits 24-30 are clear and bit
  101. ; 31 is set
  102. byte_3:
  103. lea eax,[ecx - 1]
  104. mov ecx,string
  105. sub eax,ecx
  106. ret
  107. byte_2:
  108. lea eax,[ecx - 2]
  109. mov ecx,string
  110. sub eax,ecx
  111. ret
  112. byte_1:
  113. lea eax,[ecx - 3]
  114. mov ecx,string
  115. sub eax,ecx
  116. ret
  117. byte_0:
  118. lea eax,[ecx - 4]
  119. mov ecx,string
  120. sub eax,ecx
  121. ret
  122. strlen endp
  123. end