Author Topic: hexidecimal input  (Read 10098 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: 577
    • Github
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

Github Twitter Mastodon Gitbook

shankle

  • Member
  • ****
  • Posts: 868
Re: hexidecimal input
« Reply #3 on: May 02, 2016, 12:44:31 AM »
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

  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
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:

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:
Code: [Select]
Results
esi             305419896
x:esi           12345678
edi             305419896
x:edi           12345678
ebx             305419896
x:ebx           12345678

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
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 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

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
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: 13937
  • Assembly is fun ;-)
    • MasmBasic
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 * 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

  • Member
  • *****
  • Posts: 1228
    • uasm
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
    ret
hex2num ENDP
Cod-Father

jj2007

  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: hexidecimal input
« Reply #10 on: May 02, 2016, 04:39:06 PM »
Added to testbed:
Code: [Select]
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

  • Member
  • *****
  • Posts: 1228
    • uasm
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 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

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
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
    • uasm
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