__fastcall呼び出し規約:
int foo1(int a) { return a * 2; } int foo2(int a, int b) { return a + b; } int foo3(int a, int b, int c) { return a + b + c; } int foo4(int a, int b, int c, int d) { return a + b + c + d; }
#include <stdio.h> extern int foo1(int a); extern int foo2(int a, int b); extern int foo3(int a, int b, int c); extern int foo4(int a, int b, int c, int d); int __cdecl main(int argc, char *argv[]) { printf("foo1() = %d\n", foo1(10)); printf("foo2() = %d\n", foo2(10, 20)); printf("foo3() = %d\n", foo3(10, 20, 30)); printf("foo4() = %d\n", foo4(10, 20, 30, 40)); return 0; }
int __fastcall foobar(); typedef int (__fastcall *ptr)();
コンパイル&リンク&実行
> cl /FAs /TC /Od /nologo /c /Gr /Focallee_fastcall.obj callee.c > cl /FAs /TC /Od /nologo /c /Gr /Focaller_fastcall.obj caller.c > link /SUBSYSTEM:CONSOLE /NOLOGO /OUT:fastcall.exe caller_fastcall.obj callee_fastcall.obj > fastcall.exe foo1() = 20 foo2() = 30 foo3() = 60 foo4() = 100
; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00.30729.01 TITLE C:\in_vitro\c\calling_conventions\callee.c .686P .XMM include listing.inc .model flat INCLUDELIB LIBCMT INCLUDELIB OLDNAMES PUBLIC @foo1@4 ; Function compile flags: /Odtp ; File c:\in_vitro\c\calling_conventions\callee.c _TEXT SEGMENT _a$ = -4 ; size = 4 @foo1@4 PROC ; _a$ = ecx ; 1 : int foo1(int a) { return a * 2; } push ebp mov ebp, esp push ecx mov DWORD PTR _a$[ebp], ecx mov eax, DWORD PTR _a$[ebp] shl eax, 1 mov esp, ebp pop ebp ret 0 @foo1@4 ENDP _TEXT ENDS PUBLIC @foo2@8 ; Function compile flags: /Odtp _TEXT SEGMENT _b$ = -8 ; size = 4 _a$ = -4 ; size = 4 @foo2@8 PROC ; _a$ = ecx ; _b$ = edx ; 2 : int foo2(int a, int b) { return a + b; } push ebp mov ebp, esp sub esp, 8 mov DWORD PTR _b$[ebp], edx mov DWORD PTR _a$[ebp], ecx mov eax, DWORD PTR _a$[ebp] add eax, DWORD PTR _b$[ebp] mov esp, ebp pop ebp ret 0 @foo2@8 ENDP _TEXT ENDS PUBLIC @foo3@12 ; Function compile flags: /Odtp _TEXT SEGMENT _b$ = -8 ; size = 4 _a$ = -4 ; size = 4 _c$ = 8 ; size = 4 @foo3@12 PROC ; _a$ = ecx ; _b$ = edx ; 3 : int foo3(int a, int b, int c) { return a + b + c; } push ebp mov ebp, esp sub esp, 8 mov DWORD PTR _b$[ebp], edx mov DWORD PTR _a$[ebp], ecx mov eax, DWORD PTR _a$[ebp] add eax, DWORD PTR _b$[ebp] add eax, DWORD PTR _c$[ebp] mov esp, ebp pop ebp ret 4 @foo3@12 ENDP _TEXT ENDS PUBLIC @foo4@16 ; Function compile flags: /Odtp _TEXT SEGMENT _b$ = -8 ; size = 4 _a$ = -4 ; size = 4 _c$ = 8 ; size = 4 _d$ = 12 ; size = 4 @foo4@16 PROC ; _a$ = ecx ; _b$ = edx ; 4 : int foo4(int a, int b, int c, int d) { return a + b + c + d; } push ebp mov ebp, esp sub esp, 8 mov DWORD PTR _b$[ebp], edx mov DWORD PTR _a$[ebp], ecx mov eax, DWORD PTR _a$[ebp] add eax, DWORD PTR _b$[ebp] add eax, DWORD PTR _c$[ebp] add eax, DWORD PTR _d$[ebp] mov esp, ebp pop ebp ret 8 @foo4@16 ENDP _TEXT ENDS END
; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00.30729.01 TITLE C:\in_vitro\c\calling_conventions\caller.c .686P .XMM include listing.inc .model flat INCLUDELIB LIBCMT INCLUDELIB OLDNAMES _DATA SEGMENT $SG2496 DB 'foo1() = %d', 0aH, 00H ORG $+3 $SG2497 DB 'foo2() = %d', 0aH, 00H ORG $+3 $SG2498 DB 'foo3() = %d', 0aH, 00H ORG $+3 $SG2499 DB 'foo4() = %d', 0aH, 00H _DATA ENDS PUBLIC _main EXTRN @foo4@16:PROC EXTRN @foo3@12:PROC EXTRN @foo2@8:PROC EXTRN _printf:PROC EXTRN @foo1@4:PROC ; Function compile flags: /Odtp ; File c:\in_vitro\c\calling_conventions\caller.c _TEXT SEGMENT _argc$ = 8 ; size = 4 _argv$ = 12 ; size = 4 _main PROC ; 9 : { push ebp mov ebp, esp ; 10 : printf("foo1() = %d\n", foo1(10)); mov ecx, 10 ; 0000000aH call @foo1@4 push eax push OFFSET $SG2496 call _printf add esp, 8 ; 11 : printf("foo2() = %d\n", foo2(10, 20)); mov edx, 20 ; 00000014H mov ecx, 10 ; 0000000aH call @foo2@8 push eax push OFFSET $SG2497 call _printf add esp, 8 ; 12 : printf("foo3() = %d\n", foo3(10, 20, 30)); push 30 ; 0000001eH mov edx, 20 ; 00000014H mov ecx, 10 ; 0000000aH call @foo3@12 push eax push OFFSET $SG2498 call _printf add esp, 8 ; 13 : printf("foo4() = %d\n", foo4(10, 20, 30, 40)); push 40 ; 00000028H push 30 ; 0000001eH mov edx, 20 ; 00000014H mov ecx, 10 ; 0000000aH call @foo4@16 push eax push OFFSET $SG2499 call _printf add esp, 8 ; 14 : return 0; xor eax, eax ; 15 : } pop ebp ret 0 _main ENDP _TEXT ENDS END