Who is responsible for cleaning?

I want to know which one is responsible for clearing the stack

Suppose you have a function fun like:

var = fun(int x, int y, float z, char x);

      

when fun

called, it will go on the stack along with the parameters, and then when the function returns who is responsible for clearing the stack, it is the function it self or "var" that will hold the return value.

One more thing, can anyone explain the concepts of calling conventions?

+2


source to share


3 answers


the calling convention refers to who performs the stack cleanup; caller or called party.



Calling conventions may differ:

  • where parameters and return values ​​are located (in registers; on call stack; combination of both)
  • the order in which parameters (or parts of one parameter) are passed
  • how the task of setting up and clearing a function call is split between the caller and the called party.
  • which registers that can be directly used by the callee can sometimes also be included

An architecture almost always has more than one possible calling convention.

+5


source


You yourself answered the answer: invoke agreements .

A calling agreement is like a contract . It solves the following things:

  • Who is responsible for clearing the parameters.
  • How and in what order the parameters are passed to the called function.
  • Where the return value is stored.

There are many different calling conventions, depending on the platform and programming environment. Two common calling conventions on x86 platforms:

STDCALL

Parameters are pushed onto the stack from right to left. The called function clears the stack.

Cdecl

Parameters are pushed onto the stack from right to left. The caller clears the stack.

In both cases, the return value is in a register EAX

(or ST0

for floating point values)

Many programming languages ​​for the x86 platform allow you to specify a calling convention, for example:



Delphi

function MyFunc(x: Integer): Integer; stdcall;

      

Microsoft C / C ++

int __stdcall myFunc(int x)

      

Some notes on use:

When building a simple application, it is rarely necessary to change or learn about the calling convention, but there are two typical cases where you need to deal with calls:

  • When calling external Win32 API libraries, for example: you must use compatible calling conventions, otherwise the stack may get corrupted.
  • When writing inline assembly code: you need to know in which registers and where on the stack you will find the variables.

For more details, I recommend the following Wikipedia articles:

+9


source


By the time this line is finished, var will have saved the return value of fun (), and any memory on the stack used by the fun is gone: "push", "pop" all tidy.

Calling conditionals: Anything the compiler organizes so the fun can get the job done. Consider those parameters x, y, z. What order do they go on the stack (indeed, they go through the stack)? It does not matter as long as the compromiser and the interlocutor agree! This is an agreement.

0


source







All Articles