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.

105 lines
2.3 KiB

  1. page ,132
  2. title strrchr - find last occurence of character in string
  3. ;***
  4. ;strrchr.asm - find last occurrence of character in string
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines strrchr() - find the last occurrence of a given character
  10. ; in a string.
  11. ;
  12. ;Revision History:
  13. ; 10-27-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-26-90 GJF Changed to _stdcall. Also, fixed the copyright.
  19. ; 05-10-91 GJF Back to _cdecl, sigh...
  20. ;
  21. ;*******************************************************************************
  22. .xlist
  23. include cruntime.inc
  24. .list
  25. page
  26. ;***
  27. ;char *strrchr(string, ch) - find last occurrence of ch in string
  28. ;
  29. ;Purpose:
  30. ; Finds the last occurrence of ch in string. The terminating
  31. ; null character is used as part of the search.
  32. ;
  33. ; Algorithm:
  34. ; char *
  35. ; strrchr (string, ch)
  36. ; char *string, ch;
  37. ; {
  38. ; char *start = string;
  39. ;
  40. ; while (*string++)
  41. ; ;
  42. ; while (--string != start && *string != ch)
  43. ; ;
  44. ; if (*string == ch)
  45. ; return(string);
  46. ; return(NULL);
  47. ; }
  48. ;
  49. ;Entry:
  50. ; char *string - string to search in
  51. ; char ch - character to search for
  52. ;
  53. ;Exit:
  54. ; returns a pointer to the last occurrence of ch in the given
  55. ; string
  56. ; returns NULL if ch does not occurr in the string
  57. ;
  58. ;Uses:
  59. ;
  60. ;Exceptions:
  61. ;
  62. ;*******************************************************************************
  63. CODESEG
  64. public strrchr
  65. strrchr proc \
  66. uses edi, \
  67. string:ptr byte, \
  68. chr:byte
  69. mov edi,[string] ; di = string
  70. xor eax,eax ; al=null byte
  71. or ecx,-1 ; cx = -1
  72. repne scasb ; find the null & count bytes
  73. inc ecx ; cx=-byte count (with null)
  74. neg ecx ; cx=+byte count (with null)
  75. dec edi ; di points to terminal null
  76. mov al,chr ; al=search byte
  77. std ; count 'down' on string this time
  78. repne scasb ; find that byte
  79. inc edi ; di points to byte which stopped scan
  80. cmp [edi],al ; see if we have a hit
  81. je short returndi ; yes, point to byte
  82. xor eax,eax ; no, return NULL
  83. jmp short toend ; do return sequence
  84. returndi:
  85. mov eax,edi ; ax=pointer to byte
  86. toend:
  87. cld
  88. ifdef _STDCALL_
  89. ret DPSIZE + ISIZE ; _stdcall return
  90. else
  91. ret ; _cdecl return
  92. endif
  93. strrchr endp
  94. end