I have been working on a way to convert a hex string to a dword, I have come up with this. It currently takes around 104 clocks to convert "0xffffffff" but I am sure it can be faster. Anyone have any optimization ideas ?

It has to handle trailing H or h and leading 0 or 0x. In my version if cbLen is 0 it will calculate the length but that is not necessary if it can be done faster...

hex2dw FRAME pString,cbLen

uses edi,ebx

mov edi,[pString]
mov ebx,[cbLen]
dec ebx
or ebx,ebx
jns >P1
mov ebx,edi

:
mov al,[ebx]
inc ebx
or al,al
jnz <
sub ebx,edi
sub ebx,2
P1:

mov al,[edi+ebx]
or al,20h
cmp al,"h"
jne >
dec ebx
:

xor ecx,ecx
xor eax,eax
xor edx,edx
L1:
mov edx,[edi+ebx]
and edx,0FFh
cmp edx,"x"
je >L3
sub edx,"0"
js >L3
cmp edx,9
jle >L2
and edx,07h
add edx,9
L2:
shl edx,cl
or eax,edx
add ecx,4
dec ebx
jns <L1
L3:

RET

ENDF
Posted on 2004-04-11 23:39:33 by donkey
Hello

IMO u should avoid jle >L2

Sthg like (assuming u have uppercase) :
mov r32,0
cmp edx,9
cmovg r32,7
sub edx,r32


And why not sthg like :
.data
an_hex: db 'AAAAAAAA'
mask: db 7,7,7,7,7,7,7,7
mask48: db '00000000'

.code
movq mm0,an_hex


movq mm1,
movq mm2,
pcmpgtb mm1,mm0
pand mm1,
psubusb mm2,mm1 ;
psubusb mm2,

Grosso modo and untested :grin:
Regards
Posted on 2004-04-14 06:04:19 by valy