Direction Flag in x86

user379888 picture user379888 · Apr 30, 2012 · Viewed 6.9k times · Source

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?

Answer

Nathan Fellman picture Nathan Fellman · Apr 30, 2012

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.