Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


What would RAII look like in assembly?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

Posted 30 December 2011 - 05:42 PM

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...

Sponsor:

#2 japro   Members   -  Reputation: 887

Like
1Likes
Like

Posted 30 December 2011 - 05:47 PM

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.

#3 rdragon1   Crossbones+   -  Reputation: 1200

Like
1Likes
Like

Posted 30 December 2011 - 06:02 PM

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

#4 rdragon1   Crossbones+   -  Reputation: 1200

Like
1Likes
Like

Posted 30 December 2011 - 06:08 PM

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

#5 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

Posted 30 December 2011 - 06:34 PM

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...

#6 SiCrane   Moderators   -  Reputation: 9662

Like
1Likes
Like

Posted 30 December 2011 - 06:55 PM

MSVC will output assembly for you if you use the /FA family of switches. It's been there since at least MSVC 6.

#7 rdragon1   Crossbones+   -  Reputation: 1200

Like
1Likes
Like

Posted 30 December 2011 - 07:05 PM

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.

#8 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

Posted 31 December 2011 - 05:55 AM

Okay, thanks!! I think I started to get the hang of this Posted Image
the hardest part is the beginning...

#9 Codarki   Members   -  Reputation: 462

Like
0Likes
Like

Posted 31 December 2011 - 09:41 AM

And when you turn optimizations on, mostly all simple constructors and destructors gets inlined and optimized away entirely.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS