how does c function knows

This is a discussion on how does c function knows within the C Programming forums, part of the General Programming Boards category; how does c function knows which function has made a call to it.. Is there a way?? just curious to ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    Question how does c function knows

    how does c function knows which function has made a call to it..

    Is there a way??

    just curious to know..

    thanks

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    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.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21