Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 20th, 2005, 9:06 AM   #1
lostcauz
Hobbyist Programmer
 
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4 lostcauz is on a distinguished road
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!

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:
__________________
-- lostcauz

Stepped in what?...
Behind whose barn?...
I didn't even know they had a cow!
lostcauz is offline   Reply With Quote
Old Sep 20th, 2005, 10:06 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 20th, 2005, 10:13 AM   #3
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
@lostcauz: you must have been really bored .. nice code
Polyphemus_ is offline   Reply With Quote
Old Sep 26th, 2005, 9:20 AM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
lol @ lostcauz... aren't women great Have fun man...

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

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 26th, 2005, 9:26 AM   #5
Teecha
Newbie
 
Teecha's Avatar
 
Join Date: Aug 2005
Location: U.S.
Posts: 13
Rep Power: 0 Teecha is on a distinguished road
Send a message via AIM to Teecha Send a message via MSN to Teecha Send a message via Yahoo to Teecha
Women are great!! You men wouldn't know what to do without us!!! =0)

What's wrong with sleeping in a cave, lostcauz? lol
Teecha is offline   Reply With Quote
Old Sep 26th, 2005, 9:51 AM   #6
lostcauz
Hobbyist Programmer
 
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4 lostcauz is on a distinguished road
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:
__________________
-- lostcauz

Stepped in what?...
Behind whose barn?...
I didn't even know they had a cow!
lostcauz is offline   Reply With Quote
Old Sep 26th, 2005, 9:54 AM   #7
Teecha
Newbie
 
Teecha's Avatar
 
Join Date: Aug 2005
Location: U.S.
Posts: 13
Rep Power: 0 Teecha is on a distinguished road
Send a message via AIM to Teecha Send a message via MSN to Teecha Send a message via Yahoo to Teecha
See... Now your woman can take care of you and all your ailments!
Teecha is offline   Reply With Quote
Old Sep 26th, 2005, 10:18 AM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 26th, 2005, 10:28 AM   #9
Teecha
Newbie
 
Teecha's Avatar
 
Join Date: Aug 2005
Location: U.S.
Posts: 13
Rep Power: 0 Teecha is on a distinguished road
Send a message via AIM to Teecha Send a message via MSN to Teecha Send a message via Yahoo to Teecha
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
Teecha is offline   Reply With Quote
Old Sep 26th, 2005, 10:46 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:06 AM.

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