### Author Topic: hexidecimal input  (Read 9624 times)

#### shankle

• Member
• Posts: 868
##### hexidecimal input
« on: May 01, 2016, 08:38:09 PM »
User inputs a hexidecimal value.
It goes into a field like:
hexinput db '        ',0
I then need to get that value into a field like:
hexvalue  dd 0

This shouldn't be to hard but it is eluding me. :redface:
Thanks for any help.

#### anunitu

• Member
• Posts: 1150
##### Re: hexidecimal input
« Reply #1 on: May 01, 2016, 09:33:56 PM »
Is it just a matter of how the hexadecimal is formatted? like across a single or multiple bytes or just a value transferred to another register or table?

#### fearless

• Member
• Posts: 564
##### Re: hexidecimal input
« Reply #2 on: May 01, 2016, 10:17:05 PM »
The masm32 library function htodw (convert hex string to DWORD) maybe is what your looking for?

fearless

ASUS Crosshair 8 Hero, AMD 5950X, 32GB, MSI 5700XT, NZXT Kraken Z73, Seasonic 1000W PSU

#### shankle

• Member
• Posts: 868
##### Re: hexidecimal input
« Reply #3 on: May 02, 2016, 12:44:31 AM »
Didn't know the function "htpdw" even existed.
Will give it a shot...
Since I am coding in GoAsm I will have to modify this module.
I guess that's ok????????

#### jj2007

• Member
• Posts: 13315
• Assembly is fun ;-)
##### Re: hexidecimal input
« Reply #4 on: May 02, 2016, 02:41:33 AM »
Is it just a matter of how the hexadecimal is formatted?

There are three common formats:

Init
mov esi, Val("12345678h")
mov edi, Val("\$12345678")
mov ebx, Val("0x12345678")
deb 1, "Results", esi, x:esi, edi, x:edi, ebx, x:ebx
EndOfCode

Output:
Code: [Select]
`Resultsesi             305419896x:esi           12345678edi             305419896x:edi           12345678ebx             305419896x:ebx           12345678`

#### nidud

• Member
• Posts: 2388
##### Re: hexidecimal input
« Reply #5 on: May 02, 2016, 03:53:58 AM »
deleted
« Last Edit: February 25, 2022, 11:37:12 AM by nidud »

#### shankle

• Member
• Posts: 868
##### Re: hexidecimal input
« Reply #6 on: May 02, 2016, 06:09:23 AM »
Code: [Select]
`Thank you NIdud,This code is intended for a GoAsm programSo I have modified it slightly.Data entered is f42400H but could be any hex numberI get a result of "ni rorre"?dwtoa is a masm32 and GoAsm convert Dword to ascii string moduleBufAdd        dd  0saveeax       dd  0  ; temp saveHexInputData  db  '      ',0     ; set for 6 positions;      HexInputData to Decimal DD    lea esi,HexInputData    xor eax,eax    ; result xor ecx,ecx xor edx,edx xor edi,edi mov edi,5.lupe mov cl,[esi]  ; esi: string add esi,1 and ecx,not 030h bt ecx,6 sbb edx,edx and edx,55 sub ecx,edx shl eax,4 add eax,ecx dec edi   ; length of string jnz <.lupe mov [saveeax],eax; test code begin;tmsg     db   'testing result of HexInputData',0;tmsg2    db  '        ',0      mov ecx,[saveeax]      lea ebx,tmsg2      mov [BufAdd], ebx      invoke dwtoa, ecx,[BufAdd]      ; Hex DD to string                                      ; output is in tmsg2     invoke MessageBox, [hWnd], addr tmsg, addr tmsg2, MB_OK ;true; test code end`

#### nidud

• Member
• Posts: 2388
##### Re: hexidecimal input
« Reply #7 on: May 02, 2016, 06:20:05 AM »
deleted
« Last Edit: February 25, 2022, 11:37:32 AM by nidud »

#### jj2007

• Member
• Posts: 13315
• Assembly is fun ;-)
##### Re: hexidecimal input
« Reply #8 on: May 02, 2016, 07:05:59 AM »
Up to 16 byte

Pretty good :t

Code: [Select]
`Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)10268   cycles for 100 * xtol8641    cycles for 100 * xtol228680   cycles for 100 * MovVal30435   cycles for 100 * a2uq7903    cycles for 100 * Masm32 hval10242   cycles for 100 * xtol8586    cycles for 100 * xtol228742   cycles for 100 * MovVal30452   cycles for 100 * a2uq7902    cycles for 100 * Masm32 hval10284   cycles for 100 * xtol8640    cycles for 100 * xtol228934   cycles for 100 * MovVal30436   cycles for 100 * a2uq7903    cycles for 100 * Masm32 hval10272   cycles for 100 * xtol8624    cycles for 100 * xtol228764   cycles for 100 * MovVal30439   cycles for 100 * a2uq7897    cycles for 100 * Masm32 hval10275   cycles for 100 * xtol8991    cycles for 100 * xtol228663   cycles for 100 * MovVal30536   cycles for 100 * a2uq7902    cycles for 100 * Masm32 hval74      bytes for xtol62      bytes for xtol223      bytes for MovVal34      bytes for a2uq12      bytes for Masm32 hval00000000 00000000 12AB34CD 56EF7801     = eax xtol00000000 00000000 12AB34CD 56EF7801     = eax xtol200000000 00000000 12AB34CD 56EF7801     = eax MovVal00000000 00000000 0000000C 00000000     = eax a2uq00000000 00000000 00000000 12AB34CD     = eax Masm32 hval`
Couldn't convince a2uq to produce meaningful results, and hval is limited to a DWORD 8)

#### habran

• Member
• Posts: 1228
##### Re: hexidecimal input
« Reply #9 on: May 02, 2016, 02:36:45 PM »
Here is another way to do that:
Code: [Select]
`hex2num PROC uses ebx esi src:DWORD   mov esi,src   xor ebx, ebx   .for (::)      movsx eax, BYTE PTR [esi]      .break .if (!eax)      shl  ebx, 4      lea  esi,[esi+1]      lea  edx,[eax-48]      .if (edx <= 9)    mov eax, edx      .else         or   eax,20h lea edx, DWORD PTR [eax-97] .if (edx <= 5)      add  eax, -87         .endif      .endif      add  ebx, eax   .endfor   add  eax, ebx   ret hex2num ENDP`
and here is 64 bit version:
Code: [Select]
`hex2num PROC FRAME src:QWORD   xor   r8d, r8d   .for (::)       movsx eax, BYTE PTR [rcx]       .break .if (!eax)       shl  r8d, 4       lea  rcx,[rcx+1]       lea  edx,[rax-48]       .if (edx <= 9)          mov  eax, edx       .else        or   eax,20h        lea  edx, DWORD PTR [rax-97]        .if (edx <= 5)           add  eax, -87        .endif      .endif      add  r8d, eax   .endfor    add  eax, r8d    rethex2num ENDP`
Cod-Father

#### jj2007

• Member
• Posts: 13315
• Assembly is fun ;-)
##### Re: hexidecimal input
« Reply #10 on: May 02, 2016, 04:39:06 PM »
Code: [Select]
`Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)Assembled with HJWasm10179   cycles for 100 * xtol8317    cycles for 100 * xtol223974   cycles for 100 * MovVal88369   cycles for 100 * a2uqJ7893    cycles for 100 * Masm32 hval (dword)4608    cycles for 100 * hex2num (Habran, dword)10183   cycles for 100 * xtol8591    cycles for 100 * xtol223861   cycles for 100 * MovVal88544   cycles for 100 * a2uqJ7856    cycles for 100 * Masm32 hval (dword)4604    cycles for 100 * hex2num (Habran, dword)10179   cycles for 100 * xtol8663    cycles for 100 * xtol223860   cycles for 100 * MovVal88536   cycles for 100 * a2uqJ7890    cycles for 100 * Masm32 hval (dword)4613    cycles for 100 * hex2num (Habran, dword)74      bytes for xtol62      bytes for xtol223      bytes for MovVal34      bytes for a2uqJ12      bytes for Masm32 hval (dword)70      bytes for hex2num (Habran, dword)00000000 00000000 12AB34CD 56EF7801     = eax xtol00000000 00000000 12AB34CD 56EF7801     = eax xtol200000000 00000000 12AB34CD 56EF7801     = eax MovVal00000000 00000000 12AB34CD 56EF7801     = eax a2uqJ00000000 00000000 00000000 12AB34CD     = eax Masm32 hval (dword)00000000 00000000 00000003 12AB34CD     = eax hex2num (Habran, dword)`

#### habran

• Member
• Posts: 1228
##### Re: hexidecimal input
« Reply #11 on: May 02, 2016, 07:52:21 PM »
Impresive :icon_exclaim:
I've done it for the beauty and to demonstrate HJWasm capabilities, I did not expect such advantage in the speed :shock:
I wonder how much faster is 64 bit version because there is no overhead.
Here is the result from my machine:
Code: [Select]
`Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz (SSE4)Assembled with HJWasm6585    cycles for 100 * xtol5837    cycles for 100 * xtol216475   cycles for 100 * MovVal63942   cycles for 100 * a2uqJ5376    cycles for 100 * Masm32 hval (dword)3120    cycles for 100 * hex2num (Habran, dword)6508    cycles for 100 * xtol5856    cycles for 100 * xtol216326   cycles for 100 * MovVal63416   cycles for 100 * a2uqJ5320    cycles for 100 * Masm32 hval (dword)3111    cycles for 100 * hex2num (Habran, dword)6518    cycles for 100 * xtol5913    cycles for 100 * xtol216261   cycles for 100 * MovVal63464   cycles for 100 * a2uqJ5330    cycles for 100 * Masm32 hval (dword)3140    cycles for 100 * hex2num (Habran, dword)74      bytes for xtol62      bytes for xtol223      bytes for MovVal34      bytes for a2uqJ12      bytes for Masm32 hval (dword)70      bytes for hex2num (Habran, dword)00000000 00000000 12AB34CD 56EF7801     = eax xtol00000000 00000000 12AB34CD 56EF7801     = eax xtol200000000 00000000 12AB34CD 56EF7801     = eax MovVal00000000 00000000 12AB34CD 56EF7801     = eax a2uqJ00000000 00000000 00000000 12AB34CD     = eax Masm32 hval (dword)00000000 00000000 00000003 12AB34CD     = eax hex2num (Habran, dword)--- ok ---`
Cod-Father

#### nidud

• Member
• Posts: 2388
##### Re: hexidecimal input
« Reply #12 on: May 02, 2016, 09:26:53 PM »
deleted
« Last Edit: February 25, 2022, 11:37:50 AM by nidud »

#### shankle

• Member
• Posts: 868
##### Re: hexidecimal input
« Reply #13 on: May 02, 2016, 09:32:42 PM »
Thanks everyone for responding.
To Nidud,
I am not able to get any of your versions to come up with
the correct result.
To Habran,
I have to convert your code to GoAsm 64-bit and that is
a problem for me.

#### habran

• Member
• Posts: 1228
##### Re: hexidecimal input
« Reply #14 on: May 02, 2016, 10:33:10 PM »
Hi shankle,
Why do you think that you must use GoAsm ;)
I make my choices by using common sense 8)
Cod-Father