virtual functiona and assembly code
I'm trying to get basic understanding of what is going on "behind the scene"
with virtual methods and VPRT.
In visual studio .net I see this code:
fun(d);
00432416 lea eax,[d]
00432419 push eax
0043241A call fun (42FEB4h)
0043241F add esp,4
I'm interpreting this like:
first place adress of d into register eax
then push contents of eax (adress of d) on stack
call function on adress (42FEB4h) which is probably adress of function f()
then increment stack pointer
Then program flow is moving (I don't know before 0043241F add esp,4 or after it)
in void fun(Base& b)
among other code there are:
b.f(1);
0043130E mov esi,esp
00431310 push 1
00431312 mov eax,dword ptr [b]
00431315 mov edx,dword ptr [eax]
00431317 mov ecx,dword ptr [b]
0043131A call dword ptr [edx]
0043131C cmp esi,esp
0043131E call @ILT+6220(__RTC_CheckEsp) (42E851h)
this means:
place contents of stack pointer in register esi I think it must be adress of d, but that add esp,4 in previous is enigma
push argument 1,
the rest is pretty much confuse for me ptr[b] ... where is VPRT here?
maybe I should see this as:
place adress of b in eax register
place adress (!?!) eax in register edx
again place adress of b only this time in ecx
And then two there are call. I'd like to understand this
so if there are among you guys some C++/assembly master
who can explain this step by step like I tried I'd be gratefull.
Thanks!
And the complete code is:
Code:
#include <iostream>
using namespace std;
class Base
{
int data;
public:
virtual void f(int){}
};
class Derived : public Base
{
int date;
public:
void f(int)
{
cout<<"derived"<<endl;
}
};
void fun(Base& b)
{
b.f(1);
}
int main()
{
Derived d;
fun(d);
}