Assembly code snippets

Back to the snippets overview

Details

TitleHexadecimal String to DWORD (MMX)
AuthorbitRAKE
Submitted by:bitRAKE
Date added:2002-03-24 03:41:40
Date modified:2002-03-24 03:41:40

Comments

Fastest conversion of a hexadecimal number to DWORD

Snippet

StrHex2bin PROC
    _CONST SEGMENT
        lpString db "89aBcDeF"
    _CONST ENDS
    movq mm0,QWORD PTR [lpString]

    psubusb  mm0,mxc(<30>) ; "0" = 0
    movq mm1,mm0

    pcmpgtb mm1,mxc(<09>) ; letter?
    pand mm1,mxc(<07>)

    psubusb mm0,mm1 ; fix letters

    movq mm1,mm0         ; 0F0E0D0C0B0A0908
    pand mm0,mxc(<0F00>) ; 0E000C000A000800
    pand mm1,mxc(<000F>) ; 000F000D000B0009
    psrlq mm0,8          ; 000E000C000A0008

    packuswb mm1,mm1     ; 0F0D0B09
    packuswb mm0,mm0     ; 0E0C0A08
    psllq mm1,4          ; F0D0B090
    por mm0,mm1          ; FEDCBA98
    movd eax,mm0         ; FEDCBA98

    bswap eax            ; 89ABCDEF
    ret
StrHex2bin ENDP