Direction Flag in x86

孤街醉人 提交于 2019-12-18 05:59:10

问题


I am unable to understand how does the direction flag work in x86. The text in my lectures say that it increments or decrements the source or destination register but that does not make sense with its name. Can someone explain what it does?


回答1:


This flag is used in string operations, and specifies if strings begin at a low address and proceed to higher addresses or vice versa.

For string instructions, ECX has the number of iterations, DS:ESI has the source address and ES:EDI has the destination (hence the s in ESI and the d in EDI).

After each iteration, ECX is decremented by one, and ESI and EDI are either incremented or decremented by the element size (1 for byte operations, 2 for word operations etc) according to EFLAGS.DF.

If EFLAGS.DF is 0, ESI and EDI are incremented, otherwise they're decremented.




回答2:


Let's take rep movsb as an example of an instruction that depends on the direction flag.

When you do a rep movsb, you supply a source address in esi, a destination address in edi, and count in ecx. The processor basically executes a loop. In the normal case (when the direction flag is clear) it increments esi and edi each iteration of the loop, so you initialize them to point to the beginning of the source and destination blocks you're copying. While executing the REP MOVSB, the processor increments the source and destination addresses until it reaches the end of the block being copied.

When the direction flag is set, the processor decrements the registers instead. This means you need to start with them pointing to the end of the memory block you're copying. Instead of starting from the beginning and copying to the end, it starts at the end and copies backward until it gets to the beginning.



来源:https://stackoverflow.com/questions/10380076/direction-flag-in-x86

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!