'Calling Convention'에 해당되는 글 2건

  1. 2010.04.17 calling convention(콜링 컨벤션)
  2. 2010.01.06 cdecl / stdcall / fastcall
Programming/언어론2010.04.17 17:39
convention 은

[링크 : http://engdic.daum.net/dicen/contents.do?query1=E255050]
이런 의미를 가지는데, 굳이 해석하자면, 호출 규약 혹은 관용적인 호출방법 이라고 하면 되려나?

아무튼 문득 Iczelion 의 강좌를 읽다보니 어셈블리 기본 구조에
STDCALL 이라는 용어가 나오고, calling convention 이 나오길래 자세히 읽어 봤더니
'함수를 호출시에 인자를 넘겨주는 데이터를 stack에 넣는 방법'을 의미한다.

.MODEL is an assembler directive that specifies memory model of your program. Under Win32, there's only on model, FLAT model.
STDCALL tells MASM about parameter passing convention. Parameter passing convention specifies the order of  parameter passing, left-to-right or right-to-left, and also who will balance the stack frame after the function call.
Under Win16, there are two types of calling convention, C and PASCAL
C calling convention passes parameters from right to left, that is , the rightmost parameter is pushed first. The caller is responsible for balancing the stack frame after the call. For example, in order to call a function named foo(int first_param, int second_param, int third_param) in C calling convention the asm codes will look like this:

push  [third_param]               ; Push the third parameter
push  [second_param]            ; Followed by the second
push  [first_param]                ; And the first
call    foo
add    sp, 12                                ; The caller balances the stack frame
PASCAL calling convention is the reverse of C calling convention. It passes parameters from left to right and the callee is responsible for the stack balancing after the call.
Win16 adopts
PASCAL convention because it produces smaller codes. C convention is useful when you don't know how many parameters will be passed to the function as in the case of wsprintf(). In the case of wsprintf(), the function has no way to determine beforehand how many parameters will be pushed on the stack, so it cannot do the stack balancing.
STDCALL is the hybrid of C and PASCAL convention. It passes parameter from right to left but the callee is responsible for stack balancing after the call.Win32 platform use STDCALL exclusively. Except in one case: wsprintf(). You must use C calling convention with wsprintf().

[링크 : http://win32assembly.online.fr/tut1.html]

예를 들어, C언어의 경우에는  STDCALL을 사용하며,
인자(argument)들은 오른쪽 부터 stack에 push() 된다.

이런식으로 함수 호출시 인자를 넘겨주게 된다.

음.. 개인적인 생각이지만, C언어의 경우 heap은 아래에서 위로 커나가는데 그 방향을 맞추려고
stack에도 데이터를 이렇게 반대 순서로 넣는게 아닐까 싶다.

물론 argument 순서가 의미는 없기 때문에(받아들이는 쪽에서 알아서 받는다면)
가장 위에 첫 인자가 있을 이유는 없는데, 굳이 이런식으로 방향성을 가진다는건...
메모리 구조에 기인한게 아닐려나?

(아니면 말구 -ㅁ-!)

'Programming > 언어론' 카테고리의 다른 글

dangling if-else  (0) 2014.08.13
double의 정확도 자릿수  (0) 2011.03.25
함수 포인터 (function pointer)  (0) 2010.09.16
type system  (0) 2010.09.15
calling convention(콜링 컨벤션)  (0) 2010.04.17
Posted by 구차니

댓글을 달아 주세요

expat 글 보다가 무슨 말인지 몰라서 검색은 해봤는데 점점더 미궁으로 빠져드는 느낌이다 ㄱ-
일단은 call stack 관련 선언문이라는것 외에는 이해를 전혀 못하겠다 ㅠ.ㅠ

    On the Intel 386, the cdecl attribute causes the compiler to assume that the calling function will pop off the stack space used to pass arguments. This is useful to override the effects of the -mrtd switch.
    On the Intel 386, the stdcall attribute causes the compiler to assume that the called function will pop off the stack space used to pass arguments, unless it takes a variable number of arguments.
    On the Intel 386, the fastcall attribute causes the compiler to pass the first two arguments in the registers ECX and EDX. Subsequent arguments are passed on the stack. The called function will pop the arguments off the stack. If the number of arguments is variable all arguments are pushed on the stack.

[링크 : http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html]

콜링 컨벤션(Calling convention)
MS방식은 5가지


[링크 : http://codesafe.tistory.com/94]

All arguments are widened to 32 bits when they are passed. Return values are also widened to 32 bits and returned in the EAX register, except for 8-byte structures, which are returned in the EDX:EAX register pair. Larger structures are returned in the EAX register as pointers to hidden return structures. Parameters are pushed onto the stack from right to left.

The compiler generates prolog and epilog code to save and restore the ESI, EDI, EBX, and EBP registers, if they are used in the function.

Note   When a struct, union, or class is returned from a function by value, all definitions of the type need to be the same, else the program may fail at runtime.

For information on how to define your own function prolog and epilog code, see Naked Function Calls.

The following calling conventions are supported by the Visual C/C++ compiler.

Keyword Stack cleanup Parameter passing
__cdecl Caller Pushes parameters on the stack, in reverse order (right to left)
__stdcall Callee Pushes parameters on the stack, in reverse order (right to left)
__fastcall Callee Stored in registers, then pushed on stack
(not a keyword)
Callee Pushed on stack; this pointer stored in ECX

[링크 : http://msdn.microsoft.com/en-us/library/984x0h58%28VS.71%29.aspx]

[링크 : http://en.wikipedia.org/wiki/X86_calling_conventions]
Posted by 구차니

댓글을 달아 주세요