flat assembler
Message board for the users of flat assembler.

Index > Windows > [SOLVED] - Convert integer to hexadecimal

Author
Thread Post new topic Reply to topic
ctl3d32



Joined: 30 Dec 2009
Posts: 206
Location: Brazil
ctl3d32 19 Jun 2011, 13:43
Hi folks!

I'm trying to convert a string that represents a decimal number, but 'atof' returns wrong value. See code below:
Code:
format PE GUI 4.0
entry start

include 'win32a.inc'

section '.text' code readable executable

  start:

        invoke  GetModuleHandle,0
        mov     [h_process],eax
        cinvoke wsprintf,string1,fmt1,dword[number+4],dword[number]
        cinvoke atof,strnum
        fistp   qword[integer]
        cinvoke wsprintf,string2,fmt1,dword[integer+4],dword[integer]
        cinvoke wsprintf,caption,fmt2,string1,string2
        invoke  MessageBox,HWND_DESKTOP,caption,title,MB_OK
  exit:

        invoke  ExitProcess,0

section '.bss' data readable writable

  h_process   dd ? ;handles to the executable process.
  string1     rb 40h
  string2     rb 40h
  caption     rb 40h
  integer     dq ?

section '.data' data readable writable

  title       db 'Convert Decimal to Hexadecimal',0
  fmt1        db '%X%X',0
  fmt2        db 'number = 328839641925923849',10,13,\
                 'wsprintf: %s',10,13,10,13,\
                 'number = "328839641925923849"',10,13,\
                 'atof(msvcrt.dll): %s',0
  number      dq  328839641925923849
  strnum      db '328839641925923849',0

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL',\
          msvcrt,'MSVCRT.DLL'

  include 'API\KERNEL32.INC'
  include 'API\USER32.INC'

  import  msvcrt,\
          atof,'atof'
    

Thanks,
ctl3d32


Last edited by ctl3d32 on 19 Jun 2011, 22:30; edited 1 time in total
Post 19 Jun 2011, 13:43
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 19 Jun 2011, 15:18
it is atoi or atof...atof sounds like float ? Rolling Eyes
Post 19 Jun 2011, 15:18
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 206
Location: Brazil
ctl3d32 19 Jun 2011, 15:28
I think 'atoi' is for dword.
Post 19 Jun 2011, 15:28
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 19 Jun 2011, 15:37
ctl3d32 wrote:
I think 'atoi' is for dword.


Ooh.. My mistake, forgive me.

Somehow I was thinking ascii to integer but then after I re-read the title it says Convert integer to hexadecimal

Sorry. Laughing


Last edited by typedef on 19 Jun 2011, 19:48; edited 1 time in total
Post 19 Jun 2011, 15:37
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 19 Jun 2011, 16:38
I've just checked and the number is almost correct. The problem is that the input number is 59 bits long and your 64-bit double has a mantissa of only 52 bits long.
Post 19 Jun 2011, 16:38
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 206
Location: Brazil
ctl3d32 19 Jun 2011, 17:13
So, how can i do it without using SSE registers?
Post 19 Jun 2011, 17:13
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 19 Jun 2011, 20:38
Code:
format PE GUI 4.0
entry start

include 'win32a.inc'

section '.text' code readable executable

  start:

        invoke  GetModuleHandle,0
        mov     [h_process],eax
        cinvoke wsprintf,string1,fmt1,dword[number+4],dword[number]

        ccall   atoll,strnum
        cinvoke wsprintf,string2,fmt1,edx,eax

        cinvoke wsprintf,caption,fmt2,string1,string2
        invoke  MessageBox,HWND_DESKTOP,caption,title,MB_OK
  exit:

        invoke  ExitProcess,0

; WARNING: Works on unsigned integers only (fixable, but I left it as an exercise)
proc atoll c uses esi ebx edi, pStr

        mov     esi, [pStr]

.skip:
        lodsb
        test    al, al
        jz      .error

        cmp     al, ' '
        jbe     .skip

        sub     al, '0'
        cmp     al, 10
        jae     .error

        movzx   eax, al
        xor     ebx, ebx
        xor     ecx, ecx
        mov     edi, 10
        jmp     .grabDigit

.addDigit:
        imul    ecx, edi
        jc      .error

        mul     edi
        add     ecx, edx
        jc      .error

        add     eax, ebx
        adc     ecx, 0
        jc      .error

.grabDigit:
        mov     bl, [esi]
        inc     esi

        sub     bl, '0'
        cmp     bl, 10
        jb      .addDigit

.success:
        mov     edx, ecx

.return:
        ret

.error:
        xor     eax, eax
        xor     edx, edx
        jmp     .return
endp
; I know the error signaling is stupid, I'm just following what I've seen at http://www.cppreference.com/wiki/string/c/atoi

section '.bss' data readable writable

  h_process   dd ? ;handles to the executable process.
  string1     rb 40h
  string2     rb 40h
  caption     rb 40h

section '.data' data readable writable

  title       db 'Convert Decimal to Hexadecimal',0
  fmt1        db '%X%X',0
  fmt2        db 'number = 328839641925923849',10,13,\
                 'wsprintf: %s',10,13,10,13,\
                 'number = "328839641925923849"',10,13,\
                 'atof(msvcrt.dll): %s',0
  number      dq  328839641925923849
  strnum      db '328839641925923849',0

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL'

  include 'API\KERNEL32.INC'
  include 'API\USER32.INC'    
But you should write your own 64-bit hex function since the way you are doing it now will always insert a zero at the beginning for numbers below 2^32.

Please review my code carefully, I may doing something wrong and it took my a while to get it (apparently) right. I think I'm getting so old...
Post 19 Jun 2011, 20:38
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 206
Location: Brazil
ctl3d32 19 Jun 2011, 21:21
Thanks a lot for the code. It works perfectly.
I'm going to study it right now!

Cheers,
ctl3d32
Post 19 Jun 2011, 21:21
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.