7

In looking at my disassembled code I see a lot of the following:

00B442E9  push        4  
00B442EB  push        3  
00B442ED  lea         ecx,[ebp-24h]  
00B442F0  call        Foo::Bar (0B41127h)  

I understand pushing the parameters before the call, but what's the lea doing here?

James McNellis
  • 348,265
  • 75
  • 913
  • 977
J Cooper
  • 16,891
  • 12
  • 65
  • 110

2 Answers2

12

In the thiscall calling convention used by Visual C++ for x86, the this pointer is passed in the ecx register. This lea instruction copies the this pointer into the ecx register before calling the member function.

You can read all about the lea instruction in the Stack Overflow question "What's the purpose of the LEA instruction?"

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Is there a reason it isn't just pushed onto the stack as well? – J Cooper Aug 04 '11 at 02:37
  • 1
    I do not know. g++ `thiscall` just pushes `this` onto the stack after the last argument. If I had to guess, someone probably did a performance analysis and decided that preemptively enregistering `this` yielded better performance in common use cases. – James McNellis Aug 04 '11 at 02:42
2

I think it's just an optimized form of

mov ecx, ebp
sub ecx, 24h
user541686
  • 205,094
  • 128
  • 528
  • 886