Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to understand how stack frames are built and which variables (params) are pushed to stack in what order? Some search results showed that the C/C++ compiler decides based on operations performed within a function. For example, if the function was supposed to just increment a passed-in int value by 1 (similar to ++ operator) and return it, it would put all the parameters of the function and local variables into registers.

I'm wondering which registers are used for returned or pass by value parameters. How are references returned? How does the compiler choose between eax, ebx,ecx and edx?

What do I need to know to understand how registers, stack and heap references are used, built and destroyed during function calls?

share|improve this question
    
this is rather hard to read (wall of text). Would you mind editing your post into a better shape? –  gnat Apr 18 '13 at 17:00
    
Thanks a Lot Karl! –  Gana Apr 18 '13 at 17:08
1  
This question seems rather broad to me. Also, isn't this going to be very platform-specific? –  Kazark Apr 18 '13 at 17:21
    
Question was also asked on SO: stackoverflow.com/questions/16088040/… –  Wayne Conrad Apr 18 '13 at 17:26
    
See also my answer on SO –  Basile Starynkevitch Apr 18 '13 at 17:39

3 Answers 3

up vote 3 down vote accepted

In addition to what Dirk said, an important use of stack frames is to save previous values of registers so that they can be restored after a function call. So, even on processors where registers are used for passing parameters, returning a value, and saving the return address, the values of those registers are saved on the stack before a function call so that they can be restored after the call. This allows one function to call another without overwriting its own parameters or forgetting its own return address.

So, calling a function B from function A on a typical "generic" system might involve the following steps:

  • function A:
    • push space for the return value
    • push parameters
    • push the return address
  • jump to the function B
  • function B:
    • push the address of the previous stack frame
    • push values of registers that this function uses (so they can be restored)
    • push space for local variables
    • do the necessary computation
    • restore the registers
    • restore the previous stack frame
    • store the function result
    • jump to the return address
  • function A:
    • pop the parameters
    • pop the return value

This is by no means the only way function calls can work (and I may have a step or two out of order), but it should give you an idea of how the stack is used to let the processor handle nested function calls.

share|improve this answer

This depends on the calling convention being used. Whoever defines the calling convention can make this decision however they want.

In the most common calling convention on x86, registers aren't used for passing parameters; the parameters are pushed on to the stack starting with the rightmost parameter. The return value is placed in eax and can use edx if it needs the extra space. References and pointers are both returned in the form of an address in eax.

share|improve this answer

What you are looking for is called Application Binary Interface - ABI.

There is a specification for each compiler that spells out the ABI.

Each platform will usually specify and ABI on order to support interoperability between compilers. For example, x86 Calling Conventions spells out the typical calling conventions for x86 and x86-64. I would expect a more official document than wikipedia however.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.