Saturday 15 March 2014

visual studio 2010 - Calling C++ function from assembly, with struct argument -


I am getting some unexpected behavior which probably means that I can not fully understand what the compiler is doing Consider the following indexed programs:

  #include & lt; Stdio.h & gt; #pragma pack (push, 1) structure A {unsigned minor A; Unsigned minor b; Clear A () {printf ("create \ n"); } ~ A () {printf ("destroyed \ n"); }}; #pragma pack (pop) static_assert (sizeof (a) == 4, "sizeof (A)! = 4"); A __studycol F (int p1, ap2, int p3, int p4) {print ("% 08X% 08X% 08X% 08X \ n", P1, P2, P3, P4); Return P2; } Int main () {__asm ​​{push 4 push 3 push 2 push 1 call F} return 0; } The above program will crash, but if I change the  A ()  and  ~ A ()  to  Structure A < / Code> This issue will not be related to the compilation where the compiler thinks that the logic is on the stack, defined by the constructor, defines that they are 4 bytes from where they are. If I remove the constructor, then I get it:  

00000001 00000002 00000003 00000004

Which I expected, though with the consultants I defined it

00000002 00000003 00000004 00000000

Obviously what do I expect when the task is on, then RETN 0x10 and Returns the function with RETN 0x14 , so it seems that it looks like there should be another parameter (why?). I have seen that if I change f to a zero function, then it works as expected, can someone tell me what is happening and Why? I have all the customizations closed.

"post-text" itemprop = "text">

At the casual level, only a simple value can be returned from the function, so if a more complex object Back up is needed, so the compiler will treat it as if you are passing a pointer on the returned object:

  zero f (A * return_ptr, int p1, a p2, int p3 , Int p4);   

Some customizations can however be done. In your example, your class includes two 16-bit shorts, and those 16-bit shorts can be packed in a 32-bit integer and can return to a register. However, if you define a custom constructor, then the class is now considered simple enough for this optimization.

No comments:

Post a Comment