Differences in the initialization of the EAX register when calling a function in C and C++

后端 未结 2 1414
悲哀的现实
悲哀的现实 2021-01-17 07:54

There is a curious difference between assemblies of a small program, when compiled as a C-program or as a C++-program (for Linux x86-64).

The code in question:

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-17 08:25

    In C int fun(); can take any number of arguments, so it may even be a varargs function. In C++ however it means it takes no arguments.

    The x86-64 sysv abi convention demands that the register AL must contain the number of SSE registers used when invoking a varargs function. You of course pass no argument, so it is zeroed. For convenience the compiler decided to zero the whole eax. Declare your prototype as int fun(void); and the xor shall disappear.

提交回复
热议问题