![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
![]()
__________________
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
@lostcauz: you must have been really bored
.. nice code ![]() |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#5 |
|
Newbie
|
Women are great!! You men wouldn't know what to do without us!!! =0)
What's wrong with sleeping in a cave, lostcauz? lol |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#7 |
|
Newbie
|
See... Now your woman can take care of you and all your ailments!
![]() |
|
|
|
|
|
#8 | |||
|
Programming Guru
![]() ![]() ![]() |
Quote:
LOL Quote:
Quote:
![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|||
|
|
|
|
|
#9 |
|
Newbie
|
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
![]() |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|