views:

231

answers:

2

Hi,

Here's my previous question about switching C callstacks. However, C++ uses a different calling convention (thiscall) and may require some different asm code. Can someone explain the differences and point to or supply some code snippets that switch C++ callstacks (preferably in GCC inline asm)?

Thanks, James

+2  A: 

The code given in the previous question should work fine.

The thiscall calling convention differs only in who is responsible for popping the arguments off the stack. Under the thiscall calling convention, the callee pops the arguments (and additionally, the this pointer is passed in ecx); under the C calling convention, the caller pops the arguments. This does not affect context switches.

However, if you're going to do context switches yourself, note that you need to save and restore the registers as well (probably on the stack) in addition to switching stacks.

Note, by the way, that C++ doesn't always use thiscall -- it's only used for methods with a fixed number of arguments (and apart from that, it's a Microsoftism... g++ doesn't use it).

Martin B
Um, isn't thiscall only about where the hidden `this` parameter is passed to the callee? (IN some register, IIRC; but I'm way out of my depth with this, so just ignore me if I'm babbling incoherently here...)
sbi
Are you sure you're not confusing thiscall with fastcall? thiscall is ment to pass a this pointer if I'm not mistaken via ecx, when fastcall is indeed sometimes used by compilers to pass arguments via registers instead of pushing them on stack.
Dmitry
Yes, that sounds about right. I've added a link to the MSDN entry with the details.
Martin B
+1  A: 

Note the ABI for C++ is not explicitly defined.

The idea was that compiler manufactures are able to use the optimal calling convention for the situation and thus make C++ faster.

The down side of this is that each compiler has its own calling convention thus code from different compilers are not compatable (even code form different versions (or even different optimization flags) of the same compiler can be incompatable).

Martin York