Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:34:54+00:00 2026-06-10T22:34:54+00:00

According to the MSDN documentation the this pointer is stored in ECX when using

  • 0

According to the MSDN documentation the “this” pointer is stored in ECX when using the default __thiscall calling convention for class functions. Despite this certainly being the case when translating regular C++ code I have encountered a problem when trying to access “this” with inline assembly.

Here’s the test program:

#include <cstdio>

class TestClass
{
    long x;

    public:
        inline TestClass(long x):x(x){}

    public:
        inline long getX1(){return x;}
        inline long getX2()
        {
            _asm
            {
                mov eax,dword ptr[ecx]
            }
        }
};
int main()
{
    TestClass c(42);

    printf("c.getX1() = %d\n",c.getX1());
    printf("c.getX2() = %d\n",c.getX2());

    return 0;
}

The two Get functions are translated like this:

?getX1@TestClass@@QAEJXZ (public: long __thiscall TestClass::getX1(void)):
  00000000: 8B 01              mov         eax,dword ptr [ecx]
  00000002: C3                 ret

?getX2@TestClass@@QAEJXZ (public: long __thiscall TestClass::getX2(void)):
  00000000: 8B 01              mov         eax,dword ptr [ecx]
  00000002: C3                 ret

I think it’s safe to say that these two functions are identical. Nevertheless, here’s the output from the program:

c.getX1() = 42
c.getX2() = 1

Obviously “this” is not stored in ECX when the second Get function is invoked, so my question is: How do I ensure that class functions containing inline assembly follow the calling convention and/or are invoked the same way as regular/non-inlined functions?

EDIT: The main function is translated like this:

_main:
  00000000: 51                 push        ecx
  00000001: 6A 2A              push        2Ah
  00000003: 68 00 00 00 00     push        offset $SG3948
  00000008: E8 00 00 00 00     call        _printf
  0000000D: 83 C4 08           add         esp,8
  00000010: 8D 0C 24           lea         ecx,[esp]
  00000013: E8 00 00 00 00     call        ?getX2@TestClass@@QAEJXZ
  00000018: 50                 push        eax
  00000019: 68 00 00 00 00     push        offset $SG3949
  0000001E: E8 00 00 00 00     call        _printf
  00000023: 33 C0              xor         eax,eax
  00000025: 83 C4 0C           add         esp,0Ch
  00000028: C3                 ret
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:34:55+00:00Added an answer on June 10, 2026 at 10:34 pm

    This is how I managed to make the function work corretly (ie. pass “this” in ECX):

    testclass.hpp:

    class TestClass
    {
        long x;
    
        public:
            inline TestClass(long x):x(x){}
    
        public:
            long getX1();
            long getX2();
    };
    

    testclass.cpp:

    #include "testclass.hpp"
    
    long TestClass::getX1()
    {
        return x;
    }
    long TestClass::getX2()
    {
        _asm
        {
            mov eax,dword ptr[ecx]
        }
    }
    

    testmain.cpp:

    #include <cstdio>
    #include "testclass.hpp"
    
    int main()
    {
        TestClass c(42);
    
        printf("c.getX1() = %d\n",c.getX1());
        printf("c.getX2() = %d\n",c.getX2());
    
        return 0;
    }
    

    Output:

    c.getX1() = 42
    c.getX2() = 42
    

    The problem is that inlined class functions in MSVC 2010 not necessarily follow the calling conventions specified by MSDN. I don’t think this is a bug, but you should at least be aware of it if you are planning to use inline assembly in inlined functions. My advice is that you don’t do it. If you need inline assembly in a class function keep the declaration and implementation separated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQLCMD uses windows authentication by default. According to the MSDN documentation , you can
I'm using ICommandText::GetCommandText method. According to the MSDN documentation ( http://msdn.microsoft.com/en-us/library/ms709825(v=VS.85).aspx ) I need
According to the MSDN documentation on the List<T>.Clear method : This method is an
According to the MSDN documentation, by default the FileExtensionsAttribute (.NET 4.5) should allow me
According to MSDN documentation this proc is supposed to grant login to a windows
According to MSDN documentation for RandomNumberGenerator : Application code does not directly use this
According to MSDN Documentation for partial classes : Partial methods are implicitly private So
According to the HttpResponseMessage documentation on MSDN, the reason phrase (as in, the OK
According to the MSDN documentation for the WindowStartupLocation Property : Setting CenterScreen causes a
According to the msdn documentation : GetPictureFromObject method Returns a picture (Visual Basic Picture

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.