flat assembler
Message board for the users of flat assembler.

Index > Main > Importing C functions to FASM

Author
Thread Post new topic Reply to topic
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 06 Jun 2005, 19:43
Anyone give me a hand here? What am I missing or doing wrong? Thanks

Code:
format PE GUI 4.0
entry BeginCode

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

BeginCode:
          push ebp
          mov ebp, esp
          push hellostring
          call [printf]
          pop ebp
          ret 4
          push ebp
          mov ebp, esp
          call [getch]
          pop ebp
          push 0
          call [ExitProcess]



section '.data' data readable writeable

hellostring             db "This was printed out with the printf() function!",0

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'MSVCRT.DLL'

  import kernel,\
         ExitProcess,'ExitProcess'
    
Post 06 Jun 2005, 19:43
View user's profile Send private message Reply with quote
flaith



Joined: 07 Feb 2005
Posts: 122
Location: $300:20 58 FC 60 N 300G => Vietnam
flaith 06 Jun 2005, 20:15
first you must define
Code:
  extrn printf                   ; because of c
    

and you must compile it as an objet
Code:
format MS COFF
    

and link with gcc !!!
hmmm May be !

_________________
Je suis sur de 'rien', mais je ne suis pas sur du 'tout'.
Post 06 Jun 2005, 20:15
View user's profile Send private message Visit poster's website Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 07 Jun 2005, 04:32
Post 07 Jun 2005, 04:32
View user's profile Send private message Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 07 Jun 2005, 12:19
Thanks coconut, but I'm still having problems getting this to run. It crashes at run time. I'm I still doing something wrong?

Code:
format PE CONSOLE 4.0
entry BeginCode

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

BeginCode:
          cinvoke printf, hellostring
          cinvoke getch
          push 0
          call [ExitProcess]



section '.data' data readable writeable

hellostring             db "This was printed out with the printf() function!",0

section '.idata' data readable writable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'msvcrt.dll'

  import kernel,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf',\
         getch, 'getch'
    
Post 07 Jun 2005, 12:19
View user's profile Send private message Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 07 Jun 2005, 13:25
You are missing first parameter to printf:
Code:
          cinvoke printf, format, hellostring 
(...)
format db "%s",0
hellostring             db "This was printed out with the printf() function!",0
    
Post 07 Jun 2005, 13:25
View user's profile Send private message Yahoo Messenger Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 07 Jun 2005, 13:59
pelaillo wrote:
You are missing first parameter to printf:
Code:
          cinvoke printf, format, hellostring 
(...)
format db "%s",0
hellostring             db "This was printed out with the printf() function!",0
    


I still can't get it to work. By the way, if you don't specify a format type then couldn't you just pass the pointer to the string like you do in C?

Code:
example

printf("My string"); // No argument type string
    


new code
Code:
format PE CONSOLE 4.0
entry BeginCode

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

BeginCode:

          cinvoke printf, type, hellostring
          cinvoke getch

          push 0
          call [ExitProcess]



section '.data' data readable writeable

type                    db "%s",0
hellostring             db "This was printed out with the printf() function!",0

section '.idata' data readable writable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'msvcrt.dll'

  import kernel,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf',\
         getch, 'getch'
    
Post 07 Jun 2005, 13:59
View user's profile Send private message Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 07 Jun 2005, 14:22
Maybe your 'cinvoke' macro is incorrect. Try:
Code:
    push    hellostring
    call    [printf]
    add     esp, 4
    call    [getch]    
Post 07 Jun 2005, 14:22
View user's profile Send private message Visit poster's website Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 07 Jun 2005, 14:35
Reverend wrote:
Maybe your 'cinvoke' macro is incorrect. Try:
Code:
    push    hellostring
    call    [printf]
    add     esp, 4
    call    [getch]    


Thanks man but still no dice. Sad There must be something I am missing. Seems like I read somewhere that you have to underscore the name of the functions but I have tried that and it still didn't work.
Post 07 Jun 2005, 14:35
View user's profile Send private message Reply with quote
flaith



Joined: 07 Feb 2005
Posts: 122
Location: $300:20 58 FC 60 N 300G => Vietnam
flaith 07 Jun 2005, 17:06
Much better now (look at the 'idata' section and 'getch' to '_getch')

Code:
format PE CONSOLE 4.0
entry BeginCode

include '%fasminc%\win32a.inc'

section '.code' executable readable writeable

BeginCode:

          cinvoke printf, type, hellostring
          cinvoke getch

          invoke ExitProcess


section '.data' data readable writeable
type                    db "%s",0
hellostring             db "This was printed out with the printf() function!",0

section '.idata' import data readable writable

  library kernel,'KERNEL32.DLL',\
          msvcrt,'msvcrt.dll'

  import kernel,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         printf,'printf',\
         getch, '_getch'   
    

_________________
Je suis sur de 'rien', mais je ne suis pas sur du 'tout'.
Post 07 Jun 2005, 17:06
View user's profile Send private message Visit poster's website Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 07 Jun 2005, 18:27
flaith wrote:
Much better now (look at the 'idata' section and 'getch' to '_getch


Hmm still crashing. I wouldn't think that it would be like this to call a simple c function.
Post 07 Jun 2005, 18:27
View user's profile Send private message Reply with quote
flaith



Joined: 07 Feb 2005
Posts: 122
Location: $300:20 58 FC 60 N 300G => Vietnam
flaith 07 Jun 2005, 18:42
I don't get it, it run ok for me :

Image

compiled with Fasm 1.61.8

_________________
Je suis sur de 'rien', mais je ne suis pas sur du 'tout'.
Post 07 Jun 2005, 18:42
View user's profile Send private message Visit poster's website Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 07 Jun 2005, 19:22
I don't get it either. I have exactly the same code as you have there and it will compile fine but when you execute it it crashes! Doesn't make a bit of sense. Any suggest as to what it may be?
Post 07 Jun 2005, 19:22
View user's profile Send private message Reply with quote
flaith



Joined: 07 Feb 2005
Posts: 122
Location: $300:20 58 FC 60 N 300G => Vietnam
flaith 07 Jun 2005, 20:40
Ok, and if you try my own exe ???

Download ZIP

_________________
Je suis sur de 'rien', mais je ne suis pas sur du 'tout'.
Post 07 Jun 2005, 20:40
View user's profile Send private message Visit poster's website Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 07 Jun 2005, 21:38
what windows are you running, and msvcrt.dll version? btw looking at msvcrt with dependancy walker (great free program), getch is exported as _getch

http://www.dependencywalker.com/


Last edited by coconut on 07 Jun 2005, 21:42; edited 1 time in total
Post 07 Jun 2005, 21:38
View user's profile Send private message Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007 07 Jun 2005, 21:39
flaith wrote:
Ok, and if you try my own exe ???

Download ZIP


LOL I know what it is now. I forgot to add the 'import' directive to my '.idata' section! Silly error! Laughing
Post 07 Jun 2005, 21:39
View user's profile Send private message Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 07 Jun 2005, 21:40
ouch lol
Post 07 Jun 2005, 21:40
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.