function call and return mechanism in c

Hi friends,

i want to know how function call & return works in c


  • : Hi friends,
    : i want to know how function call & return works in c

    This is pretty low level (Assembly) stuff.

    What C does (following it's standard calling convention):
    1. It pushes the parameters from last to first on the stack
    2. It pushes the current code segment and the instruction pointer to
    the next instruction (the return location) on the stack
    3. It uses a call-instruction, that tells the CPU to jump to
    the specified code location (where the function code in memory starts)
    4. The function does it's magic
    5. Last, the function pops off the return segment and address and
    jumps to that location (which is the next instruction after the call
    in point 2)
    The return value of the function is stored in eax (a processor register)
    6. The calling program pop's the parameters off the stack (cleans up)
    and continues
    The first point is probably already confusing.

    A stack is a location in (virtual) memory where the process can store it's local variables and data. Normally, variables declared in C functions are put on the stack ("int c; char* s;" etc).
    The stack is a 'buffer' that works on the Last In, First Out (LIFO) principle. Basically, you 'push' something on it (meaning you put something in the buffer). Then when you 'pop' (get something from the buffer) you take the last 'pushed' item (the item that was stored last).
    So with pushing and popping (storing and retrieving) you always refer to the last item. So to get to the first item, all other items need to be pop'd off first.

    The code segment and instruction pointer hold the address of the current instruction in memory (pretty easy, right).

    Then when the function returns, it stores it's value into EAX, which is a processor register: basically, these are the memory locations that are inside your CPU and that are used for all calculations. When calculating something, often you first need to load data from memory into one of these CPU registers before you can do anything with them.
    EAX is 32 bits large, hence the fact that all return types for C functions must be of a basic type, such as 'long', 'int', or a pointer, but not a structure or (in C++) class.
    Best Regards,

    The way I see it... Well, it's all pretty blurry
  • To expand on the previous poster (Who explained it very well),
    here is an example.

    Lets say, we are calling the printf() function:
    // call printf
    printf ("Hello, World!");

    The compilier converts this into assembly code, simular to this:

    push str ; push the address of our string
    call _printf ; call printf. push next address on the stack, so we
    ; have a place to retutn to
    pop eax ; cleanup the stack

    ; do something else--make sure we dont fall into _printf


    mov eax, [sp-2] ; get address of our string from stack

    ; loop through string, call routines to print character

    ret ; pop address from top of stack (Address after "call _printf")
    ; into IP. The processor executes instructions at CS:IP,
    ; (real mode), or codesel:IP (PMode), so the processor
    ; continues execution after "call _printf",

    You do not need to worry about how it works unless you are working
    in assembly language--posted above. I, however, do recommend
    learning assembly language to understand the concepts better.

    An interesting note is that, if the stack gets currupt, an invalid address
    will be popped into IP, so the processor will start executing random
    instructions from a random location in memory-whether code, data,garbage, whatever, resulting a crash.

    While stack curruption is possible in C, most of these details are handled
    by the compilier.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=]Website[rightbr][/link][/size]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!