Calling Conventions

0

jak mamy procke czy funkcje to jaka jest różnica miedzy przekazywanie parametrów ( left->right) a ( right->left) ?????? :

0

A moze by tak help poczytac:

When you declare a procedure or function, you can specify a calling convention using one of the directives register, pascal, cdecl, stdcall, and safecall. For example,

function MyFunction(X, Y: Real): Real; cdecl;
...

Calling conventions determine the order in which parameters are passed to the routine. They also affect the removal of parameters from the stack, the use of registers for passing parameters, and error and exception handling. The default calling convention is register.

The register and pascal conventions pass parameters from left to right; that is, the left most parameter is evaluated and passed first and the rightmost parameter is evaluated and passed last. The cdecl, stdcall, and safecall conventions pass parameters from right to left.
For all conventions except cdecl, the procedure or function removes parameters from the stack upon returning. With the cdecl convention, the caller removes parameters from the stack when the call returns.

The register convention uses up to three CPU registers to pass parameters, while the other conventions pass all parameters on the stack.
The safecall convention implements exception "firewalls." On Windows, this implements interprocess COM error notification.

The table below summarizes calling conventions.

Calling conventions
Directive Parameter order Clean-up Passes parameters in registers?
register Left-to-right Routine Yes
pascal Left-to-right Routine No
cdecl Right-to-left Caller No
stdcall Right-to-left Routine No
safecall Right-to-left Routine No
The default register convention is the most efficient, since it usually avoids creation of a stack frame. (Access methods for published properties must use register.) The cdecl convention is useful when you call functions from shared libraries written in C or C++, while stdcall and safecall are recommended, in general, for calls to external code. On Windows, the operating system APIs are stdcall and safecall. Other operating systems generally use cdecl. (Note that stdcall is more efficient than cdecl.)

The safecall convention must be used for declaring dual-interface methods. The pascal convention is maintained for backward compatibility. For more information on calling conventions, see Program control.
The directives near, far, and export refer to calling conventions in 16-bit Windows programming. They have no effect in 32-bit applications and are maintained for backward compatibility only.

0

30 sek przed napisaniem tego postu przeczytałem ten dział z helpa .......
a napisałem bo nie wszytko jarze :-[

0

procedura Jakas(p1, p2, p3:integer);

Left to right: najpierw przekazywany jest parametr p1, potem p2 potem p3

Right to left: najpierw p3, potem p2, potem p1

0

:-) :-) nie żebym był niegrzeczny ale to akurat zrozumiałem z tego helpa :-) .........chodzi mi właśnie o to jaki przekazywanie parametrów ma skutek na wykonanie procki czy funkcji .......

0

Jeżeli chodzi o Deemera to mogę sobie dać głowę uciąć, że helpa czyta zawsze (żeby nie powiedzieć, że cały czas), więc zbywanie go helpem.
Co do przekazywania parametrów to chodzi tutaj o przekazywanie przez stos. Troszkę assemblera tu potrzeba.
Do procedur parametry mogą być przekazywane przez rejstry lub przez stos. Przekazywanie przez rejestry jest najszybsze, ale jest ich ograniczona ilość. Dlatego pierwsze 3 parametry, zamiast lądować na stosie ladują w rejestrach eax, edx, ecx
Na stos parametry przekazywane są albo od prawej albo od lewej. Jak wiadomo to co pierwsze wejdzie na stos wychodzi ostatnie, dlatego często ma to znaczenie.
Jak mamy procedurkę:
Test(a, b, c, d, e, f: Integer);
to:
a znajdzie się w eax
b - edx
c - ecx
a potem po kolei na stosie wylądują (domyślny pascalowy styl):
d, e i f
Jeżeli użyjemy stylu C (od prawej) to mamy:
f - eax
e - edx
d - ecx
i na stosie kolejno:
c, b i a.
Deklaracja stylu przekazywania jest najbardziej istotna przy komunikacji z innymi programami, dll-kami itp. We własnym programie najlepiej stosować domyślny pascalowy styl (chyba, że używamy asemblera to dla ułatwienia można czasem sobie użyć najwygodniejszego w danej chwili).
Jest jeszcze różnica w porządkowaniu stosu, ale to już inna bajka.
Z mojej perspektywy od strony asm najlepsze wydaje się być stdcall łączące zalety pascal i cdcel.

0

Sorry Deemer.

Dryobates: to jak to jest z tym C. Piszesz, ze w konwencji C tez sa uzywane rejestry (tyle, ze od prawej). Jednak help twierdzi, ze TYLKO konwencja register uzywa rejestrow, zas cdecl nie i jest generalnie b. podobna do stdcall, z tym, ze w cdecl (jako jedynej) procedura wywolujaca ma posprzatac na stosie, zas w stdcall jak i w pozostalych ze stosu sprzata procedura wywolywana. Z tego wnioskuje, ze w C nie przekazuje sie przez rejestry. Jesli sie myle, oswiec mnie plz.

0

Prawdopodobnie Kubie chodziło o modyfikator __fastcall używany w C++Builder. On powoduje, że 3 parametry przekazywane są przez rejestry.
Cytat z helpu:

Use the __fastcall modifier to declare functions that expect parameters to be passed in registers. The first three parameters are passed (from left to right) in EAX, EDX, and ECX, if they fit in the register.

Jeśli nie o tym mówił Dryo, to już nie wiem.

0

Prawdopodobnie Kubie chodziło o modyfikator __fastcall używany w C++Builder.

Ano. Tylko mój błąd, bo napisałem, że pascal jest domyślnym. A domyślnym jest register [wstyd]

1 użytkowników online, w tym zalogowanych: 0, gości: 1