<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="../style/style.css" />
<title>CMP: Compare Two Operands (x86 Instruction Set Reference)</title>
</head>
<body>
<div class="main_container"><h1>x86 Instruction Set Reference</h1>
<title>CMP: Compare Two Operands (x86 Instruction Set Reference)</title>
<h1>CMP</h1>
<h2>Compare Two Operands</h2>
<object>
<table class="box">
<tr>
<th>Opcode</th>
<th>Mnemonic</th>
<th>Description</th>
</tr>
<tr>
<td class="grid"><code>3C ib</code></td>
<td class="grid"><code>CMP AL, imm8</code></td>
<td class="grid">Compare imm8 with AL.</td>
</tr>
<tr>
<td class="grid"><code>3D iw</code></td>
<td class="grid"><code>CMP AX, imm16</code></td>
<td class="grid">Compare imm16 with AX.</td>
</tr>
<tr>
<td class="grid"><code>3D id</code></td>
<td class="grid"><code>CMP EAX, imm32</code></td>
<td class="grid">Compare imm32 with EAX.</td>
</tr>
<tr>
<td class="grid"><code>80 /7 ib</code></td>
<td class="grid"><code>CMP r/m8, imm8</code></td>
<td class="grid">Compare imm8 with r/m8.</td>
</tr>
<tr>
<td class="grid"><code>81 /7 iw</code></td>
<td class="grid"><code>CMP r/m16, imm16</code></td>
<td class="grid">Compare imm16 with r/m16.</td>
</tr>
<tr>
<td class="grid"><code>81 /7 id</code></td>
<td class="grid"><code>CMP r/m32,imm32</code></td>
<td class="grid">Compare imm32 with r/m32.</td>
</tr>
<tr>
<td class="grid"><code>83 /7 ib</code></td>
<td class="grid"><code>CMP r/m16,imm8</code></td>
<td class="grid">Compare imm8 with r/m16.</td>
</tr>
<tr>
<td class="grid"><code>83 /7 ib</code></td>
<td class="grid"><code>CMP r/m32,imm8</code></td>
<td class="grid">Compare imm8 with r/m32.</td>
</tr>
<tr>
<td class="grid"><code>38 /r</code></td>
<td class="grid"><code>CMP r/m8,r8</code></td>
<td class="grid">Compare r8 with r/m8.</td>
</tr>
<tr>
<td class="grid"><code>39 /r</code></td>
<td class="grid"><code>CMP r/m16,r16</code></td>
<td class="grid">Compare r16 with r/m16.</td>
</tr>
<tr>
<td class="grid"><code>39 /r</code></td>
<td class="grid"><code>CMP r/m32,r32</code></td>
<td class="grid">Compare r32 with r/m32.</td>
</tr>
<tr>
<td class="grid"><code>3A /r</code></td>
<td class="grid"><code>CMP r8,r/m8</code></td>
<td class="grid">Compare r/m8 with r8.</td>
</tr>
<tr>
<td class="grid"><code>3B /r</code></td>
<td class="grid"><code>CMP r16,r/m16</code></td>
<td class="grid">Compare r/m16 with r16.</td>
</tr>
<tr>
<td class="grid"><code>3B /r</code></td>
<td class="grid"><code>CMP r32,r/m32</code></td>
<td class="grid">Compare r/m32 with r32.</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Description</th>
</tr>
<tr>
<td class="instruction_set_reference_box">
<p>Compares the first source operand with the second source operand and sets the status flags in the EFLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction. When an immediate value is used as an operand, it is sign-extended to the length of the first operand.</p>
<p>The CMP instruction is typically used in conjunction with a conditional jump (Jcc), condition move (CMOVcc), or SETcc instruction. The condition codes used by the Jcc, CMOVcc, and SETcc instructions are based on the results of a CMP instruction. Appendix B, EFLAGS Condition Codes, in the IA-32 Intel Architecture Software Developer's Manual, Volume 1, shows the relationship of the status flags and the condition codes.</p>
</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Operation</th>
</tr>
<tr>
<td class="instruction_set_reference_box">
<pre>temp <span class="operator">=</span> Source1 <span class="operator">-</span> SignExtend<span class="operator">(</span>Source2<span class="operator">)</span><span class="operator">;</span>
ModifyStatusFlags<span class="operator">(</span><span class="operator">)</span><span class="operator">;</span> <span class="comment">//Modify status flags in the same manner as the SUB instruction</span>
</pre>
</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Flags affected</th>
</tr>
<tr>
<td class="instruction_set_reference_box">
<p>The CF, OF, SF, ZF, AF, and PF flags are set according to the result.
</p>
</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Protected Mode Exceptions</th>
</tr>
<tr>
<td class="instruction_set_reference_box">
<div>
<table class="operations_table">
<tr><td><code>#GP(0)</code></td><td>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit. If the DS, ES, FS, or GS register contains a null segment selector.</td></tr>
<tr><td><code>#GP(0)</code></td><td>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit. If the DS, ES, FS, or GS register contains a null segment selector.</td></tr>
<tr><td><code>#SS(0)</code></td><td>If a memory operand effective address is outside the SS segment limit.</td></tr>
<tr><td><code>#PF(fault-code)</code></td><td>If a page fault occurs.</td></tr>
</table>
</div>
</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Real-Address Mode Exceptions</th>
</tr>
<tr>
<td class="instruction_set_reference_box">
<div>
<table class="operations_table">
<tr><td><code>#GP</code></td><td>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.</td></tr>
<tr><td><code>#GP</code></td><td>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.</td></tr>
</table>
</div>
</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Virtual-8086 Mode Exceptions</th>
</tr>
<tr>
<td class="instruction_set_reference_box">
<div>
<table class="operations_table">
<tr><td><code>#GP(0)</code></td><td>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.</td></tr>
<tr><td><code>#GP(0)</code></td><td>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.</td></tr>
<tr><td><code>#SS(0)</code></td><td>If a memory operand effective address is outside the SS segment limit.</td></tr>
<tr><td><code>#PF(fault-code)</code></td><td>If a page fault occurs.</td></tr>
</table>
</div>
</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Instruction</th>
<th>Latency</th>
<th>Throughput</th>
<th>Execution Unit</th>
</tr>
<tr><td class="grid"><code>CPUID</code></td><td class="grid">0F3n/0F2n</td><td class="grid">0F3n/0F2n</td><td class="grid">0F2n</td></tr>
<tr><td class="grid"><code>CMP</code></td><td class="grid">1/0.5</td><td class="grid">0.5/0.5</td><td class="grid">ALU</td></tr>
</table>
</object>
</div>
</body>
</html>
