What would RAII look like in assembly?

Started by
7 comments, last by Codarki 12 years, 3 months ago
Hi, I know that when a variable comes into existence its constructor will be called, and its destructor will be called upon exiting scope, I just wonder what those processes look like in assembly? I've been thinking like crazy!!
the hardest part is the beginning...
Advertisement
Do you mean how does the assembly look like that the compiler produces? Then you can simply look at it since pretty much any compiler can output assembly.

Or do you mean what it looks like when you are actually writing assembly? In that case there is the issue that assembly doesn't really know types to begin with, also there are no constructors in the "language", whether you initialise data or not is up to you. You just put "stuff" into registers and the instructions you use it with decide how it is interpreted.

Hi, I know that when a variable comes into existence its constructor will be called, and its destructor will be called upon exiting scope, I just wonder what those processes look like in assembly? I've been thinking like crazy!!


It looks like a 'call' or 'branch' instruction to call the constructor at the beginning of scope, and another call to the destructor at the end of the scope. They're just functions.

Of course, there may be optimizations at play that inline the constructor or destructor, so you might not see an actual call/branch/whatever instruction
Using MSVC, this code:



int main()
{
MyClass c( 12.0f );
std::printf("Hello\n");
}


Looks like this in x86 asm:


00CE1260 push ebp
00CE1261 mov ebp,esp
00CE1263 sub esp,44h
00CE1266 push ebx
00CE1267 push esi
00CE1268 push edi
00CE1269 push ecx
00CE126A fld dword ptr [__real@41400000 (0CE4838h)]
00CE1270 fstp dword ptr [esp]
00CE1273 lea ecx,[c]
00CE1276 call MyClass::MyClass (0CE10C3h)
00CE127B push offset string "Hello\n" (0CE4830h)
00CE1280 call dword ptr [__imp__printf (0CE7238h)]
00CE1286 add esp,4
00CE1289 lea ecx,[c]
00CE128C call MyClass::~MyClass (0CE100Fh)
00CE1291 xor eax,eax
00CE1293 pop edi
00CE1294 pop esi
00CE1295 pop ebx
00CE1296 mov esp,ebp
00CE1298 pop ebp
00CE1299 ret


You can see the call to constructor and destructor at 00CE1276 and 00CE128C
Whoa! btw rdragon1, what tools do you use to generate assembly code from C++ code? I mean what version of MSVC? that's cool. Btw thanks for the response tho. I really need to know what it will look like if the code is like this:


void test()
{
ClassA a; //a's ctor called
if(1)
{
ClassB b; //b's ctor called
return;
}
//after this a's dtor will be called
}


I wonder where a's destructor will be called since there's a 'return' before 'a' goes out of scope....
the hardest part is the beginning...
MSVC will output assembly for you if you use the /FA family of switches. It's been there since at least MSVC 6.
I just ran the program in the debugger and switched to the disassembly window.

For your other question, the destructor will be called whenever the object goes out of scope, so the compiler will add another call to the destructor just before that other return.
Okay, thanks!! I think I started to get the hang of this laugh.png
the hardest part is the beginning...
And when you turn optimizations on, mostly all simple constructors and destructors gets inlined and optimized away entirely.

This topic is closed to new replies.

Advertisement