News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

String reverse

Started by lamer, December 23, 2005, 01:38:03 PM

Previous topic - Next topic

lamer

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


Ratch

lamer,

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

http://www.masmforum.com/simple/index.php?topic=2869.0

vadiu

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..

MichaelW

@@: 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


eschew obfuscation

lamer

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

hutch--

lamer,

Compliments, the algo looks nicely thought out.  :U
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Ratch


lamer

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