When disassembling an executable I encountered the cmove
instruction. I've already searched on the Internet but I've only found that it's a conditional move, and if the source and destination are equal a mov
occurs. What I don't understand yet is why I need it, since it doesn't change the operands. What is its purpose?
CMP Instruction It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands. It is used along with the conditional jump instruction for decision making.
cmovl means "perform move if previous comparison resulted in "less than".
The pop instruction removes the 4-byte data element from the top of the hardware-supported stack into the specified operand (i.e. register or memory location). It first moves the 4 bytes located at memory location [SP] into the specified register or memory location, and then increments SP by 4.
( bh is the upper 8 bits of bx , which is itself the lower 16 bits of ebx . So the value you need is the original value shifted left by 8, which we get by adding two hex 0 digits).
Instructions can be defined as the language used to command a computer architecture. The x86 instructions basically tell the processor what to do. They are generally categorized into: These instructions consist of arithmetic and logical operations. Arithmetic operation can be applied on numerical quantities to multiply, add, subtract or divide.
This is a source of considerable confusion for people new to x86 assembly. Note the absence of % before the register names, and the use of square brackets instead of parentheses for the address, and the lack of an l suffix on the instruction.
MASM uses the standard Intel syntax for writing x86 assembly code. The full x86 instruction set is large and complex (Intel's x86 instruction set manuals comprise over 2900 pages), and we do not cover it all in this guide. For example, there is a 16-bit subset of the x86 instruction set. Using the 16-bit programming model can be quite complex.
General-Purpose Instructions - x86 Assembly Language Reference Manual The x86 Assembly Language Reference Manual documents the syntax of the Solaris x86 assembly language. This manual is provided to help experienced assembly language programmers understand disassembled output of Solaris compilers.
The CMOVcc
instructions don't compare the source and destination. They use the flags from a previous comparison (or other operation that sets the flags) which determines if the move should be done or not. (Intel manual)
Example; this copies edx
to ecx
if eax
and ebx
are equal:
cmp eax, ebx cmove ecx, edx
This does the same as:
cmp eax, ebx jne skip mov ecx, edx skip:
The purpose of cmov
is to allow software (in some cases) to avoid a branch.
For example, if you have this code:
cmp eax,ebx jne .l1 mov eax,edx .l1:
..then when a modern CPU sees the jne
branch it will take a guess about whether the branch will be taken or not taken, and then start speculatively executing instructions based on the guess. If the guess is wrong there's a performance penalty, because the CPU has to discard any speculatively executed work and then start fetching and executing the correct path.
For a conditional move (e.g. cmove eax,edx
) the CPU doesn't need to guess which code will be executed and the cost of a mispredicted branch is avoided. However, the CPU can't know if the value in eax
will change or not, which means that later instructions that depend on the results of the conditional move have to wait until the conditional move completes (instead of being speculatively executed with an assumed value and not stalling).
This means that if the branch can be easily predicted a branch can be faster; and if the branch can't be easily predicted the condition move can be faster.
Note that a conditional move is never strictly needed (it can always be done with a branch instead) - it's more like an optional optimization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With