1

i know this is the big deal to manipulate stack but i think it would be a great lesson for me. im searched the internet, and i found calling convention. I know how its working and why. I whant to simulate some of "Callee clean-up stack" maybe stdcall, fastcall its doesnt matter, important think is that who clean-up stack, then i will be have less work do to :)

for example. i have function in C

double __fastcall Add(int a, int b) {
    return a + b;
}

it will be Calee

and i have pointer to this function with type void*,

void* p = reinterpreted_cast<void*>(Add);

And i have function Caller

void Call(void* p, int a, int b) {

    //some code about push and pop arg
    //some code about save p into register
    //some code about move 'p' into CPU to call this function manually
    //some code about take of from stack/maybe register the output from function
}

And thats it, its helpful when i use calling convention "Calle clean-up" because i dont need

//some code about cleans-up this mess

I dont know how to do it, i know it can be done with assembler. but i afraid about it, and i never 'touch' this language. i would be greatful to simulate that calling with C, but when anyone can do it with ASM i will be haapy :)

I told also what i whant to do with it, when i will be know how to manually call function, i will be able to call function with several parameters(if i know the number and size of it) and any type of function. so i will be able to call any function in any language when that function is in the right calling convention.

I'm using Windows OS x64 and MinGw

2
  • In the case of your Call function, is there any reason why you can't just define a new type like typedef double (__fastcall *binaryop)(int, int); and use that for p?
    – Michael
    Aug 9, 2013 at 12:34
  • Does Win64 even have fastcall ? I was under the impression there is only one Windows 64bit calling convention, which is "kind of" fastcall because it passes args 1..4 in registers, but then it's definitely not the same as traditional 32bit Windows fastcall. Are you saying you're trying to call 32bit code from within 64bit code, i.e. implement your own "thunking" ?
    – FrankH.
    Aug 9, 2013 at 13:27

1 Answer 1

1

First of all: C is intended to hide calling conventions and everything that is specific to how your code is executed from the programmer and provide an abstract layer above it.

The only condition when you need to (as you say) "manually" call a function is when you do it from asm.

C as a language has no direct control over the stack or the program counter.

To cite from GCC manual for fastcall for x86:

 On the Intel 386, the `fastcall' attribute causes the compiler to
 pass the first two arguments in the registers ECX and EDX.
 Subsequent arguments are passed on the stack. The called function
 will pop the arguments off the stack. If the number of arguments
 is variable all arguments are pushed on the stack.

Also as far as I remember return values are passed in EAX.

So in order to call a function in this way you need to provide the arguments in ECX, EDX and then invoke the call instruction on the function address

int __fastcall Add(int a, int b) {
    return a + b;
}

Please note I have changed the return type to int, because I do not remember how doubles are passed back.

int a, b;
// set a,b to something

void* p = reinterpreted_cast<void*>(Add);
int return_val;
asm (
    "call %3"
    : "=a" (return_val) // return value is passed in eax
    : "c" (a) // pass c in ecx
    , "d" (b) // pass b in edx
    , "r" (p) // pass p in a random free register
);

By calling convention it is up to the callee to clean up any used stack space. In this case we didn't use any, but if we did then your compiler will translate your function Add in such a way that it cleans up the stack automatically.

The code above is actually a hack in such a way that I use the GCC extended asm syntax to automatically put our variables into the appropriate registers. It will generate sufficient code around this asm call to make sure data is consistent.

If you wish to use a stack based calling convention then cdecl is the standard one

int __cdecl Add(int a, int b) {
    return a + b;
}

Then we need to push the arguments to the stack prior to calling

asm (
    "push %1\n" // push a to the stack
    "push %2\n" // push b to the stack
    "call %3"   // the callee will pop them from the stack and clean up
    : "=a" (return_val) // return value is passed in eax
    : "r" (a) // pass c in any register
    , "r" (b) // pass b in any register
    , "r" (p) // pass p in any register
);

One thing that I have not mentioned is that this asm call does not save any of our in-use registers, so I do not recommend putting this in a function that does anything else. In 32 bit x86 there is an instruction pushad that will push all general purpose registers to the stack and an equivalent (popad) to restore them. An equivalent for x86_64 is unavailable though. Normally when you compile C code the compiler will know which registers are in use and will save them in order for the callee not to overwrite them. Here it does not. If your callee uses registers that are in use by the caller - they will be overwritten!

8
  • you say Intel386, so this code will be "unmovable code" ? and this ill 'explode' on AMD ? is there any way to make multiplatorm code ? i mean, i whant to use only Windows, but AMD, Intel and architeture x86 and x64 Aug 9, 2013 at 13:25
  • @user2418897 Intel i386 is the technical term for what is commonly referred to x86. The same calling conventions were ported to x86_64. In terms of the call instruction the syntax is identical in all x86/x86_64 based processors from Intel, AMD, VIA (and god knows who else) starting with the 80386 back in the 80s. It will not work on IBM Power PC or ARM (mobile phone processors), but you won't find a windows implementation on those either ;)
    – Sergey L.
    Aug 9, 2013 at 13:30
  • @user2418897 Calling conventions are processor architecture specific any way. If you use them manually in contrast to have your compiler do it for you then you need to implement different ones for different processor architectures. Luckily x86 and x86_64 share the same calling conventions and the same instruction for it and all hardware vendors that make x86/x86_64 compatible processors implement it.
    – Sergey L.
    Aug 9, 2013 at 13:37
  • Thanks its very helpful :) i have another question(maybe not last), i call in thread "__fastcall convention" and as i understand this conventin is hibrid with arg, 2 first int goes into register and rest into stack. I find table on wikipedia with some call convention and there was some convention where all (fun arg) goes into stack, can you show me how to call function ADD with another convention (where onvention use stack) Aug 9, 2013 at 13:41
  • @user2418897 added and example with cdecl
    – Sergey L.
    Aug 9, 2013 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.