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