Author Topic: hexidecimal input  (Read 9453 times)

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: hexidecimal input
« Reply #15 on: May 02, 2016, 10:40:21 PM »
deleted
« Last Edit: February 25, 2022, 11:38:04 AM by nidud »

jj2007

  • Member
  • *****
  • Posts: 12982
  • Assembler is fun ;-)
    • MasmBasic
Re: hexidecimal input
« Reply #16 on: May 02, 2016, 10:43:54 PM »
I did not expect such advantage in the speed :shock:

The crt_sscanf is surprisingly slow ::)

Code: [Select]
Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz (SSE4)
Assembled with HJWasm
5837    cycles for 100 * xtol2   QWORD size, accepts 123aBcD with or without trailing h
16475   cycles for 100 * MovVal  QWORD size, accepts almost everything*) (decimal, 1010b, 0xAb12, $abc, 0abch)
63942   cycles for 100 * a2uqJ   QWORD size, CRT, accepts only hex with the given format string
5376    cycles for 100 * Masm32 hval (dword)
3120    cycles for 100 * hex2num (Habran, dword)

*)
include \masm32\MasmBasic\MasmBasic.inc
  Init
  Print Str$("\n%i", Val("2748"))
  Print Str$("\n%i", Val("101010111100b"))
  Print Str$("\n%i", Val("101010111100y"))
  Print Str$("\n%i", Val("0xaBc"))
  Print Str$("\n%i", Val("$aBc"))
  Print Str$("\n%i (bad format)", Val("aBc"))
  Print Str$("\n%i", Val("aBch"))
  Print Str$("\n%i", Val("aBcH"))
EndOfCode

habran

  • Member
  • *****
  • Posts: 1228
    • uasm
Re: hexidecimal input
« Reply #17 on: May 03, 2016, 06:18:16 AM »
Hi shankle,
Here is 64 bit in a plain asm:
Quote
hex2dec   PROC  src:QWORD
   movsx   eax, BYTE PTR [rcx]
   xor   edx, edx
   mov   r9, rcx
   test   eax, eax
   je   LN16
LL2:
   shl   edx, 4
   lea   r8d, DWORD PTR [rax-48]
   cmp   r8d, 9
   ja   LN6
   mov   eax, r8d
   jmp   LN8
LN6:
   or   eax, 32
   lea   ecx, DWORD PTR [rax-97]
   cmp   ecx, 5
   ja   LN8
   add   eax, -87
LN8:
   inc   r9
   add   edx, eax
   movsx   eax, BYTE PTR [r9]
   test   eax, eax
   jne   LL2
LN16:
   mov   eax, edx
   ret   
hex2dec   ENDP
it is created from this C source:
Quote
uint_32  hex2dec(const char *src)
{
  uint_32 a;
  uint_32 b = 0;
  for ( ;; )
  {
    a = *src;
    if (!a) break;
    b = (b << 4);
    if (a >= '0' && a <= '9') a -= '0';
    else {
      a |= 0x20;
      if (a >= 'a' && a <= 'f') a -= 'a' - 10;
    }
    b = b + a;
    src++;
  }
  return (b);
}
Cod-Father

shankle

  • Member
  • ****
  • Posts: 868
Re: hexidecimal input
« Reply #18 on: May 03, 2016, 04:40:46 PM »
Thanks everyone for your help.  I think I have solved my problem.
It might not be the best but its works.