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

  1.         page    ,132
  2.         title   strchr - search string for given character
  3. ;***
  4. ;strchr.asm - search a string for a given character
  5. ;
  6. ;       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ;       defines strchr() - search a string for a character
  10. ;
  11. ;*******************************************************************************
  12.  
  13.         .xlist
  14.         include cruntime.inc
  15.         .list
  16.  
  17. page
  18. ;***
  19. ;char *strchr(string, chr) - search a string for a character
  20. ;
  21. ;Purpose:
  22. ;       Searches a string for a given character, which may be the
  23. ;       null character '\0'.
  24. ;
  25. ;       Algorithm:
  26. ;       char *
  27. ;       strchr (string, chr)
  28. ;       char *string, chr;
  29. ;       {
  30. ;         while (*string && *string != chr)
  31. ;             string++;
  32. ;         if (*string == chr)
  33. ;             return(string);
  34. ;         return((char *)0);
  35. ;       }
  36. ;
  37. ;Entry:
  38. ;       char *string - string to search in
  39. ;       char chr     - character to search for
  40. ;
  41. ;Exit:
  42. ;       returns pointer to the first occurence of c in string
  43. ;       returns NULL if chr does not occur in string
  44. ;
  45. ;Uses:
  46. ;
  47. ;Exceptions:
  48. ;
  49. ;*******************************************************************************
  50.  
  51.         CODESEG
  52.  
  53. found_bx:
  54.         lea     eax,[edx - 1]
  55.         pop     ebx                 ; restore ebx
  56.         ret                         ; _cdecl return
  57.  
  58.         align   16
  59.         public  strchr, __from_strstr_to_strchr
  60. strchr  proc
  61.  
  62.         .FPO    ( 0, 2, 0, 0, 0, 0 )
  63.  
  64.         xor     eax,eax
  65.         mov     al,[esp + 8]        ; al = chr (search char)
  66.  
  67. __from_strstr_to_strchr label proc
  68.  
  69.         push    ebx                 ; PRESERVE EBX
  70.         mov     ebx,eax             ; ebx = 0/0/0/chr
  71.         shl     eax,8               ; eax = 0/0/chr/0
  72.         mov     edx,[esp + 8]       ; edx = buffer
  73.         test    edx,3               ; test if string is aligned on 32 bits
  74.         jz      short main_loop_start
  75.  
  76. str_misaligned:                     ; simple byte loop until string is aligned
  77.         mov     cl,[edx]
  78.         inc     edx
  79.         cmp     cl,bl
  80.         je      short found_bx
  81.         test    cl,cl
  82.         jz      short retnull_bx
  83.         test    edx,3               ; now aligned ?
  84.         jne     short str_misaligned
  85.  
  86. main_loop_start:                    ; set all 4 bytes of ebx to [chr]
  87.         or      ebx,eax             ; ebx = 0/0/chr/chr
  88.         push    edi                 ; PRESERVE EDI
  89.         mov     eax,ebx             ; eax = 0/0/chr/chr
  90.         shl     ebx,10h             ; ebx = chr/chr/0/0
  91.         push    esi                 ; PRESERVE ESI
  92.         or      ebx,eax             ; ebx = all 4 bytes = [chr]
  93.  
  94. ; in the main loop (below), we are looking for chr or for EOS (end of string)
  95.  
  96. main_loop:
  97.         mov     ecx,[edx]           ; read  dword (4 bytes)
  98.         mov     edi,7efefeffh       ; work with edi & ecx for looking for chr
  99.  
  100.         mov     eax,ecx             ; eax = dword
  101.         mov     esi,edi             ; work with esi & eax for looking for EOS
  102.  
  103.         xor     ecx,ebx             ; eax = dword xor chr/chr/chr/chr
  104.         add     esi,eax
  105.  
  106.         add     edi,ecx
  107.         xor     ecx,-1
  108.  
  109.         xor     eax,-1
  110.         xor     ecx,edi
  111.  
  112.         xor     eax,esi
  113.         add     edx,4
  114.  
  115.         and     ecx,81010100h       ; test for chr
  116.         jnz     short chr_is_found  ; chr probably has been found
  117.  
  118.         ; chr was not found, check for EOS
  119.  
  120.         and     eax,81010100h       ; is any flag set ??
  121.         jz      short main_loop     ; EOS was not found, go get another dword
  122.  
  123.         and     eax,01010100h       ; is it in high byte?
  124.         jnz     short retnull       ; no, definitely found EOS, return failure
  125.  
  126.         and     esi,80000000h       ; check was high byte 0 or 80h
  127.         jnz     short main_loop     ; it just was 80h in high byte, go get
  128.                                     ; another dword
  129. retnull:
  130.         pop     esi
  131.         pop     edi
  132. retnull_bx:
  133.         pop     ebx
  134.         xor     eax,eax
  135.         ret                         ; _cdecl return
  136.  
  137. chr_is_found:
  138.         mov     eax,[edx - 4]       ; let's look one more time on this dword
  139.         cmp     al,bl               ; is chr in byte 0?
  140.         je      short byte_0
  141.         test    al,al               ; test if low byte is 0
  142.         je      retnull
  143.         cmp     ah,bl               ; is it byte 1
  144.         je      short byte_1
  145.         test    ah,ah               ; found EOS ?
  146.         je      retnull
  147.         shr     eax,10h             ; is it byte 2
  148.         cmp     al,bl
  149.         je      short byte_2
  150.         test    al,al               ; if in al some bits were set, bl!=bh
  151.         je      retnull
  152.         cmp     ah,bl
  153.         je      short byte_3
  154.         test    ah,ah
  155.         jz      retnull
  156.         jmp     short main_loop     ; neither chr nor EOS found, go get
  157.                                     ; another dword
  158. byte_3:
  159.         pop     esi
  160.         pop     edi
  161.         lea     eax,[edx - 1]
  162.         pop     ebx                 ; restore ebx
  163.         ret                         ; _cdecl return
  164.  
  165. byte_2:
  166.         lea     eax,[edx - 2]
  167.         pop     esi
  168.         pop     edi
  169.         pop     ebx
  170.         ret                         ; _cdecl return
  171.  
  172. byte_1:
  173.         lea     eax,[edx - 3]
  174.         pop     esi
  175.         pop     edi
  176.         pop     ebx
  177.         ret                         ; _cdecl return
  178.  
  179. byte_0:
  180.         lea     eax,[edx - 4]
  181.         pop     esi                 ; restore esi
  182.         pop     edi                 ; restore edi
  183.         pop     ebx                 ; restore ebx
  184.         ret                         ; _cdecl return
  185.  
  186. strchr  endp
  187.         end
  188.  
  189.