flat assembler
Message board for the users of flat assembler.

Index > Main > "strstr" - string searching

Author
Thread Post new topic Reply to topic
pal



Joined: 26 Aug 2008
Posts: 227
pal 26 May 2009, 17:16
Well yeah, I'm trying to make an strstr function (searches for a string within a string then returns the first offset position e.g. searching for "l" in "hello" would return 3). I can do it using all cmp, lea etc, but I wanna do it using the rep mnemonic.

So I was thinking:

- Load the strings with lea or w/e
- Use one of the repeated ones comparing the two strings until they are either equal or one of them equals to zero. Not sure if there is one for doing this :S
- Compare the two strings until they don't match, then compare the needle string byte to 0 (to see if the whole string was matched). If so then return a number, if not then do it again or something.

Is this the right way of doing this? Obviously I don't want a full code sample as it would be pointless, but any pointers? Sorry for all the questions by the way (just it takes me ages to code in assembly so I will waste soo much time doing this).

EDIT by DOS386 2009-05-30 : enhanced subject
Post 26 May 2009, 17:16
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 27 May 2009, 02:20
It would be really slow to compare the two strings sequentially without aborting immediately when there's no match, and I don't think it's possible with rep to "break" out of it, is there?
Post 27 May 2009, 02:20
View user's profile Send private message Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete 27 May 2009, 06:53
Don't you want to use the special string instructions (stos) and registers (edi,esi) for those tasks?
Post 27 May 2009, 06:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20459
Location: In your JS exploiting you and your system
revolution 27 May 2009, 06:56
Borsuc wrote:
... I don't think it's possible with rep to "break" out of it, is there?
Code:
repz cmpsb    
Post 27 May 2009, 06:56
View user's profile Send private message Visit poster's website Reply with quote
pal



Joined: 26 Aug 2008
Posts: 227
pal 27 May 2009, 14:57
Hmm, I'm thinking that to get the offset of a possible occurance I am going to have to manually do it as cmpsb will increment both values, which I do not want obviously. From there it should be simple.

Push the placement, do a repz cmpsb (the only problem is if both of the strings are the same and have null chars after then then it will carry on incrementing for ages), once it breaks compare the value in the needle string to 0, if it is then it is the occurance, if not then go back to finding an occurance. Or something like that. Thats what I think it should be anyway.
Post 27 May 2009, 14:57
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 27 May 2009, 16:56
Post 27 May 2009, 16:56
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 27 May 2009, 21:50
Check out the BlackBook chapter 9 for some nice tricks.

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 27 May 2009, 21:50
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 27 May 2009, 22:51
revolution wrote:
Borsuc wrote:
... I don't think it's possible with rep to "break" out of it, is there?
Code:
repz cmpsb    
nice, revolution, I wasn't aware of that thanks.

_________________
Previously known as The_Grey_Beast
Post 27 May 2009, 22:51
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.