About passing parameters in compiled programming languages

Asked

Viewed 101 times

1

What would be more advantageous when calling a function, putting in the stack from right to left or vice versa? And cleaning the stack? The function that calls (Caller) or what is called (calee) should be responsible for this?

My two cents:

When the function that calls is responsible for cleaning the stack, it is possible to create functions with the amount of variable parameters, because it is not necessary to know such quantity before the execution time, being only necessary some more logic. (ex: printf)

Regarding the order in which the arguments are placed in the stack, from left to right allows the first arguments to be used to determine the amount of arguments that the function will receive, however, this is possible in the other order by placing such information in the last arguments, so it is a matter of convention, only.

Any supplement or correction?

  • You are confused to understand your doubt and, is that a question? You yourself seem to be trying to answer...

  • I’m sorry if you’re confused... yes, I’m trying to answer, it’s a question of proof, but I realize that the answer can be complex, it depends on how much who’s going to correct the question wants to go into the details. I’m trying not to make statements in the answer, but to point out what usually happens in most compilers, architectures, conventions etc...

1 answer

2


What would be more advantageous when calling a function, put in the stack from right to left or vice versa?

If I understand what you want to know, in your code, in general, it makes little difference how you put it. It is interesting to worry about it just because of some optimization for some specific situation, after all has call convention put the first parameters directly into register, if possible.

Are we talking about which architecture? It may vary. What might look good on one might not be the best on another. Even how it compiles may vary. You’re actually speaking two languages and that changes too. It may vary according to the API you’re using.

Note that there is a difference between you writing your code and what the compiler will do. And what the compiler will make a decision according to the standard convention or other if the code indicates this.

C usually uses right to left. Pascal usually uses left to right. But it’s not your code problem, unless you need to interoperate or need extreme optimization.

Note that the typical convention of language does not necessarily need to be used in it, nor is it unique to it.

If you’re not thinking about optimization, it doesn’t matter how you put the arguments.

What about cleaning the battery? The function that calls (Caller) or what is called (calee) should be responsible for this?

Depending on the convention used, it may be the responsibility of both, each takes care of a party, which is the case of cdecl, although it is strictly considered that it is the caller who is responsible.

Much of the conventions leave the so-called function dealing with the preservation and cleaning of registers, including the pointer to the stack, which tends to make the code more efficient, with the stdcall.

When the calling function is responsible for clearing the stack, it is possible to create functions with the amount of variable parameters

It is actually possible to have variable amount of arguments, not of parameters.

Apart from something I don’t know or have not attempted to attack me or understood in the question, the rest seems correct.

  • It is a question that I have to solve of a proof and that is open to many interpretations, because everything can influence: the language, the architecture, the convention used and the interpretation itself of the convention used, as happens with cdecl, on the part of the chosen compiler. Conclusion: usually, the "Cleanup" by the function it calls is more advantageous to enable variable arguments, but can be opened to interpretation by the fact that a stdcall, for example, is more efficient and the insertion order does not matter. It’s a question of a microcontroller chair with a teacher barking...

  • Thank you for the reply!

  • It is not easy to answer something complex here, even more without details.

  • Well then! There are not many details, unfortunately! The question simply states that usually C uses one convention and pascal uses another and asks which convention is more advantageous and why. Since the question itself does not give so many details, I think the answer should be written based on what is expected usually. So I came here to ask for help to know if my claims have any foundation or not, along with a possible correction.

  • Without details the question tends to be based on opinions and even can answer, there are no clear criteria. It may turn out to be what was passed on in class, but we don’t have that information.

  • The worst was not even passed in class, and it doesn’t have much to do with the discipline, but I don’t think this subject is of much interest to the stackoverflow community, anyway, thanks for the help. abs

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.