|
Hobbyist Programmer
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4 
|
I commented my code. This runs in 33 clocks on a 2.8 ghz XP sp2 after terminating any high priority processes.
str1 db 'abcdefghijklmnopqrstuvwxyzabc',0
str2 db 64 dup(0)
lea esi,str1 ; load effective address of string
lea edi,str2 ; load effective address of buffer
mov ecx,[sizeof str1-1] ; determine length of string less terminating '0'
mov ebx,ecx ; load length for manipulation
cmp ecx,4 ; ensure string is large enough for dword manipulations
jb l1 ; if not, work with one byte at a time
and ebx,3 ; test dword alignment by checking two least significant bytes
jz l2 ; if zero it's 4 byte aligned
cmp ebx,3 ; test if both lsb's set
jnz @f ; next test
sub ecx,1 ; both lsb's are set, work byte by byte until dword aligned
mov al,[esi+ecx] ; load last character
sub ebx,1 ; decrement for next test
mov [edi],al ; save byte to beginning of buffer
add edi,1 ; increment buffer position for next byte
@@: cmp ebx,2 ; test only bit 1
jnz @f ; next test
sub ecx,1 ; decrement string position
mov al,[esi+ecx] ; load byte
sub ebx,1 ; prepare for next byte
mov [edi],al ; save byte to buffer
add edi,1 ; increment buffer position
@@: cmp ebx,1 ; test only bit 0
jnz l1 ; next
sub ecx,1 ; update string position
mov al,[esi+ecx] ; load byte
mov [edi],al ; store to buffer
add edi,1 ; update buffer position
jmp l2 ; move on to dword manipulation
l1: sub ecx,1 ; string is less than 4 characters
mov al,[esi+ecx] ; load byte
mov [edi],al ; store byte
add edi,1 ; update buffer
or ecx,ecx ; last byte?
jz l3 ; done
jmp l1 ; continue
l2: sub ecx,4 ; back up 4 bytes
jnz @f ; if > 0 go to dword loading loop
mov eax,[esi+ecx]; else load the only dword
bswap eax ; reverse dword
mov [edi],eax ; store dword
jmp l3 ; done
@@: mov eax,[esi+ecx]; load dword
bswap eax ; swap bytes
mov [edi],eax ; store dword
add edi,4 ; update buffer for next dword
sub ecx,4 ; back up to next dword
jnz @b ; loop
mov eax,[esi] ; load last dword
bswap eax ; swap
mov [edi],eax ; store and done. str2 contains str1 reversed
l3: Last one, I promise. 
__________________
-- lostcauz
Stepped in what?...
Behind whose barn?...
I didn't even know they had a cow!
|