-1

I'm learning assembly and a textbook shows an example of condition control:

enter image description here

I don't understand why x>=y is used in line 3, why not just follow the logic and use x<y (same as original C code)? is any particular reason to !(not) the condition in if statement?

  • 6
    Please show the code as text, not as picture... – piet.t Sep 17 '18 at 13:14
  • I don't think there's a big reason, but I think the author is familiar with the convention that compilers generally optimize `if` branches over `else` branches and the condition negation above effectively implements that (no jump if the original if branch is entered). – Petr Skocik Sep 17 '18 at 13:14
  • Compiler optimizations, probably for some obscure reason jge is faster than jle or something like that. It doesn't really matter. – m0skit0 Sep 17 '18 at 13:14
  • 1
    @m0skit0: `jge` and `jle` have identical performance on all x86 CPUs. The obvious reason is that `if(x){ do stuff; }` is implemented with fall-through to the `do stuff`, or a conditional jump on the inverse condition. – Peter Cordes Sep 17 '18 at 13:23
  • 2
    I cannot see the pictures. – alk Sep 17 '18 at 13:25
  • 1
    The original if/else code has two branches, and the resulting asm has two branches too, and each branch has the same condition trigger. Whether the `<` or `>=` is used is mathematically equal (with the correct-other branch following of course). The compiler has to pick one, and this code doesn't seem to provide any hint which branch will be more "hot", so it's pretty much random in such case, maybe it will keep the order of original source then, i.e. the "if" branch is first (so the conditional jump needs the "else" condition). Or maybe it just did hit that option first in the evaluation tree. – Ped7g Sep 17 '18 at 13:26
  • @Ped7g: Why not make this an answer? It looks like one. Answering in comment is discouraged at SO. – alk Sep 17 '18 at 13:29
  • @PeterCordes Just for the record, I didn't even think once jge is faster than jle (that would be pretty awkward, but hey, x86 is weird). – m0skit0 Sep 17 '18 at 14:31
  • 3
    Downvote because your code is in pictures. I'm going to retract the downvote once you fix that. – fuz Sep 17 '18 at 15:18
  • Please don't post code as an image. It makes it difficult for people to copy and paste your code to try and run it or post answers. –  Sep 18 '18 at 02:09
  • @amjad Your comment flag has been declined. Please read about [how to flag comments](https://meta.stackoverflow.com/questions/373801/when-is-a-comment-hostile-or-unfriendly-educating-newer-users-how-to-flag-comm). –  Sep 18 '18 at 02:10

1 Answers1

3

An if() statement means "skip this if the condition is not true".

If the condition is true, you want execution to fall-through into the if body, and if it's false you want execution to jump over the if body.

Thus, the obvious / literal way to compile an if is with a jcc on the inverse condition, like jnl. (Optimized code could certainly do much better for those highly-related if/else blocks, like subtract and conditional negate.)

If you wanted to use jl, you have to put the if-body out-of-line, maybe after the ret at the end of the function, and then jump back from it.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847