how does c function knows which function has made a call to it..
Is there a way??
just curious to know..
thanks
how does c function knows which function has made a call to it..
Is there a way??
just curious to know..
thanks
You'd need to pass a parameter indicating which function made the call.
Technically, you could unportably examine the stack and the symbol table to see at runtime, but this is evil and stupid.
As an aside, If you want to know the name of the current function at runtime, C99 supports the __func__ identifier.
A C function doesn't know. Prior to executing a CALL in assembly a stack frame is setup using PUSH to push items onto the stack. The actual setup depends on whether you are in protected mode or real mode.
Since most modern computers are in protected mode using all 32-bit code, I will show you the stack frame for entry to a function.
EBP - Previous value of EBP
EBP +4 - Address of caller
EBP +8 - start of parameter list
I'm fairly sure this is correct - I haven't done any assembly or stack frame coding in quite some time so I might be a bit rusty. I'm sure Fordy could weigh in on this and correct me if I'm wrong.
Function names are simply a compiler-side convenience. Most assembly listings from compiled C source have very few readable function names for various reasons. It's not the name of the function that matters, rather it's the starting address of the function code that matters.
You would have to dive into assembly to understand what a function call is composed of. I would recommend the IA-32 and IA-64 technical reference manuals from Intel as a starting point. They have the entire pseudo-function code for every assembly opcode available.
For functions that must return a value all integral values are stored in EAX and all floating point values are stored in ST(0).
The compiler knows that when you specify an integral return type such as int, long, etc., it looks in EAX to get the return value. In assembly you would have to manually code this to create functions that can return a value. For all floating point variables the compiler looks in ST(0) or the top of the floating point stack to retrieve the value. Pointers can be returned as well and stored in EAX since pointers are really just more values - but they are just interpreted differently - they are not treated as values but as offsets into memory or complete memory address locations.
If thats confusing, look at some assembly language manuals. So the answer to your question is that the actual CPU sets up the stack frame for C, BASIC, Java, C#, and every other language running on the system. In the end it's all assembly language.
Last edited by VirtualAce; 10-06-2005 at 12:25 PM.