flat assembler
Message board for the users of flat assembler.

Index > Main > printf ( "%f : %f\n", double_var1, double_var2)

Author
Thread Post new topic Reply to topic
paule32



Joined: 05 Oct 2009
Posts: 2
paule32
Hello,

why crash my program, when i try to printf string to console?
Or in other words, how can I format a string to output it in nice style?
Code:

format PE console
entry start

include 'i:\Projekte\kmice\asm\include\win32a.inc'

section '.code' code readable executable
start:
        push ebp
    mov ebp, esp

        fld qword [LC0]
        fld qword [LC1]
      faddp
       fstp qword [_var1]
  fld qword [LC2]
     fld qword [_var1]
   faddp
       fstp qword [_var2]

;        ccall [printf], user_str
        cinvoke printf, user_str, [_var1]

        ccall [printf], hello_msg
   ccall [getchar]
     stdcall [ExitProcess],0

 leave
       ret

section '.data' data readable writeable
hello_msg db 'Hello, world!',0
LC0 dq 2.1000
LC1 dq 3.0500
LC2 dq 2.5000
_var1 dq 0.0
_var2 dq 0.0
user_str db "--> %f, %5f",10,0


section '.idata' import data readable

library kernel,'kernel32.dll',\
        msvcrt,'msvcrt.dll',\
        user,'USER32.DLL',\
        kbase,'kbase.dll'

import kernel,\
       ExitProcess,'ExitProcess'

import msvcrt,\
       printf,'printf',\
       getchar,'_fgetchar'

import user,\
        wsprintf,'wsprintfA'
    


Thanks for helping
Post 05 Oct 2009, 19:55
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Hi paule32, welcome on board.
This works on my system.

Code:
format PE console
entry start

include 'include\win32a.inc'

section '.code' code readable executable
start:
        ;push ebp
        ;mov ebp, esp

        ;fld qword [LC0]
        ;fld qword [LC1]
        ;faddp
        ;fstp qword [_var1]
        ;fld qword [LC2]
        ;fld qword [_var1]
        ;faddp
        ;fstp qword [_var2]
        
        fld qword [LC0]
        fadd qword [LC1]
        fadd qword [LC2]
        fstp qword [_var1] 

        ;ccall [printf], user_str
        cinvoke printf, user_str, hello_msg, dword [_var1], dword [_var1+4]

        invoke ExitProcess, 0
        ;ccall [printf], hello_msg
        ;ccall [getchar]
        ;stdcall [ExitProcess],0

        ;leave
        ;ret

section '.data' data readable writeable
hello_msg db 'Hello, world!',0
LC0 dq 2.1000
LC1 dq 3.0500
LC2 dq 2.5000
_var1 dq 0.0
_var2 dq 0.0
user_str db "%s --> %.5f",13,10,0


section '.idata' import data readable

library kernel,'kernel32.dll',\
        msvcrt,'msvcrt.dll',\
        user,'USER32.DLL',\
        kbase,'kbase.dll'

import kernel,\
       ExitProcess,'ExitProcess'

import msvcrt,\
       printf,'printf',\
       getchar,'_fgetchar'

import user,\
        wsprintf,'wsprintfA'

    
Post 05 Oct 2009, 20:26
View user's profile Send private message Reply with quote
paule32



Joined: 05 Oct 2009
Posts: 2
paule32
Hello Picnic!

Thanks for the fast reply and working application.
Post 05 Oct 2009, 21:28
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Now it's my turn to ask.
I need to convert a buffer to a float, i tried msvcrt library fucntion sscanf with no success.
Test program output is 0.0, it should be 12345.67
Code:
    format PE CONSOLE 4.0
        include 'win32axp.inc'

.data
        fmt db '%f',0
        buffer db '12345.67',0
        f dq 0.0

.code
entry $
        push f
        push fmt
        push buffer
        call [sscanf]
        add esp, 12

        push DWORD [f+4]
        push DWORD [f]
        push fmt
        call [printf]
        add esp, 12
        ret

section '.idata' import data readable writeable

        library msvcrt, 'msvcrt.dll'

        import msvcrt,\
            printf, 'printf',\
            sscanf, 'sscanf'
    
Post 30 Jan 2010, 12:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17248
Location: In your JS exploiting you and your system
revolution
It works for me if I convert the value to a double.
Code:
        format PE CONSOLE 4.0
        include 'win32axp.inc'

.data
        fmt db '%f',0
        buffer db '12345.67',0
        f dq 0.0

.code
entry $
        push f
        push fmt
        push buffer
        call [sscanf]
        add esp, 12

    fld dword[f]
        fst qword[f]

        push DWORD [f+4]
        push DWORD [f]
        push fmt
        call [printf]
        add esp, 12
        ret

section '.idata' import data readable writeable

        library msvcrt, 'msvcrt.dll'

        import msvcrt,\
            printf, 'printf',\
            sscanf, 'sscanf'    
Post 30 Jan 2010, 13:32
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Picnic,

For scanf "%f" format spec corresponds to float * argument. double * requires "%lf".
Post 30 Jan 2010, 13:32
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6034
Location: Poland
MHajduk
Hi Picnic!

Try this:
Code:
format PE CONSOLE 4.0
include 'win32axp.inc'

.data 
       InputFmt        db '%lf', 0
       OutputFmt       db 'Scanned number = %f', 0
       Buffer          db '12345.67', 0
  f               dq 0.0
      
.code
entry $
        cinvoke sscanf, Buffer, InputFmt, f
 
    cinvoke printf, OutputFmt, dword [f], dword [f+4]
   
    cinvoke getchar

 ret 

section '.idata' import data readable writeable

   library msvcrt, 'msvcrt.dll'

  import msvcrt,\
            getchar, 'getchar',\
             printf, 'printf',\
               sscanf, 'sscanf' 
    
Very Happy
Post 30 Jan 2010, 13:50
View user's profile Send private message Visit poster's website Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Thanks for the feedback guys Very Happy
Post 30 Jan 2010, 14:17
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-2020, Tomasz Grysztar.

Powered by rwasa.