<?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>MASKMOVQ: Store Selected Bytes of Quadword (x86 Instruction Set Reference)</title>
</head>
<body>
<div class="main_container"><h1>x86 Instruction Set Reference</h1>
<title>MASKMOVQ: Store Selected Bytes of Quadword (x86 Instruction Set Reference)</title>
<h1>MASKMOVQ</h1>
<h2>Store Selected Bytes of Quadword</h2>
<object>
<table class="box">
<tr>
<th>Opcode</th>
<th>Mnemonic</th>
<th>Description</th>
</tr>
<tr>
<td class="grid"><code>0F F7 /r</code></td>
<td class="grid"><code>MASKMOVQ mm1, mm2</code></td>
<td class="grid">Selectively write bytes from mm1 to memory location using the byte mask in mm2.</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Description</th>
</tr>
<tr>
<td class="instruction_set_reference_box">
<p>Stores selected bytes from the source operand (first operand) into a 64-bit memory location. The mask operand (second operand) selects which bytes from the source operand are written to memory. The source and mask operands are MMX technology registers. The location of the first byte of the memory location is specified by DI/EDI and DS registers. (The size of the store address depends on the address-size attribute.) The most significant bit in each byte of the mask operand determines whether the corresponding byte in the source operand is written to the corresponding byte location in memory: 0 indicates no write and 1 indicates write.</p>
<p>The MASKMOVQ instruction generates a non-temporal hint to the processor to minimize cache pollution. The non-temporal hint is implemented by using a write combining (WC) memory type protocol (see &quot;Caching of Temporal vs. Non-Temporal Data&quot; in Chapter 10, of the IA-32 Intel Architecture Software Developer's Manual, Volume 1). Because the WC protocol uses a weakly-ordered memory consistency model, a fencing operation implemented with the SFENCE or MFENCE instruction should be used in conjunction with MASKMOVEDQU instructions if multiple processors might use different memory types to read/write the destination memory locations.</p>
<p>This instruction causes a transition from x87 FPU to MMX technology state (that is, the x87 FPU top-of-stack pointer is set to 0 and the x87 FPU tag word is set to all 0s [valid]).</p>
<p>The behavior of the MASKMOVQ instruction with a mask of all 0s is as follows:</p>
<ul>
<li>No data will be written to memory.</li>
<li>Transition from x87 FPU to MMX technology state will occur.</li>
<li>Exceptions associated with addressing memory and page faults may still be signaled (implementation dependent).</li>
<li>Signaling of breakpoints (code or data) is not guaranteed (implementation dependent).</li>
<li>If the destination memory region is mapped as UC or WP, enforcement of associated semantics for these memory types is not guaranteed (that is, is reserved) and is implementation- specific.</li>
</ul>
<p>The MASKMOVQ instruction can be used to improve performance for algorithms that need to merge data on a byte-by-byte basis. It should not cause a read for ownership; doing so generates unnecessary bandwidth since data is to be written directly using the byte-mask without allocating old data prior to the store.</p>
</td>
</tr>
</table>
</object>
<object>
<table class="box">
<tr>
<th>Operation</th>
</tr>
<tr>
<td class="instruction_set_reference_box">
<pre><span class="keyword">if</span><span class="operator">(</span>Mask<span class="operator">[</span><span class="number">7</span><span class="operator">]</span> <span class="operator">==</span> <span class="number">1</span><span class="operator">)</span> Destination<span class="operator">[</span><span class="operator">(</span>E<span class="operator">)</span>DI<span class="operator">]</span> <span class="operator">=</span> Source<span class="operator">[</span><span class="number">0..7</span><span class="operator">]</span><span class="operator">;</span>
<span class="keyword">if</span><span class="operator">(</span>Mask<span class="operator">[</span><span class="number">15</span><span class="operator">]</span> <span class="operator">==</span> <span class="number">1</span><span class="operator">)</span> Destination<span class="operator">[</span><span class="operator">(</span>E<span class="operator">)</span>DI <span class="operator">+</span> <span class="number">1</span><span class="operator">]</span> <span class="operator">=</span> Source<span class="operator">[</span><span class="number">8..15</span><span class="operator">]</span><span class="operator">;</span>
<span class="keyword">if</span><span class="operator">(</span>Mask<span class="operator">[</span><span class="number">23</span><span class="operator">]</span> <span class="operator">==</span> <span class="number">1</span><span class="operator">)</span> Destination<span class="operator">[</span><span class="operator">(</span>E<span class="operator">)</span>DI <span class="operator">+</span> <span class="number">2</span><span class="operator">]</span> <span class="operator">=</span> Source<span class="operator">[</span><span class="number">16..23</span><span class="operator">]</span><span class="operator">;</span>
<span class="keyword">if</span><span class="operator">(</span>Mask<span class="operator">[</span><span class="number">31</span><span class="operator">]</span> <span class="operator">==</span> <span class="number">1</span><span class="operator">)</span> Destination<span class="operator">[</span><span class="operator">(</span>E<span class="operator">)</span>DI <span class="operator">+</span> <span class="number">3</span><span class="operator">]</span> <span class="operator">=</span> Source<span class="operator">[</span><span class="number">24..31</span><span class="operator">]</span><span class="operator">;</span>
<span class="keyword">if</span><span class="operator">(</span>Mask<span class="operator">[</span><span class="number">39</span><span class="operator">]</span> <span class="operator">==</span> <span class="number">1</span><span class="operator">)</span> Destination<span class="operator">[</span><span class="operator">(</span>E<span class="operator">)</span>DI <span class="operator">+</span> <span class="number">4</span><span class="operator">]</span> <span class="operator">=</span> Source<span class="operator">[</span><span class="number">32..39</span><span class="operator">]</span><span class="operator">;</span>
<span class="keyword">if</span><span class="operator">(</span>Mask<span class="operator">[</span><span class="number">47</span><span class="operator">]</span> <span class="operator">==</span> <span class="number">1</span><span class="operator">)</span> Destination<span class="operator">[</span><span class="operator">(</span>E<span class="operator">)</span>DI <span class="operator">+</span> <span class="number">5</span><span class="operator">]</span> <span class="operator">=</span> Source<span class="operator">[</span><span class="number">40..47</span><span class="operator">]</span><span class="operator">;</span>
<span class="keyword">if</span><span class="operator">(</span>Mask<span class="operator">[</span><span class="number">55</span><span class="operator">]</span> <span class="operator">==</span> <span class="number">1</span><span class="operator">)</span> Destination<span class="operator">[</span><span class="operator">(</span>E<span class="operator">)</span>DI <span class="operator">+</span> <span class="number">6</span><span class="operator">]</span> <span class="operator">=</span> Source<span class="operator">[</span><span class="number">48..55</span><span class="operator">]</span><span class="operator">;</span>
<span class="keyword">if</span><span class="operator">(</span>Mask<span class="operator">[</span><span class="number">63</span><span class="operator">]</span> <span class="operator">==</span> <span class="number">1</span><span class="operator">)</span> Destination<span class="operator">[</span><span class="operator">(</span>E<span class="operator">)</span>DI <span class="operator">+</span> <span class="number">7</span><span class="operator">]</span> <span class="operator">=</span> Source<span class="operator">[</span><span class="number">56..63</span><span class="operator">]</span><span class="operator">;</span>
</pre>
</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>For an illegal memory operand effective address in the CS, DS, ES, FS or GS segments. (even if mask is all 0s). If the destination operand is in a nonwritable segment. If the DS, ES, FS, or GS register contains a null segment selector.</td></tr>
<tr><td><code>#GP(0)</code></td><td>For an illegal memory operand effective address in the CS, DS, ES, FS or GS segments. (even if mask is all 0s). If the destination operand is in a nonwritable segment. If the DS, ES, FS, or GS register contains a null segment selector.</td></tr>
<tr><td><code>#SS(0)</code></td><td>For an illegal address in the SS segment (even if mask is all 0s).</td></tr>
<tr><td><code>#PF(fault-code)</code></td><td>For a page fault (implementation specific).</td></tr>
<tr><td><code>#NM</code></td><td>If TS in CR0 is set.</td></tr>
<tr><td><code>#MF</code></td><td>If there is a pending FPU exception.</td></tr>
<tr><td><code>#UD</code></td><td>If EM in CR0 is set. If OSFXSR in CR4 is 0. If CPUID feature flag SSE is 0. If Mod field of the ModR/M byte not 11B</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(0)</code></td><td>If any part of the operand lies outside the effective address space from 0 to FFFFH. (even if mask is all 0s).</td></tr>
<tr><td><code>#GP(0)</code></td><td>If any part of the operand lies outside the effective address space from 0 to FFFFH. (even if mask is all 0s).</td></tr>
<tr><td><code>#NM</code></td><td>If TS in CR0 is set.</td></tr>
<tr><td><code>#MF</code></td><td>If there is a pending FPU exception.</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">
Same exceptions as in Real Address Mode
<div>
<table class="operations_table">
<tr><td><code>#PF(fault-code)</code></td><td>For a page fault (implementation specific).</td></tr>
<tr><td><code>#PF(fault-code)</code></td><td>For a page fault (implementation specific).</td></tr>
</table>
</div>
</td>
</tr>
</table>
</object>
</div>
</body>
</html>
