0

I'm currently learning about the FLAGS register in x86 based processors. I understand that the DF(Direction Flag) is used for deciding if string operation should occur backwards or forwards. Either way the operation goes, it's still going to end up with the same result. Sources, Direction Flag in x86 and https://pdos.csail.mit.edu/6.828/2005/readings/i386/s02_03.htm. Here in this first source people had explanation of how the DF flag worked but the applications where very vague.

My question is what is the application of this feature? I know how it works but I can't really figure out its uses.

  • 4
    It's not going to end up with the same result for obvious reasons. As for applications, you can scan for a letter in a string from the end back (`strrchr`), or you can implement `memmove` for overlapping regions. – Jester Aug 19 '21 at 19:22
  • 3
    `esi -= 1` is different from `esi += 1`. It's only the same result if you start with a pointer to the end, and there's only one match position, if we're talking about `rep scasb` / `rep cmpsb` or a search loop. https://www.felixcloutier.com/x86/scas:scasb:scasw:scasd. – Peter Cordes Aug 19 '21 at 19:23
  • 2
    Note that the only fast "string" operations are `rep movs` and `rep scas` with DF=0. Everything else (including `rep scasb` always, and anything with DF=1) is slow, not optimized in microcode. So while it's interesting to see the original design of DF, you pretty much never want to change it. – Peter Cordes Aug 19 '21 at 19:26
  • This "why" question is in the context of searching for an application for the DF flag. – Daniel Catalano Aug 20 '21 at 21:47
  • So is the DF flag a vestige feature of the x86 ISA? A remnant of the ISA that is zeroed out and never used in modern times? @Peter Cordes – Daniel Catalano Aug 20 '21 at 21:55
  • 1
    By compiler-generated code, and most hand-written code, yes. You could consider using it if optimizing for code-size over speed, if it actually saves space, but that's rare. It's usually easier to just leave it at 0. – Peter Cordes Aug 20 '21 at 22:19
  • 1
    Re: the title question: probably your best bet would be to check [Stephen Morse](https://en.wikipedia.org/wiki/Stephen_P._Morse)'s book on how/why he designed 8086 that way, https://www.stevemorse.org/8086/index.html. He was the primary architect of the 8086 ISA. (Other engineers worked on the actual HW implementation, but reportedly he more or less single-handedly designed the instructions and their machine-encodings). And yes, @old_timer, he's still alive, although IDK if he's interested in answering questions like this, apparently genealogy is his primary hobby these days. – Peter Cordes Aug 20 '21 at 22:23
  • 1
    Hmm, The 8086 Primer seems to be more of a guide to how the ISA works, at least in chapter 3, less about *why* it was designed that way. Except what you can infer from how Morse chooses to describe instructions like LEA and their purpose. (And in your case, overlapping src/dst is the example case for having DF for `rep movs`, pg68 .. 69). Although there are some direct statements about design intent, like that `stos` and `lods` weren't "intended" to be used with `rep`, although he does say `rep stos` has uses! It's neat that a scanned copy of the book is free on his own web site. – Peter Cordes Aug 20 '21 at 22:34
  • Thank you soooo much for your responses peter! You saved me from hours of banging my head on the wall trying to figure out the application of the DF register! This question is now resolved. @Peter Cordes – Daniel Catalano Aug 21 '21 at 02:10

0 Answers0