Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   String reversal (http://www.programmingforums.org/showthread.php?t=5982)

lostcauz Sep 20th, 2005 9:06 AM

String reversal
 
I didn't want to keep polluting Jim's thread with non-C code, so the saga continues...

This algorithm is more robust and runs in 49-53 cycles on my P4. Comments, critiques, and corrections always welcome.

I'll be out of town for a couple of days since someone here gave my wife the bright idea of gettin' away from my routine. I won't mention any names but their initials are INFINITE RECURSION and TEECHA!!! She's even making me drive! Next thing you know she'll be making me sleep in a tent, or a cave! :D

DaWei, don't be booby trappin' my house while I'm gone. :p

:

;Reverse a string

str1 db 'abcdefghijklmnopqrstuvwxyzabc',0
str2 db 64 dup(0)

        lea esi,str1                       
        lea edi,str2
        mov ecx,[sizeof str1-2]
        cmp ecx,7
        ja l0
        or ecx,ecx
        jnz @f
        mov al,[esi]
        mov [edi],al
        add edi,1
        jmp l3
       
@@:    mov al,[esi+ecx]
        mov [edi],al
        add edi,1
        sub ecx,1
        jnz @b
        mov al,[esi]
        mov [edi],al
        add edi,1
        jmp l3

l0:    mov ebx,ecx
        shr ecx,2
        shl ecx,2
        sub ebx,ecx
        jz l1
        cmp ebx,3
        jnz @f
        mov al,[esi+ecx+3]
        sub ebx,1
        mov [edi],al
        add edi,1

@@:        cmp ebx,2
        jnz @f
        mov al,[esi+ecx+2]
        sub ebx,1
        mov [edi],al
        add edi,1

@@:        cmp ebx,1
        jnz l1
        mov al,[esi+ecx+1]
        mov [edi],al
        add edi,1
       
l1:    mov al,[esi+ecx]
        mov [edi],al
        add edi,1

l2:    sub ecx,4
        mov eax,[esi+ecx]        ; 4 byte aligned string now
        bswap eax
        mov [edi],eax
        add edi,4
        or ecx,ecx
        jnz l2                         
l3:


DaWei Sep 20th, 2005 10:06 AM

:cool:

Polyphemus_ Sep 20th, 2005 10:13 AM

@lostcauz: you must have been really bored :D.. nice code ;)

Infinite Recursion Sep 26th, 2005 9:20 AM

lol @ lostcauz... aren't women great :) Have fun man...

Here is where we are staying: http://ozarkcave.com

Teecha Sep 26th, 2005 9:26 AM

Women are great!! You men wouldn't know what to do without us!!! =0)

What's wrong with sleeping in a cave, lostcauz? lol

lostcauz Sep 26th, 2005 9:51 AM

Yes, I agree women are great and I definitely would be lost without her.

We went to a secluded area and I must say it sucked canal water! Now my back hurts, I have a cold, my feet hurt and to top it off I'm covered in poison oak. Thanks a lot! :p I won't be mentioning where you guys decided to go or sure enough I'll be a cave dweller next. :eek:

:

str1 db 'abcdefghijklmnopqrstuvwxyzabc',0
str2 db 64 dup(0)

        lea esi,str1          ; 43-45 cycles
        lea edi,str2
        mov ecx,[sizeof str1-1]
        mov ebx,ecx
        cmp ecx,4
        jb l1
        and ebx,3          ; test for alignment
        jz l2
        cmp ebx,3
        jnz @f
        sub ecx,1
        mov al,[esi+ecx]
        sub ebx,1
        mov [edi],al
        add edi,1

@@:        cmp ebx,2
        jnz @f
        sub ecx,1
        mov al,[esi+ecx]
        sub ebx,1
        mov [edi],al
        add edi,1

@@:        cmp ebx,1
        jnz l1
        sub ecx,1
        mov al,[esi+ecx]
        mov [edi],al
        add edi,1
        jmp l2
       
l1:        sub ecx,1          ; string is less than 4 characters
        mov al,[esi+ecx]
        mov [edi],al
        add edi,1
        or ecx,ecx
        jz l3
        jmp l1
       
l2:        sub ecx,4
        jnz @f
        mov eax,[esi+ecx]       
        bswap eax
        mov [edi],eax
        jmp l3
         
@@:        mov eax,[esi+ecx] ; 4 byte aligned string
        bswap eax
        mov [edi],eax
        add edi,4
        sub ecx,4
        jnz @b                         
        mov eax,[esi]       
        bswap eax
        mov [edi],eax
l3:


Teecha Sep 26th, 2005 9:54 AM

See... Now your woman can take care of you and all your ailments! ;)

Infinite Recursion Sep 26th, 2005 10:18 AM

Quote:

What's wrong with sleeping in a cave, lostcauz? lol
Nothing when it has 6 million dollars worth of technological rennovations, including dehumidifiers and jacuzzis :) LOL

Quote:

I'm covered in poison oak.
lol, ya gotta stop rolling around in the woods man.

Quote:

Now your woman can take care of you and all your ailments!
and if not, just scratch the poision oak spot and offer to give her a massage :)

Teecha Sep 26th, 2005 10:28 AM

Dehumidifier... hmmm.. I need one of those in my classroom NOW!! I'm about to sweat to death, and being in a little bitty room with 22 other people doesn't help matters :(

DaWei Sep 26th, 2005 10:46 AM

Reading this thread is getting under my skin. Pooh on you. I think I'll wait about a month and take a Caribbean cruise. Noooooo poison oak be's dere. :p


All times are GMT -5. The time now is 5:39 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC