{"id":73,"date":"2006-11-02T12:14:27","date_gmt":"2006-11-02T17:14:27","guid":{"rendered":"http:\/\/www.nynaeve.net\/?p=73"},"modified":"2019-12-13T17:39:49","modified_gmt":"2019-12-13T22:39:49","slug":"win32-calling-conventions-__thiscall-in-assembler","status":"publish","type":"post","link":"http:\/\/www.nynaeve.net\/?p=73","title":{"rendered":"Win32 calling conventions: __thiscall in assembler"},"content":{"rendered":"<p>The final calling convention that I haven&#8217;t gone over in depth is __thiscall.<\/p>\n<p>Unlike the other calling conventions that I have previously discussed, __thiscall is not typically explicitly decorated on functions (and in most cases, you cannot decorate it explicitly).<\/p>\n<p>As the name might imply, __thiscall is used exclusively for functions that have a <em>this<\/em> pointer &#8211; that is, non-static C++ class member functions.  In a non-static C++ class member function, the <em>this<\/em> pointer is passed as a hidden argument.  In Microsoft C++, the hidden argument is the first actual argument to the routine.<\/p>\n<p>When a function is __thiscall, you will typically see the <em>this<\/em> pointer passed in the <em>ecx<\/em> register.  In this respect, __thiscall is rather similar to __fastcall, as the first argument (<em>this<\/em>) is passed via register in <em>ecx<\/em>.  Unlike __fastcall, however, all remaining arguments are passed via the stack; <em>edx<\/em> is not supported as an additional argument register.  Like __fastcall and __stdcall, when using __thiscall, the callee cleans the stack (and not the caller).<\/p>\n<p>In some circumstances, with non-exported, internal-only-to-a-module functions, CL may use <em>ebx<\/em> instead of <em>ecx<\/em> for <em>this<\/em>.  For any exported __thiscall function (or a function whose address escapes a module), the compiler must use <em>ecx<\/em> for <em>this<\/em>, however.<\/p>\n<p>Continuing with the previous examples, consider a function implementation like so:<\/p>\n<pre>class C\r\n{\r\npublic:\r\n\tint c;\r\n\r\n\t__declspec(noinline)\r\n\tint ThiscallFunction1(int a, int b)\r\n\t{\r\n\t\treturn (a + b) * c;\r\n\t}\r\n};<\/pre>\n<p>This function operates the same as the other example functions that we have used, with the exception that &#8216;c&#8217; is a member variable and not a parameter.<\/p>\n<p>The implementation of this function looks like so in assembler:<\/p>\n<pre>\r\nC::ThiscallFunction1 proc near\r\n\r\na= dword ptr  4\r\nb= dword ptr  8\r\n\r\nmov     eax, [esp+8]       ; eax=b\r\nmov     edx, [esp+4]       ; edx=a\r\nadd     eax, edx           ; eax=a+b\r\nimul    eax, [ecx]         ; eax=eax*this->c\r\nretn    8                  ; return eax;\r\nC::ThiscallFunction1 endp\r\n<\/pre>\n<p>Note that <em>[ecx+0]<\/em> is the offset of the member variable &#8216;c&#8217; from <em>this<\/em>.  This function is similar to the __stdcall version, except that instead of being passed as an explicit argument, the &#8216;c&#8217; parameter is implicitly passed as part of the class object <em>this<\/em> and is then referenced off of the <em>this<\/em> pointer.<\/p>\n<p>Consder a call to this function like this in C:<\/p>\n<pre>C* c = new C;\r\nc->c = 3;\r\nc->ThiscallFunction1(1, 2);<\/pre>\n<p>This is actually a bit more complicated than the other examples, because we also have a call to <em>operator new<\/em> to allocate memory for the class object.  In this instance, <em>operator new<\/em> is a __cdecl function that takes a single argument, which is the count in bytes to allocate.  Here, sizeof(class C) is 4 bytes.<\/p>\n<p>In assembler, we can thus expect to see something like this:<\/p>\n<pre>push    4                    ; sizeof(class C)   \r\ncall    operator new         ; allocate a class C object\r\nadd     esp, 4               ; clean stack from new call\r\npush    2                    ; 'a'\r\npush    1                    ; 'b'\r\nmov     ecx, eax             ; (class C* c)'this'\r\nmov     dword ptr [eax], 3   ; c->c = 3\r\ncall    C::ThiscallFunction1 ; Make the call\r\n<\/pre>\n<p>Ignoring the call to <em>operator new<\/em> for the most part, this is relatively what we would expect.  <em>ecx<\/em> is used to pass <em>this<\/em>, and <em>this->c<\/em> is set to 3 before the call to ThiscallFunction1, as we would expect, given the C++ code.<\/p>\n<p>With all of this information, you should have all you need to recognize and identify __thiscall functions. The main takeaways are:<\/p>\n<ul>\n<li><em>ecx<\/em> is used as an argument register, along with the stack, but not <em>edx<\/em>.  This allows you to differentiate between a __fastcall and __thiscall function.<\/li>\n<li>Arguments passed on the stack are cleaned by the caller and not the callee, like __stdcall.<\/li>\n<li>For virtual function calls, look for a <em>vtable<\/em> pointer as the first class member (at offset 0) from <em>this<\/em>.  (For multiple inheritance, things are a bit more complex; I am ignoring this case right now).  Vtable accesses to retrieve a function pointer to call through after loading <em>ecx<\/em> before a function call are a tell-table sign of a __thiscall virtual function call.<\/li>\n<li>For functions whose visibility scope is confined to one module, the compiler sometimes substitutes <em>ebx<\/em> for <em>ecx<\/em> as a volatile argument register for <em>this<\/em>.<\/li>\n<\/ul>\n<p>Note that if you explicitly specify a calling convention on a class member function, the function ceases to be __thiscall and takes on the characteristics of the specified calling convention, passing <em>this<\/em> as the first argument according to the conventions of the requested calling convention.<\/p>\n<p>That&#8217;s all for __thiscall.  Next up in this series is a brief review and table of contents of what we have covered so far with common Win32 x86 calling conventions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The final calling convention that I haven&#8217;t gone over in depth is __thiscall. Unlike the other calling conventions that I have previously discussed, __thiscall is not typically explicitly decorated on functions (and in most cases, you cannot decorate it explicitly). As the name might imply, __thiscall is used exclusively for functions that have a this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,8,5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/73"}],"collection":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":1,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":647,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions\/647"}],"wp:attachment":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}