在汇编中查找子字符串
我想知道是否有一种比我目前计划做的更有效的方法来在汇编中查找子字符串。
我知道字符串指令“scansb/scasw/scads”可以将 EAX 中的值与 EDI 寻址的值进行比较。但是,据我了解,使用这种方法一次只能搜索一个字符。
因此,如果我想在字符串“pleasehelpme”中找到“help”的位置,我可以使用 scansb 找到 h 的偏移量,然后跳转到另一个函数来比较余数。如果余数不正确,我会跳回 scansb 并尝试再次搜索,这次是在前一个偏移标记之后。
然而,我不想这样做,然后发现有一个更有效的方法。有什么建议吗?提前致谢
I'm wondering if there is a more efficient method to finding a substring in assembly then what I am currently planning to do.
I know the string instruction "scansb/scasw/scads" can compare a value in EAX to a value addressed by EDI. However, as far as I understand, I can only search for one character at a time using this methodology.
So, if I want to find the location of "help" in string "pleasehelpme", I could use scansb to find the offset of the h, then jump to another function where I compare the remainder. If the remainder isn't correct, I jump back to scansb and try searching again, this time after the previous offset mark.
However, I would hate to do this and then discover there is a more efficient method. Any advice? Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确实有更有效的方法,无论是指令方面还是算法方面。
如果您有硬件,则可以使用 sse 4.2 比较字符串函数,该函数非常快。查看概述 http://software.intel.com/sites/products/documentation/studio/composer/en-us/2009/compiler_c/intref_cls/common/intref_sse42_comp.htm 以及使用 C 内联函数的示例 http:// software.intel.com/en-us/articles/xml-parsing-accelerator-with-intel-streaming-simd-extensions-4-intel-sse4/
如果您有长子字符串或多个搜索模式,< a href="http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm" rel="nofollow">Boyer-Moore,Knuth-Morris-Pratt 和 Rabin-Karp 算法可能更有效。
There are indeed more efficient ways, both instruction-wise and algorithmically.
If you have the hardware you can use the sse 4.2 compare string functions, which are very fast. See an overview http://software.intel.com/sites/products/documentation/studio/composer/en-us/2009/compiler_c/intref_cls/common/intref_sse42_comp.htm and an example using the C instrinsics http://software.intel.com/en-us/articles/xml-parsing-accelerator-with-intel-streaming-simd-extensions-4-intel-sse4/
If you have long substrings or multiple search patterns, the Boyer-Moore, Knuth-Morris-Pratt and Rabin-Karp algorithms may be more efficient.
我认为没有更有效的方法(只能对此方法进行一些优化)。另外这可能会引起兴趣。
I don't think there is a more efficient method (only some optimizations that can be done to this method). Also this might be of interest.
scansb
是strcmp
的汇编变体,而不是strstr
的汇编变体。如果你想要一个真正有效的方法,那么你必须使用更好的算法。例如,如果您在长字符串中搜索,那么您可以尝试一些特殊的算法:http://en。 wikipedia.org/wiki/String_searching_algorithm
scansb
is the assembly variant forstrcmp
, not forstrstr
. if you want a really efficient method, then you have to use better algorithm.For example, if you search in a long string, then you could try some special algorithms: http://en.wikipedia.org/wiki/String_searching_algorithm