x86汇编语言中的CLD和STD是什么? DF是做什么的? [英] What are CLD and STD for in x86 assembly language? What does DF do?

查看:579
本文介绍了x86汇编语言中的CLD和STD是什么? DF是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我知道CLD会清除方向标记,而STD会设置方向标记. 但是设置和清除方向标记有什么意义呢?

解决方案

direction标志用于影响字符串指令偏移指针寄存器的方向.这些说明与带有REP前缀的重复操作相同. . (尽管lodsrep中不是很有用).

字符串说明如下: MOVS (将内存复制到内存),STOS(存储AL/AX/EAX/RAX),SCAS(扫描字符串),CMPS(比较字符串)和LODS(加载字符串).还有ins/outs用于在内存和IO端口之间进行复制.这些指令均以字节,字,dword和qword操作数的大小提供.

简而言之,当方向标志为0时,指令将在每次迭代后通过将指向数据的指针递增来工作(直到ECX为零或其他条件,具体取决于REP前缀的形式) ,如果标志为1,则指针递减.

例如, movsd 将dword从[ds:esi]复制到(或在64位模式下为rdi),然后执行以下操作:(请参阅从Intel的PDF摘录的链接的ISA参考手册中的操作"部分)

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 stosrep movs以这种方式进行了优化,而不是repne/repe scascmps).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆