The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: lamer on December 23, 2005, 01:38:03 PM

Title: String reverse
Post by: lamer on December 23, 2005, 01:38:03 PM
Hi all!
I wrote a little function for reversing string, that reverses the input buffer (no need in second buffer) and works approx 1.5 times faster than szRev function from masm32.lib. May be somebody will find it usefull.
StrReverse proc lpString:LPSTR
mov eax,lpString                       ; put string address in EAX
mov edx,eax                            ; same in EDX
@@:                                    ; move EDX to the end of string
add edx,1
cmp byte ptr [edx],0
jne @B
sub edx,1                              ; return EDX to last not null character
@@:
mov cl,byte ptr [eax]          ; swap EAX and EDX, moving from two brinks
mov ch,byte ptr [edx]          ; in opposite directions
mov [eax],ch                   ; while EAX is less than EDX
mov [edx],cl
add eax,1
sub edx,1
cmp eax,edx
jl @B
ret
StrReverse endp

Title: Re: String reverse
Post by: Ratch on December 23, 2005, 04:54:33 PM
lamer,

     String reversal is a topic which has been exhaustively plowed over.  Ratch

http://www.masmforum.com/simple/index.php?topic=2869.0
Title: Re: String reverse
Post by: vadiu on December 23, 2005, 05:06:11 PM
Why do we have two equal labels ("@@") and Masm doesn't complain?
What is @B? I've seen this more times and I don't understand it...
Thanks..
Title: Re: String reverse
Post by: MichaelW on December 23, 2005, 05:20:42 PM
@@: is an anonymous label, @F (forward) refers to next @@:, and @B (back) refers to the previous @@:. See Predefined Symbols here:

http://webster.cs.ucr.edu/Page_TechDocs/MASMDoc/ReferenceGuide/Chap_03.htm

And Anonymous Labels here:

http://webster.cs.ucr.edu/Page_TechDocs/MASMDoc/ProgrammersGuide/Chap_07.htm


Title: Re: String reverse
Post by: lamer on December 23, 2005, 09:19:04 PM
Ratch,
It's impossible to keep track of each topic - otherwise you just have either to give up your work or to desert your family (or to be a moderator :bg).
I just wanted to share...
By the way, the topic of string reversing was started at October 1, and I wrote the procedure much before - just saw it today and decided to post.
Next time will search before posting.

Regards
Title: Re: String reverse
Post by: hutch-- on December 23, 2005, 10:15:40 PM
lamer,

Compliments, the algo looks nicely thought out.  :U
Title: Re: String reverse
Post by: Ratch on December 24, 2005, 08:07:14 AM
MichaelW,

     Have you seen this thread?  Ratch

http://win.asmcommunity.net/board/index.php?topic=22540.0
Title: Re: String reverse
Post by: lamer on December 24, 2005, 10:09:23 AM
hutch--,
Thanks for compliments :lol
As I see the proc can be improved (there is no need in EBX at all), so I post an improved version. It is advisable to look at your own code after a couple of months :bg