I have recently learned that
nop instruction is actually
xchg eax, eax... what it does is basically exchanges
eax with itself.
As far as CPU goes, does the exchange actually happen?
Reverse Engineering Stack Exchange is a question and answer site for researchers and developers who explore the principles of a system through analysis of its structure, function, and operation. It only takes a minute to sign up.Sign up to join this community
There are several instructions, which could be used depending on the compiler.
xchg eax, eax is byte code 90. It is a legit instruction, which takes up a single processing cycle. In addition, there are several other instructions, which could be used in place of
xchg eax, eax:
lea eax, [eax + 0x00] byte code 8D 40 00 mov eax, eax byte code 89 C0
Since all of those instructions are different length, compiler chooses one of the most appropriate versions depending on alignment requirements.
Regarding compilers' choices, a few pointers:
The short answer is "Yes." In fact, if you experiment by generating machine language op codes directly you will discover that there is a whole range of operations that are effectively NOPs, all of which take a single processor cycle to execute.
While they are not technically "Documented," you will find that very close to the 0x90,
XCHG EAX, EAX
XCHG EBX, EBX
XCHG ECX, ECX
XCHG EDX, EDX