I've compiled the following using Visual Studio C++ 2008 SP1, x64 C++ compiler:
I'm curious, why did compiler add those nop instructions after those calls?
PS1. I would understand that the 2nd and 3rd nops would be to align the code on a 4 byte margin, but the 1st nop breaks that assumption.
PS2. The C++ code that was compiled had no loops or special optimization stuff in it:
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//This makes no sense. I used it to set a debugger breakpoint
::GdiFlush();
srand(::GetTickCount());
}
PS3. Additional Info: First off, thank you everyone for your input.
Here's additional observations:
My first guess was that incremental linking could've had something to do with it. But, the
Releasebuild settings in theVisual Studiofor the project haveincremental linkingoff.This seems to affect
x64builds only. The same code built asx86(orWin32) does not have thosenops, even though instructions used are very similar:
- I tried to build it with a newer linker, and even though the
x64code produced byVS 2013looks somewhat different, it still adds thosenops after somecalls:
- Also
dynamicvsstaticlinking to MFC made no difference on presence of thosenops. This one is built with dynamical linking to MFC dlls withVS 2013:
- Also note that those
nops can appear afternearandfarcalls as well, and they have nothing to do with alignment. Here's a part of the code that I got fromIDAif I step a little bit further on:
As you see, the nop is inserted after a far call that happens to "align" the next lea instruction on the B address! That makes no sense if those were added for alignment only.
- I was originally inclined to believe that since
nearrelativecalls (i.e. those that start withE8) are somewhat faster thanfarcalls (or the ones that start withFF,15in this case)
the linker may try to go with near calls first, and since those are one byte shorter than far calls, if it succeeds, it may pad the remaining space with nops at the end. But then the example (5) above kinda defeats this hypothesis.
So I still don't have a clear answer to this.





