hum.... what do you think of this htodw proc. it appeared clearer to me than the one by svin i found in m32lib, but i don' t really know for the speed, .... since you' r a clock time expert, svin, could you give me some advice ?

; htodw : converts an hexadecimal string to a dword
; input  :
;  ptHexString: pointer to the null-terminated hexadecimal
;  string to convert
;  the case doesn' t matters, the dword will be only the 
;  last 8 bytes if the string exceeds 8 bytes. the string
;  can be less than 8 bytes.
; output :
;  eax: converted dword

htodw			proc	uses esi edx,ptHexString:dword

			xor	edx,edx
			mov	esi,ptHexString

htodw_loop:		lodsb

			test	al,al
			jz	htodw_endloop

			shl	edx,4

			sub	al,"0"

			cmp	al,9
			jbe	htodw_addnumber

			cmp	al,"a" - "0"
			jb	@f

			add	al,"A" - "a"

@@:			add	al,"0" - "A" + 10

htodw_addnumber:	xor	dl,al

			jmp	htodw_loop

htodw_endloop:		mov	eax,edx

			ret

htodw			endp
Posted on 2001-04-04 09:26:00 by roy
Roy, Algo looks OK, you could replace the LODSB with, mov al, inc esi which is usually faster if the speed is what you are looking for. Regards, hutch@pbq.com.au
Posted on 2001-04-04 10:24:00 by hutch--
This proc is faster than mine, it used better high level algo. It's better. Though it may be a little bit improved. Thanks for the proc. I send explonation what can be improved later with new version, of the algo realization. If you have some more porcs - post. Good day - we have one stdproc faster. The Svin.
Posted on 2001-04-04 13:57:00 by The Svin
thank you svin i just admire your work just please take my proc and tweak it, turn it, rip it, squash it to improve it as much as you can, because i know you can do this 1000 time better than me =) i' m looking forward to seeing your improvements
Posted on 2001-04-04 15:32:00 by roy
Here is optimized version (though I think here can be further optimization - I have a blurred thought I can remove checking for L U cases) What is done: 1. Steve suggestion to use pointer and increament for loading into al - it speed up by ~ 9 - 10 clocks with most uncomfortable string "AbCdFfEe". 2. esi not used so we save 2 clocks for saving\restoring 3. Main - I removed test al,al for 0. because we can sub al,"0" and check if it's 0 in one instruction. I checked it with different strings now it's ~18 - 30% faster. (it highly depends on usage in string array "0" -"9" with this characters all proc runs faster, but optimisation improvements are contstant (removing one instruction inside the loop, loading al, and enter - exit proc are constant)

htodw3         proc   ptHexString:dword

         xor   edx,edx
         mov   ecx,ptHexString

htodw_loop:     
         mov al, 
         inc ecx
         sub   al,"0"
         js   htodw_endloop
         shl   edx,4
         cmp   al,9
         jbe   htodw_addnumber
         cmp   al,"a" - "0"
         jb   @f
         add   al,"A" - "a"

@@: 
        add   al,"0" - "A" + 10

htodw_addnumber:   xor   dl,al

         jmp   htodw_loop

htodw_endloop:      mov   eax,edx
	ret
htodw3         endp
Posted on 2001-04-04 15:48:00 by The Svin
nice ! very nice =) i' d like to keep the case convertion, i haven' t understood if you wanted to keep it or no, but well, do as you like. well, you' re really an optimization guru =)
Posted on 2001-04-04 15:53:00 by roy
I are a strange man, you wrote algo faster than mine and keep calling me GURU. Anyway I've figured out how to remove two more instruction from the loop (each of them slow down proc ~ by 10 % for each "a".."f" and "A"..."F" charachter) We can substruct for "a" to "f" case and at the same time check if it was U case, and if it was correct otherwise leave it to final addition:

htodw        proc   ptHexString:dword

         xor   edx,edx
         mov   ecx,ptHexString

htodw_loop:     
         mov al, 
         inc ecx
         sub   al,"0"
         js   htodw_endloop
         shl   edx,4
         cmp   al,9
         jbe   htodw_addnumber
         sub   al,"a" - "0" - 10      ;sub 27h to make a - 30h = 10 b - 30h =11...
         jns   htodw_addnumber ;if it was U case then SF will be set
         add al,20h		;then correct it for U case

htodw_addnumber:   xor   dl,al

         jmp   htodw_loop

htodw_endloop:      mov   eax,edx

The Svin.
Posted on 2001-04-05 17:32:00 by The Svin