Questions tagged [calling-convention]

A calling convention refers to the way a function transmits parameters to a called function and receives a return value from it.

890 questions
167
votes
4 answers

What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64

Following links explain x86-32 system call conventions for both UNIX (BSD flavor) & Linux: http://www.int80h.org/bsdasm/#system-calls http://www.freebsd.org/doc/en/books/developers-handbook/x86-system-calls.html But what are the x86-64 system call…
claws
  • 47,010
  • 55
  • 140
  • 185
159
votes
8 answers

What is __stdcall?

I'm learning about Win32 programming, and the WinMain prototype looks like: int WINAPI WinMain ( HINSTANCE instance, HINSTANCE prev_instance, PSTR cmd_line, int cmd_show ) I was confused as to what this WINAPI identifier was for and found: #define…
Tristan Havelick
  • 58,645
  • 19
  • 52
  • 64
117
votes
4 answers

Why does Windows64 use a different calling convention from all other OSes on x86-64?

AMD has an ABI specification that describes the calling convention to use on x86-64. All OSes follow it, except for Windows which has it's own x86-64 calling convention. Why? Does anyone know the technical, historical, or political reasons for this…
JanKanis
  • 5,166
  • 3
  • 31
  • 38
111
votes
7 answers

How exactly does the callstack work?

I'm trying to get a deeper understanding of how the low level operations of programming languages work and especially how they interact with the OS/CPU. I've probably read every answer in every stack/heap related thread here on Stack Overflow, and…
Christoph
  • 21,851
  • 24
  • 86
  • 121
93
votes
4 answers

Why can a T* be passed in register, but a unique_ptr cannot?

I'm watching Chandler Carruth's talk in CppCon 2019: There are no Zero-Cost Abstractions in it, he gives the example of how he was surprised by just how much overhead you incur by using an std::unique_ptr over an int*; that segment starts about…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
86
votes
3 answers

__cdecl or __stdcall on Windows?

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…
Etienne Dechamps
  • 21,669
  • 4
  • 29
  • 28
78
votes
9 answers

What is the meaning and usage of __stdcall?

I've come across __stdcall a lot these days. MSDN doesn't explain very clearly what it really means, when and why should it be used, if at all. I would appreciate if someone would provide an explanation, preferably with an example or two.
vehomzzz
  • 37,854
  • 69
  • 173
  • 207
60
votes
5 answers

What are callee and caller saved registers?

I'm having some trouble understanding the difference between caller and callee saved registers and when to use what. I am using the MSP430 : procedure: mov.w #0,R7 mov.w #0,R6 add.w R6,R7 inc.w R6 cmp.w R12,R6 jl l$loop mov.w R7,R12 ret the…
mugetsu
  • 3,812
  • 9
  • 43
  • 76
60
votes
7 answers

Retrieving the calling method name from within a method

I have a method in an object that is called from a number of places within the object. Is there a quick and easy way to get the name of the method that called this popular method. Pseudo Code EXAMPLE: public Main() { PopularMethod(); } public…
user26901
60
votes
3 answers

Where is the x86-64 System V ABI documented?

The x86-64 System V ABI (used on everything except Windows) used to live at http://x86-64.org/documentation/abi.pdf, but that site has now fallen off the internet. Is there a new authoritative home for the document?
Jeffrey Yasskin
  • 3,833
  • 1
  • 23
  • 35
58
votes
5 answers

What registers to save in the ARM C calling convention?

It's been a while since I last coded arm assembler and I'm a little rusty on the details. If I call a C function from arm, I only have to worry about saving r0-r3 and lr, right? If the C function uses any other registers, is it responsible for…
richq
  • 52,738
  • 20
  • 144
  • 141
51
votes
19 answers

Weird MSC 8.0 error: "The value of ESP was not properly saved across a function call..."

We recently attempted to break apart some of our Visual Studio projects into libraries, and everything seemed to compile and build fine in a test project with one of the library projects as a dependency. However, attempting to run the application…
user11180
  • 701
  • 2
  • 6
  • 5
45
votes
3 answers

What registers are preserved through a linux x86-64 function call

I believe I understand how the linux x86-64 ABI uses registers and stack to pass parameters to a function (cf. previous ABI discussion). What I'm confused about is if/what registers are expected to be preserved across a function call. That is, what…
boneheadgeek
  • 595
  • 1
  • 5
  • 9
44
votes
8 answers

Pass function as a parameter

I've written function 'A' that will call one of a number of other functions. To save re-writing function 'A', I'd like to pass the function to be called as a parameter of function 'A'. For example: function A{ Param($functionToCall) …
woter324
  • 1,734
  • 3
  • 15
  • 32
43
votes
3 answers

Why is %eax zeroed before a call to printf?

I am trying to pick up a little x86. I am compiling on a 64bit mac with gcc -S -O0. Code in C: printf("%d", 1); Output: movl $1, %esi leaq LC0(%rip), %rdi movl $0, %eax ; WHY? call _printf I do not understand why %eax is cleared…
sh54
  • 1,080
  • 1
  • 10
  • 20
1
2 3
59 60