How do I execute c with assembly

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
oblivian516
Posts: 2
Joined: Thu Jul 31, 2008 11:37 pm

How do I execute c with assembly

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: How do I execute c with assembly

Post 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
oblivian516
Posts: 2
Joined: Thu Jul 31, 2008 11:37 pm

Re: How do I execute c with assembly

Post by oblivian516 »

Now, simply call the C function as if it was written in asm.
And how might I do that?
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: How do I execute c with assembly

Post by thepowersgang »

Code: Select all

call _main
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)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Post Reply