x86汇编语言中的CLD和STD是什么? DF是做什么的? [英] What are CLD and STD for in x86 assembly language? What does DF do?
问题描述
嗯,我知道CLD会清除方向标记,而STD会设置方向标记. 但是设置和清除方向标记有什么意义呢?
direction标志用于影响字符串指令偏移指针寄存器的方向.这些说明与带有REP
前缀的重复操作相同. .
(尽管lods
在rep
中不是很有用).
字符串说明如下: MOVS
(将内存复制到内存),STOS
(存储AL/AX/EAX/RAX),SCAS
(扫描字符串),CMPS
(比较字符串)和LODS
(加载字符串).还有ins
/outs
用于在内存和IO端口之间进行复制.这些指令均以字节,字,dword和qword操作数的大小提供.
ECX
为零或其他条件,具体取决于REP
前缀的形式) ,如果标志为1,则指针递减.
例如, movsd
将dword从[ds:esi]
复制到
dword [es:edi] = dword [ds:esi] // 4-byte copy memory to memory
if (DF == 0)
esi += 4;
edi += 4;
else // DF == 1
esi -= 4;
edi -= 4;
fi
使用REP前缀,可以执行ECX次数,现代x86 CPU已优化了快速字符串"微码,该微码使用16字节或32字节内部操作进行复制(或stos
存储).另请参见关于内存带宽和ERMSB功能的此问题与解答. (请注意,只有rep stos
和rep movs
以这种方式进行了优化,而不是repne/repe scas
或cmps
).
well, I know that CLD clears direction flag and STD sets direction flag. but what's the point in setting and clearing direction flag?
The direction flag is used to influence the direction in which string instructions offset pointer registers. These are the same instructions that can be used with the REP
prefix to repeat the operation.
(Although lods
isn't very useful with rep
).
The string instructions are: MOVS
(copy mem to mem), STOS
(store AL/AX/EAX/RAX), SCAS
(scan string), CMPS
(compare string), and LODS
(load string). There's also ins
/outs
for copying between memory and an IO port. Each of these instructions is available in byte, word, dword, and qword operand sizes.
In a nutshell, when the direction flag is 0, the instructions work by incrementing the pointer to the data after every iteration (until ECX
is zero or some other condition, depending on the flavour of the REP
prefix), while if the flag is 1, the pointer is decremented.
For example, movsd
copies a dword from [ds:esi]
to [es:edi]
(or rdi in 64-bit mode), and does this: (See the "Operation" section in the linked ISA reference manual entry extracted from Intel's PDFs)
dword [es:edi] = dword [ds:esi] // 4-byte copy memory to memory
if (DF == 0)
esi += 4;
edi += 4;
else // DF == 1
esi -= 4;
edi -= 4;
fi
With a REP prefix, it does this ECX times, and modern x86 CPUs have optimized "fast strings" microcode that does the copying (or stos
storing) with 16-byte or 32-byte internal operations. See also this Q&A about memory bandwidth and the ERMSB feature. (Note that only rep stos
and rep movs
are optimized this way, not repne/repe scas
or cmps
).
这篇关于x86汇编语言中的CLD和STD是什么? DF是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!