__cdecl,__ stdcall和__fastcall的调用方式完全相同吗? [英] __cdecl, __stdcall and __fastcall are all called the exact same way?

查看:155
本文介绍了__cdecl,__ stdcall和__fastcall的调用方式完全相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual C ++ 2010和MASM作为我的x64-Assembler.
这是我的C ++代码:

I am using Visual C++ 2010, and MASM as my x64-Assembler.
This is my C++ code:

// include directive
#include "stdafx.h"
// functions
extern "C" int Asm();
extern "C" int (convention) sum(int x, int y) { return x + y; }
// main function
int main()
{
    // print asm
    printf("Asm returned %d.\n", Asm());
    // get char, return
    _getch();
    return EXIT_SUCCESS;
}

我的汇编代码:

; external functions
extern sum : proc
; code segment
.code
Asm proc
    ; create shadow space
    sub rsp, 20o
    ; setup parameters
    mov ecx, 10
    mov edx, 15
    ; call
    call sum
    ; clean-up shadow space
    add rsp, 20o
    ; return
    ret
Asm endp
end

执行此操作的原因是可以了解不同的调用约定. 我将以sum的调用约定stdcall进行修改,并修改asm代码,以便以sum的"stdcall"方式进行调用.一旦完成该工作,就可以进行快速呼叫,然后以快速呼叫"方式在asm中调用它.

The reason I am doing this is so I can learn the different calling conventions. I would make sum's calling convention stdcall, and modify the asm code so it would call sum the "stdcall" way. Once I got that working, I would make it, say, fastcall, and then call it in asm the "fastcall" way.

但是现在看看我的汇编代码.当我使用该代码时,无论sum是stdcall,fastcall还是cdecl,它都会编译,执行并输出25作为我的sum.

But look at my assembly code right now. When I use that code, no matter if sum is stdcall, fastcall or cdecl, it will compile, execute fine, and print 25 as my sum.

我的问题:__cdecl,__ stdcall和__fastcall如何以及为什么都可以以完全相同的方式调用?

My question: How, and why can __cdecl, __stdcall and __fastcall all be called the exact same way?

推荐答案

问题是您要针对x64目标进行编译.来自 MSDN

The problem is that you're compiling for x64 targets. From MSDN

给定扩展寄存器集,x64仅使用__fastcall调用 约定和基于RISC的异常处理模型. __fastcall 模型对前四个参数和堆栈帧使用寄存器 传递其他参数.

Given the expanded register set, x64 just uses the __fastcall calling convention and a RISC-based exception-handling model. The __fastcall model uses registers for the first four arguments and the stack frame to pass the other parameters.

切换到针对x86目标进行编译,您应该能够看到正在使用的各种调用约定.

Switch over to compiling for x86 targets, and you should be able to see the various calling conventions in action.

这篇关于__cdecl,__ stdcall和__fastcall的调用方式完全相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆