News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

hexidecimal input

Started by shankle, May 01, 2016, 08:38:09 PM

Previous topic - Next topic

shankle

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

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

The masm32 library function htodw (convert hex string to DWORD) maybe is what your looking for?


shankle

Thanks guys for replying.
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

Quote from: anunitu on May 01, 2016, 09:33:56 PM
Is it just a matter of how the hexadecimal is formatted?

There are three common formats:

include \masm32\MasmBasic\MasmBasic.inc      ; download
  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:
Results
esi             305419896
x:esi           12345678
edi             305419896
x:edi           12345678
ebx             305419896
x:ebx           12345678

nidud

#5
deleted

shankle


Thank you NIdud,
This code is intended for a GoAsm program
So I have modified it slightly.
Data entered is f42400H but could be any hex number
I get a result of "ni rorre"?
dwtoa is a masm32 and GoAsm convert Dword to ascii string module

BufAdd        dd  0
saveeax       dd  0  ; temp save
HexInputData  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

#7
deleted

jj2007

Quote from: nidud on May 02, 2016, 03:53:58 AMUp to 16 byte

Pretty good :t

Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)

10268   cycles for 100 * xtol
8641    cycles for 100 * xtol2
28680   cycles for 100 * MovVal
30435   cycles for 100 * a2uq
7903    cycles for 100 * Masm32 hval

10242   cycles for 100 * xtol
8586    cycles for 100 * xtol2
28742   cycles for 100 * MovVal
30452   cycles for 100 * a2uq
7902    cycles for 100 * Masm32 hval

10284   cycles for 100 * xtol
8640    cycles for 100 * xtol2
28934   cycles for 100 * MovVal
30436   cycles for 100 * a2uq
7903    cycles for 100 * Masm32 hval

10272   cycles for 100 * xtol
8624    cycles for 100 * xtol2
28764   cycles for 100 * MovVal
30439   cycles for 100 * a2uq
7897    cycles for 100 * Masm32 hval

10275   cycles for 100 * xtol
8991    cycles for 100 * xtol2
28663   cycles for 100 * MovVal
30536   cycles for 100 * a2uq
7902    cycles for 100 * Masm32 hval

74      bytes for xtol
62      bytes for xtol2
23      bytes for MovVal
34      bytes for a2uq
12      bytes for Masm32 hval

00000000 00000000 12AB34CD 56EF7801     = eax xtol
00000000 00000000 12AB34CD 56EF7801     = eax xtol2
00000000 00000000 12AB34CD 56EF7801     = eax MovVal
00000000 00000000 0000000C 00000000     = eax a2uq
00000000 00000000 00000000 12AB34CD     = eax Masm32 hval


Couldn't convince a2uq to produce meaningful results, and hval is limited to a DWORD 8)

habran

Here is another way to do that:

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:

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
    ret
hex2num ENDP
Cod-Father

jj2007

Added to testbed:
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)
Assembled with HJWasm
10179   cycles for 100 * xtol
8317    cycles for 100 * xtol2
23974   cycles for 100 * MovVal
88369   cycles for 100 * a2uqJ
7893    cycles for 100 * Masm32 hval (dword)
4608    cycles for 100 * hex2num (Habran, dword)

10183   cycles for 100 * xtol
8591    cycles for 100 * xtol2
23861   cycles for 100 * MovVal
88544   cycles for 100 * a2uqJ
7856    cycles for 100 * Masm32 hval (dword)
4604    cycles for 100 * hex2num (Habran, dword)

10179   cycles for 100 * xtol
8663    cycles for 100 * xtol2
23860   cycles for 100 * MovVal
88536   cycles for 100 * a2uqJ
7890    cycles for 100 * Masm32 hval (dword)
4613    cycles for 100 * hex2num (Habran, dword)

74      bytes for xtol
62      bytes for xtol2
23      bytes for MovVal
34      bytes for a2uqJ
12      bytes for Masm32 hval (dword)
70      bytes for hex2num (Habran, dword)

00000000 00000000 12AB34CD 56EF7801     = eax xtol
00000000 00000000 12AB34CD 56EF7801     = eax xtol2
00000000 00000000 12AB34CD 56EF7801     = eax MovVal
00000000 00000000 12AB34CD 56EF7801     = eax a2uqJ
00000000 00000000 00000000 12AB34CD     = eax Masm32 hval (dword)
00000000 00000000 00000003 12AB34CD     = eax hex2num (Habran, dword)

habran

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:
Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz (SSE4)
Assembled with HJWasm
6585    cycles for 100 * xtol
5837    cycles for 100 * xtol2
16475   cycles for 100 * MovVal
63942   cycles for 100 * a2uqJ
5376    cycles for 100 * Masm32 hval (dword)
3120    cycles for 100 * hex2num (Habran, dword)

6508    cycles for 100 * xtol
5856    cycles for 100 * xtol2
16326   cycles for 100 * MovVal
63416   cycles for 100 * a2uqJ
5320    cycles for 100 * Masm32 hval (dword)
3111    cycles for 100 * hex2num (Habran, dword)

6518    cycles for 100 * xtol
5913    cycles for 100 * xtol2
16261   cycles for 100 * MovVal
63464   cycles for 100 * a2uqJ
5330    cycles for 100 * Masm32 hval (dword)
3140    cycles for 100 * hex2num (Habran, dword)

74      bytes for xtol
62      bytes for xtol2
23      bytes for MovVal
34      bytes for a2uqJ
12      bytes for Masm32 hval (dword)
70      bytes for hex2num (Habran, dword)

00000000 00000000 12AB34CD 56EF7801     = eax xtol
00000000 00000000 12AB34CD 56EF7801     = eax xtol2
00000000 00000000 12AB34CD 56EF7801     = eax MovVal
00000000 00000000 12AB34CD 56EF7801     = eax a2uqJ
00000000 00000000 00000000 12AB34CD     = eax Masm32 hval (dword)
00000000 00000000 00000003 12AB34CD     = eax hex2num (Habran, dword)

--- ok ---
Cod-Father

nidud

#12
deleted

shankle

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

Hi shankle,
Why do you think that you must use GoAsm ;)
I make my choices by using common sense 8)
Cod-Father