calling-convention

__cdecl, __stdcall and __fastcall are all called the exact same way?

北慕城南 提交于 2019-11-29 07:03:15
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; } And my assembly code: ; 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

Order of evaluation of arguments in function calling?

半腔热情 提交于 2019-11-29 06:47:13
I am studying about undefined behavior in C and I came to a statement that states that there is no particular order of evaluation of function arguments but then what about the standard calling conventions like _cdecl and _stdcall , whose definition said (in a book) that arguments are evaluated from right to left. Now I am confused with these two definitions one, in accordance of UB, states different than the other which is in accordance of the definition of calling convention. Please justify the two. Keith Thompson As Graznarak's answer correctly points out, the order in which arguments are

Calling convention for function returning struct

瘦欲@ 提交于 2019-11-29 03:49:40
For the following C code: struct _AStruct { int a; int b; float c; float d; int e; }; typedef struct _AStruct AStruct; AStruct test_callee5(); void test_caller5(); void test_caller5() { AStruct g = test_callee5(); AStruct h = test_callee5(); } I get the following disassembly for Win32: _test_caller5: 00000000: lea eax,[esp-14h] 00000004: sub esp,14h 00000007: push eax 00000008: call _test_callee5 0000000D: lea ecx,[esp+4] 00000011: push ecx 00000012: call _test_callee5 00000017: add esp,1Ch 0000001A: ret And for Linux32: 00000000 <test_caller5>: 0: push %ebp 1: mov %esp,%ebp 3: sub $0x38,%esp

What's safecall?

限于喜欢 提交于 2019-11-29 03:27:48
I'm working on the creation of an ActiveX EXE using VB6, and the only example I got is all written in Delphi. Reading the example code, I noticed there are some functions whose signatures are followed by the safecall keyword. Here's an example: function AddSymbol(ASymbol: OleVariant): WordBool; safecall; What is the purpose of this keyword? Safecall passes parameters from right to left, instead of the pascal or register (default) from left to right With safecall, the procedure or function removes parameters from the stack upon returning (like pascal, but not like cdecl where it's up to the

C: Return value via stack/register question

别来无恙 提交于 2019-11-28 21:26:06
I am new to C, and there is one thing I can not understand. When function returns something that is not bigger than register -- my compiler puts it in EAX. When I return big structure (not pointer but structure itself) -- it is returned via stack. My question is: how compiler knows how to call function exported by another object? There is a calling conventions (like stdcall) but it is about passing the arguments, not reading the returned value, right? There should be some rule like "If return value declared to be larger than EAX, than take it from [bp-...]". And one more: would it be right to

Is “asmlinkage” required for a c function to be called from assembly?

穿精又带淫゛_ 提交于 2019-11-28 18:50:51
I am writing a C function that will be invoked from assembly code. (Specifically, I want to do some checking job in the path of system call handling in linux kernel, so I will call the c function before a system call is dispatched in entry_32.S) I am confused with the "asmlinkage" modifier when defining my c function. I know asmlinkage is to tell the compiler that the parameters will be passed through stack. #define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0))) Questions: (1) Is asmlinkage required when defining such a function that will be invoked from assembly code? (2) what is the

__cdecl or __stdcall on Windows?

蹲街弑〆低调 提交于 2019-11-28 15:20:34
I'm currently developing a C++ library for Windows which will be distributed as a DLL. My goal is to maximize binary interoperability; more precisely, the functions in my DLL must be usable from code compiled with multiple versions of MSVC++ and MinGW without having to recompile the DLL. However, I'm confused about which calling convention is best, cdecl or stdcall . Sometimes I hear statements like "the C calling convention is the only one guaranteed to be the same accross compilers", which contrasts with statements like " There are some variations in the interpretation of cdecl ,

How does method chaining work?

北城以北 提交于 2019-11-28 14:50:06
How does getRequestDispatcher("xxx") get called from getServletContext() in the example below? How does calling procedures like this work in general? Please give me a clear picture about this context. RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp"); dispatcher.include(request, response); AGreenman getServletContext() returns a ServletContext object, which has a method called getRequestDispatcher() . Your line of code is just shorthand for two method calls, and is equivalent to this code: ServletContext context = getServletContext(); RequestDispatcher

How to “goto” into different function in c?

无人久伴 提交于 2019-11-28 09:00:57
Basically I am trying to simulate assembly code in C. Here is the C code: int main() { test(); main_next: printf("Hello, World!"); } void test() { goto main_next; } Trying to compile this code (Linux 32 bit, gcc 4.6.3), I got this error: error: label ‘main_randomtag_next’ used but not defined Does anyone know how to do this kind of inter-procedural goto in C? Thank you! But what about the children? stack? goto between functions doesn't make any sense if you think about the stack. What will be on the stack when you jump? The source and destination functions could potentially have different

Is fastcall really faster?

江枫思渺然 提交于 2019-11-28 07:58:39
Is the fastcall calling convention really faster than other calling conventions, such as cdecl? Are there any benchmarks out there that show how performance is affected by calling convention? It depends on the platform. For a Xenon PowerPC, for example, it can be an order of magnitude difference due to a load-hit-store issue with passing data on the stack. I empirically timed the overhead of a cdecl function at about 45 cycles compared to ~4 for a fastcall . For an out-of-order x86 (Intel and AMD), the impact may be much less, because the registers are all shadowed and renamed anyway. The