[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Parameter Passing Order

Can anybody explain why pascal order of pushing parameters is not suitable for variable
number of arguments.Why can't compiler generate more information for such cases.Please
explain the root problem in this convention.

Regards,
Chai-yo
0
chai_yo123
Asked:
chai_yo123
1 Solution
 
_lychee_Commented:
in pascal arguments pushed from left to right, so the rightmost argument is the shallowest element on the stack...

y this is bad for variable arguments: the info on no. of args is normally the 1st argument (y not the last? i guess that makes type checking much more difficult?)... so when the routine is entered, the code can't determine how long the variable list is....

i guess it's possible for the compiler to push an extra word of information on how many params were passed, but y use that when u can just use the C convention?
0
 
LucHoltkampCommented:
Another problem with the PASCAL calling convention is that the called function is responsible for cleaning up the stack. Whereas in the C calling convention the caller cleans up the stack after the call. The problem is that the called function does not know how many parameters are on the stack (it must be known during compile-time, but it can vary...), so it cannot clean up the stack.

>> I guess it's possible for the compiler to push an extra word
>> of information on how many params were passed
I don't thinks so... and not only the number is neccesairy but also the size, otherwise the called function still can't clean up the stack. This would also mean a special calling convention for var-args...

>> the info on no. of args is normally the 1st argument (y not
>> the last? i guess that makes type checking much more
>> difficult?)...
Even impossible, for the adress of that 1st argument can not be retrieved without the information in that same first argument...

Luc
0
 
_lychee_Commented:
actually the called function can (with quite a lot of hassle) clean up the stack....

it pops the cs/ip, pops everything else, pushes back the cs/ip and then returns... that's on the intel archi at least...

but again, y go thru the hassle when u have the (wonderful?) c calling convention?
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

 
wyllikerCommented:
LucHoltkamp is correct with the reason why PASCAL calling conventions cannot take variable parameters - the number of arguments to remove from the stack would be unknown in that you would only know the minimum number of arguments and that the number of arguments could be different on a per caller basis.

The language Pascal does runtime parameter checking so it requires that all of the parameters' types are known when it generates code.  With variable argument C functions - it is not possible for the compiler to know the true type of any one variable argument. It would be up to the called function to have a way of discerning the type of argument - printf uses the format masks to coerce values to match and assumes the type of the corresponding argument but you could just as easily pass garbage to printf and it wouldn't know until it blew up.


0
 
isasoriCommented:
Fixed amount of arguments - concept of language Pascal as strict programming language of a high level.
By the way, it is one from the reasons, why the Pascal compiler works much faster C compiler.
If to add in Ïàñêàëü a variable amount of arguments, arithmetic of the pointers and still some things, we shall receive C with the changed syntax.
0

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now