hi! im trying to code again ;D any optimization tips would be nice


;hex string to dd!

htod proc uses ecx edx esi hexstr:DWORD

xor eax, eax
xor ecx, ecx
xor edx, edx
mov esi, hexstr
hexloop:
mov dl, byte ptr

test dl, dl
jz hexit ; success

cmp dl, 30h
jl error
cmp dl, 39h
jle number
cmp dl, 40h
jle error
cmp dl, 46h
jle letter
cmp dl, 60h
jle error
cmp dl, 66h
jle letter

error: mov eax, -1 ; TODO ;)
hexit: ret

letter: sub dl, 7
number: and dl, 0Fh

shl eax, 4
jc error ;overflow

or eax, edx
inc ecx
jmp hexloop
htod endp
Posted on 2003-02-21 17:49:49 by sortof
Posted on 2003-02-22 12:34:03 by bitRAKE
You can use my improved variant of buliaNaza's algo


OPTION PROLOGUE:NONE ;turn it off
OPTION EPILOGUE:NONE ;
Align 16 ;You must include always Align 16 before the proc!!!
l_htodw PROC lpString:DWORD ;
mov ecx, [esp+4] ;ecx->lpString
mov eax, 0 ;
db 3Eh ;ds: prefix
mov [esp+4], ebx ;save ebx register
xor ebx, ebx ;
L_1: ;
db 3Eh ;
movzx edx, byte ptr [ecx] ;
shl eax,4 ;
and ebx, 0Fh ;
add eax, ebx ;
cmp edx,40h ;
sbb ebx,ebx ;
sub edx,37h ;
and ebx,7 ;
inc ecx ;
add ebx, edx ;
jns L_1 ;
mov ebx, [esp+4] ;restore ebx register
ret 4 ;
l_htodw ENDP ;
OPTION PROLOGUE:PROLOGUEDEF ; turn back on the defaults
OPTION EPILOGUE:EPILOGUEDEF ;


Why improved? For PPro, PII and PIII we have:
buliaNaza's algo in memory:

ALign 16
[B]004011D0[/B] 8B4C2404 mov ecx, dword ptr [esp+4] ;D0
004011D4 33C0 xor eax, eax ;D1
004011D6 33D2 xor edx, edx ;D2
L_1: ;
[B]004011D8[/B] C1E004 shl eax, 4 ;[B]D0[/B]
004011DB 83E20F and edx, 0Fh ;D1
004011DE 03C2 add eax, edx ;[B]D0 D0[/B]
[B]004011E0[/B] 0FB611 movzx edx, byte ptr [ecx] ;D0 D1
004011E3 41 inc ecx ;D1 D2
004011E4 83FA40 cmp edx, 40h ;[B]D0[/B]
[B]004011E7[/B] 1BDB sbb ebx, ebx ;[B]D0[/B]
004011E9 83EA37 sub edx, 37h ;D1
004011EC 83E307 and ebx, 7 ;[B]D0[/B]
[B]004011EF[/B] 03D3 add edx, ebx ;[B]D0[/B]
004011F1 79E5 jns L_1 (004011D8) ;D1
004011F3 C3 ret ;

My variant in memory:
Align 16
[B]004011D0[/B] 8B4C2404 mov ecx, dword ptr [esp+4] ;D0
004011D4 B800000000 mov eax, 0 ;D1
004011D9 3E895C2404 mov dword ptr ds:[esp+4], ebx ;D0
004011DE 33DB xor ebx, ebx ;D1
L_1: ;
[B]004011E0[/B] 3E0FB611 movzx edx, byte ptr ds:[ecx] ;[B]D0[/B]
004011E4 C1E004 shl eax, 4 ;D1
004011E7 83E30F and ebx, 0Fh ;D2
004011EA 03C3 add eax, ebx ;[B]D0[/B]
004011EC 83FA40 cmp edx, 40h ;D1
[B]004011EF[/B] 1BDB sbb ebx, ebx ;[B]D0[/B]
004011F1 83EA37 sub edx, 37h ;D1
004011F4 83E307 and ebx, 7 ;[B]D0[/B]
004011F7 41 inc ecx ;D1
004011F8 03DA add ebx, edx ;[B]D0[/B]
004011FA 79E4 jns L_1 (004011E0) ;D1
[B]004011FC[/B] 8B5C2404 mov ebx, dword ptr [esp+4] ;D0
00401200 C20400 ret 4 ;


Regards,
Lingo
Posted on 2003-02-23 01:26:40 by lingo12
Do you use mov eax,0 instead of xor eax,eax to align 16 start of loop?
Posted on 2003-02-28 18:50:22 by The Svin
Yes, and "db 3Eh ;ds: prefix " too...

Regards,
Lingo
Posted on 2003-02-28 20:56:55 by lingo12
Size optimized.


;; 0030:0039-0041:0046
aam 10h ;; 0300:0309-0401:0406
aad 9 ;; 001B:0024-0025:002A
sub al,1Bh ;; 0000:0009-000A:000F
Posted on 2003-03-01 04:01:15 by Nexo
if al for example = 31h ("1")
the code makes it 13h
What is it for?
Posted on 2003-03-02 00:11:36 by The Svin

if al for example = 31h ("1")
the code makes it 13h
What is it for?


Replace you CPU or debugger or something other ;)

; al=31h
aam 10h ; ah=31h/10h=03h al=31h%10h=01h
aad 9 ; al=ah*9+al=3*9+1=1Ch
sub al,1Bh ; al=1Ch-1Bh=1

It is code excellent working already 12 years (many thanks to Stepan).
Posted on 2003-03-02 01:51:51 by Nexo
Replace you CPU or debugger or something other ;)

Hands? :)
I understand now, though can't figure out what kind
of code I entered in the first place :)
Posted on 2003-03-02 04:56:19 by The Svin
Works only with capital ABCDEF.
Bit 20h need to be reset to make it universal.
Posted on 2003-03-02 05:39:34 by The Svin

Works only with capital ABCDEF.

It is was indicated from comments.
Bit 20h need to be reset to make it universal.

Bitmask 20h? May be set bit for more universality:


or al,20h ;; 0030:0039-0061:0066
aam 10h ;; 0300:0309-0601:0606
aad 3 ;; 0009:0012-0013:0018
sub al,9 ;; 0000:0009-000A:000F
Posted on 2003-03-02 06:28:11 by Nexo
Works fine now.
Posted on 2003-03-02 07:52:28 by The Svin