Page 1 of 1

Getting a return variable.. (mixing C and asm)

Posted: Tue Mar 31, 2009 5:51 pm
by earlz
Ok, maybe this should go in general programming, but it is for my kernel.. lol

ok, how exactly do you get the return value of a C function from assembly?

yes.. I know it seems very noobish.. but I can't seem to find the answer.. I've only seen stuff about passing parameters, and nothing more... and I tried experimenting with pushing and popping different things and I can't seem to get it..

Re: Getting a return variable.. (mixing C and asm)

Posted: Tue Mar 31, 2009 7:07 pm
by JohnnyTheDon
Usually RAX/EAX/AX/AL.

Re: Getting a return variable.. (mixing C and asm)

Posted: Tue Mar 31, 2009 7:09 pm
by earlz
JohnnyTheDon wrote:Usually RAX/EAX/AX/AL.
Thanks! that's the trick.. I always thought C intended to be more portable than that though...

Re: Getting a return variable.. (mixing C and asm)

Posted: Tue Mar 31, 2009 7:15 pm
by JohnnyTheDon
Well the calling convention will always depend on the platform. For example, the C calling convention on x86-64 uses registers to hold parameters.

Re: Getting a return variable.. (mixing C and asm)

Posted: Tue Mar 31, 2009 7:23 pm
by quok
earlz wrote:Ok, maybe this should go in general programming, but it is for my kernel.. lol
Moved to General Programming.

Re: Getting a return variable.. (mixing C and asm)

Posted: Tue Mar 31, 2009 7:26 pm
by pcmattman
I always thought C intended to be more portable than that though...
It is - the C compiler hides the machine-specific details from you.

Re: Getting a return variable.. (mixing C and asm)

Posted: Tue Mar 31, 2009 7:57 pm
by earlz
pcmattman wrote:
I always thought C intended to be more portable than that though...
It is - the C compiler hides the machine-specific details from you.
Well, I always thought it an agreed upon unspoken standard that parameters are passed on the stack.. though I suppose a compiler could do just about anything it wants as long as it compiles C code and it works

Re: Getting a return variable.. (mixing C and asm)

Posted: Tue Mar 31, 2009 8:00 pm
by pcmattman
Well, I always thought it an agreed upon unspoken standard that parameters are passed on the stack
That's not so on some RISC architectures.

That's the beauty of C - it still works on practically every architecture because the compiler actually creates the assembly code for the target architecture for you, following the relevant ABIs and other standards.

Re: Getting a return variable.. (mixing C and asm)

Posted: Wed Apr 01, 2009 2:19 am
by AJ
Hi,

You may be interested in Googling the System V ABI, particularly if you want to mix a lot of C/C++/ASM.

Cheers,
Adam

Re: Getting a return variable.. (mixing C and asm)

Posted: Wed Apr 01, 2009 8:48 am
by Troy Martin
(inappropriate remark removed - Combuster)