flat assembler
Message board for the users of flat assembler.

Index > Main > C functions in asm

Author
Thread Post new topic Reply to topic
Sarge



Joined: 04 Nov 2005
Posts: 14
Location: Australia
Sarge 04 Nov 2005, 11:43
Hi,

I am very new to asm and to fasm, i have some c functions that i would like to use in asm, is this possible and if so could you please show me.

Thanks,
Sarge
Post 04 Nov 2005, 11:43
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 04 Nov 2005, 13:31
Well, im assuming your using gcc, either on *nix, or on windows (mingw32).

Firstly, you will want to build the asm source as an object (eg: Format ELF for linux, i think Format MSCOFF for windows.

Then if you want to call a function called Test, and pass it the parameters: param1, param2, and param3, then you would do:

Code:
pushd param3 param2 param1
call Test    


And when you go to assemble the code, you simply use fasm to assemble it into an object file (usually .o), then use a linker (like ld, part of the gcc package), to make it into an executable and link it with the the compiled C code you have.

ld c_code_binary.o asm_code_binary.o -o yourprogram

This should work,

Good luck,
gunblade
Post 04 Nov 2005, 13:31
View user's profile Send private message Reply with quote
BXM



Joined: 03 Nov 2005
Posts: 26
Location: mars
BXM 04 Nov 2005, 17:02
- In the .idata section, declare the name of the API(s).
- Then declare what functions you want to import
- To call the function, use "invoke" or "stdcall".

You may find code calling functions from the Win32 API in /FASM/EXAMPLES .
The first example imports the "ExitProcess" function from Kernel32.dll,
and "mciSendString" from winmm.dll.
/FASM/EXAMPLES/DLL is also a good and simple example.
Post 04 Nov 2005, 17:02
View user's profile Send private message Reply with quote
Sarge



Joined: 04 Nov 2005
Posts: 14
Location: Australia
Sarge 05 Nov 2005, 21:14
Thanks you very much gunblade and BXM, this is exactly what i need. I really appreciate it.

Thanks,
Sarge
Post 05 Nov 2005, 21:14
View user's profile Send private message Reply with quote
Chewy509



Joined: 19 Jun 2003
Posts: 297
Location: Bris-vegas, Australia
Chewy509 08 Nov 2005, 02:26
...and to follow on from what the others have said, if you're adventurous and have the right CPU/OS, then the following pdf gives the ABI for x86_64 (aka AMD64) which is what GCC uses on AMD64 based linux systems...

http://www.x86-64.org/documentation/abi-0.96.pdf

PS. For those that don't want to read the full ABI, the calling convention has *changed* for x86_64 based systems. A mix of registers and the stack is now used to pass values to a procedure.
Post 08 Nov 2005, 02:26
View user's profile Send private message Visit poster's website Reply with quote
Sarge



Joined: 04 Nov 2005
Posts: 14
Location: Australia
Sarge 08 Nov 2005, 20:04
I am still having trouble getting this to work, the exe is showing a error message, these are the steps im doing.

1. build .o out of the .c files with gcc
2. use fasm to assemble the asm file to .o
3. use ld [ all the .o that were built with gcc and the asm .o ] -o myapp.exe
4. run exe, which gives me a error

any help please.
Post 08 Nov 2005, 20:04
View user's profile Send private message Reply with quote
HyperVista



Joined: 18 Apr 2005
Posts: 691
Location: Virginia, USA
HyperVista 08 Nov 2005, 22:27
Hi Sarge,

What error message are you seeing?
Post 08 Nov 2005, 22:27
View user's profile Send private message Visit poster's website Reply with quote
Sarge



Joined: 04 Nov 2005
Posts: 14
Location: Australia
Sarge 08 Nov 2005, 22:46
Hi,

Well i have created a startup.o file because fasm is saying "warning: cannot find entry symbol _mainCRTStartup; defaulting to 00401000" and when i link the startup.o with ld and run the exe i get,

"This application has failed to start because [ENC].dll was not found. Re-installing the application may fix the problem"

[ENC] = all encrpted text which i cannot make out to find the right dll

This is my startup.c file that is built to .o,
Code:
int main(int argc, const char* argv[])
{

}
    


and this is the asm file that is built as .o

Code:
format  MS COFF
extrn   _PMACos
public __pb_main
section "code" code

__pb_main:
  push  ebp
  mov   ebp,esp
  je    _15
  mov   eax,0
  pop   ebp
  ret

_15:
  call  _PMACos
    


Thanks,
Sarge
Post 08 Nov 2005, 22:46
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 09 Nov 2005, 10:29
hehe, nasty mistake there.. gcc/ld find main() in the C file, and use it as the main function, yet its empty, so itll crash.

theres two ways to get round this. Either:

remove that startup.c file, its not needed, and in your asm code, put:
Code:
public _main
_main:    

at the beginning of the code.
ld should be able to pick that up.

I would be tempted to say that you should use gcc to link though.. as ld doesnt automatically link with the standard libraries, and therefore you can have some errors which says something like: file.exe: not an executable.

If you really want the main to be in the C file, you can keep it as it is, but in the main() function, you put a line saying:
Code:
__pb_main();
    


but i would recommend the first method as theres no need to have a C file which just calls the asm code, its easily possible to just add the _main: to the asm file (although the label might be called just main: or __main:, theres a whole strange thing about underscoring labels, its different for every compiler/linker, and sometimes OS. In linux anyway, i dont have to put any underscores, in a program, i just put:
Code:
public main
main:    

and gcc picks it up fine as the startup label.

Good luck,
gunblade
Post 09 Nov 2005, 10:29
View user's profile Send private message Reply with quote
Sarge



Joined: 04 Nov 2005
Posts: 14
Location: Australia
Sarge 09 Nov 2005, 13:06
Hi gunblade,

I still seem to be getting "ld: warning: cannot find entry symbol _mainCRTStartup; defaulting to 00401000" for some reason, i have taken out my startup.c and added what you said to the asm file but i just keep getting this error.

btw, can you please show me how to use gcc to compile a exe. if possible.

Thanks,
Sarge
Post 09 Nov 2005, 13:06
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 09 Nov 2005, 13:31
aah, forgot that in windows, they have a different starting label.
so instead of the
Code:
public main
main:    


do:

Code:
public _mainCRTStartup
_mainCRTStartup:    


and ld should be able to pick that up fine.

as for the process, youve got it, just use gcc to compile the C files into .o,
use fasm to assemble into .o, then use ld to link them together.

if you want the entry label to be called something else, you should be able to do:
Code:
ld -e entrylabelname .....    

which will allow you to choose the name of the entry label.
Post 09 Nov 2005, 13:31
View user's profile Send private message Reply with quote
Sarge



Joined: 04 Nov 2005
Posts: 14
Location: Australia
Sarge 09 Nov 2005, 13:42
Hi, gunblade

Thanks alot mate, this really helps me out alot. Now i just have to figure out why it opens a console window and crashes.

Thanks,
Sarge
Post 09 Nov 2005, 13:42
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.