Page 1 of 1
How do I execute c with assembly
Posted: Fri Aug 01, 2008 1:25 am
by oblivian516
How do execute c with assembly?
I have searched all over and I havent found anything

can someone please help me?
EDIT: why do I want to do this? because I want to start an os, I would just like to display a simple text message on the screen thro C, so then I can continue developing.
Re: How do I execute c with assembly
Posted: Fri Aug 01, 2008 1:38 am
by AJ
Hi,
There are 2 things you could mean here - calling a C function from your assembly stub, or loading a binary which you have written in C and executing that. From the context of your question, I guess you have created an ASM boot loader and want to run a kernel-type binary.
Let's assume that you have loaded the C executable (bit of a misnomer - there's nothing special about the executable just because it was written in C) in to memory. Before you run your C code, there are a few things to do. Firstly, your asm code needs to set up a stack to allow C functions to call each other and pass parameters. Next, you need to push any c parameters to the stack in a right-to-left order. Now, simply call the C function as if it was written in asm.
Some compilers append a leading underscore to the C function name but you can avoid this with compiler flags / a cross-compiler.
For far more information than I have given you, Google for the System V ABI.
Cheers,
Adam
Re: How do I execute c with assembly
Posted: Fri Aug 01, 2008 5:48 am
by oblivian516
Now, simply call the C function as if it was written in asm.
And how might I do that?
Re: How do I execute c with assembly
Posted: Fri Aug 01, 2008 5:56 am
by thepowersgang
or the like.
If you have dynamically loaded it (from disk etc.) then replace _main with eax or another register that contains the entrypoint (first instruction)