home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / platform / strlen.asm < prev    next >
Assembly Source File  |  1998-06-17  |  3KB  |  123 lines

  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-1997, 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. ;*******************************************************************************
  13.  
  14.         .xlist
  15.         include cruntime.inc
  16.         .list
  17.  
  18. page
  19. ;***
  20. ;strlen - return the length of a null-terminated string
  21. ;
  22. ;Purpose:
  23. ;       Finds the length in bytes of the given string, not including
  24. ;       the final null character.
  25. ;
  26. ;       Algorithm:
  27. ;       int strlen (const char * str)
  28. ;       {
  29. ;           int length = 0;
  30. ;
  31. ;           while( *str++ )
  32. ;                   ++length;
  33. ;
  34. ;           return( length );
  35. ;       }
  36. ;
  37. ;Entry:
  38. ;       const char * str - string whose length is to be computed
  39. ;
  40. ;Exit:
  41. ;       EAX = length of the string "str", exclusive of the final null byte
  42. ;
  43. ;Uses:
  44. ;       EAX, ECX, EDX
  45. ;
  46. ;Exceptions:
  47. ;
  48. ;*******************************************************************************
  49.  
  50.         CODESEG
  51.  
  52.         public  strlen
  53.  
  54. strlen  proc
  55.  
  56.         .FPO    ( 0, 1, 0, 0, 0, 0 )
  57.  
  58. string  equ     [esp + 4]
  59.  
  60.         mov     ecx,string              ; ecx -> string
  61.         test    ecx,3                   ; test if string is aligned on 32 bits
  62.         je      short main_loop
  63.  
  64. str_misaligned:
  65.         ; simple byte loop until string is aligned
  66.         mov     al,byte ptr [ecx]
  67.         inc     ecx
  68.         test    al,al
  69.         je      short byte_3
  70.         test    ecx,3
  71.         jne     short str_misaligned
  72.  
  73.         add     eax,dword ptr 0         ; 5 byte nop to align label below
  74.  
  75.         align   16                      ; should be redundant
  76.  
  77. main_loop:
  78.         mov     eax,dword ptr [ecx]     ; read 4 bytes
  79.         mov     edx,7efefeffh
  80.         add     edx,eax
  81.         xor     eax,-1
  82.         xor     eax,edx
  83.         add     ecx,4
  84.         test    eax,81010100h
  85.         je      short main_loop
  86.         ; found zero byte in the loop
  87.         mov     eax,[ecx - 4]
  88.         test    al,al                   ; is it byte 0
  89.         je      short byte_0
  90.         test    ah,ah                   ; is it byte 1
  91.         je      short byte_1
  92.         test    eax,00ff0000h           ; is it byte 2
  93.         je      short byte_2
  94.         test    eax,0ff000000h          ; is it byte 3
  95.         je      short byte_3
  96.         jmp     short main_loop         ; taken if bits 24-30 are clear and bit
  97.                                         ; 31 is set
  98.  
  99. byte_3:
  100.         lea     eax,[ecx - 1]
  101.         mov     ecx,string
  102.         sub     eax,ecx
  103.         ret
  104. byte_2:
  105.         lea     eax,[ecx - 2]
  106.         mov     ecx,string
  107.         sub     eax,ecx
  108.         ret
  109. byte_1:
  110.         lea     eax,[ecx - 3]
  111.         mov     ecx,string
  112.         sub     eax,ecx
  113.         ret
  114. byte_0:
  115.         lea     eax,[ecx - 4]
  116.         mov     ecx,string
  117.         sub     eax,ecx
  118.         ret
  119.  
  120. strlen  endp
  121.  
  122.         end
  123.