0

Is there a jmp command that jumps if the ZF flag or the SF flat is set? Basically I would like to do something like the following:

sub $NUMBER,  %eax
jlez

The equivalent of:

while (i - num >= 0) {
    //
}
carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • You may want to use `cmp` instead of `sub`. after `cmp` use `jle` if `i` is signed and `jbe` is `i` is unsigned. – W. Chang Sep 13 '20 at 04:30
  • Note that assemblers typically offer synonyms that are the exact same opcode - e.g. `jle` (jump if less than or equal) and `jng` (jump if not greater ). This is useful to make the code reflect the author's thoughts; but can confuse people using disassemblers (because a disassembler can't know which synonym it should use and will use a "possibly different' default). – Brendan Sep 13 '20 at 04:44
  • 1
    The page describing `Jcc` in the Intel SDM describes all the variants of the instruction and which flags are checked. – prl Sep 13 '20 at 05:05
  • 2
    Note that just checking SF is insufficient. To properly check for less-than, it has to check OF, too. – prl Sep 13 '20 at 05:06
  • @prl: The OP wants to branch on the subtraction result compared *against zero*, not on the inputs to the subtraction compared to each other. As you say, that's different if signed overflow is possible, and is why you can just use `jge` or `jle` on FLAGS set by `sub`. Note that [comparing against zero can't ever set OF](https://stackoverflow.com/questions/33721204/test-whether-a-register-is-zero-with-cmp-reg-0-vs-or-reg-reg/33724806#33724806), so just checking SF is sufficient. – Peter Cordes Sep 13 '20 at 10:39
  • So for example, if `$NUMBER` is `2000000000` and `%eax` is `-2000000000`, the subtraction will overflow and the result will be `294967296`, which is positive. To be clear, you want to *not* take the jump in that case? There doesn't seem to be any [instruction](https://c9x.me/x86/html/file_module_x86_id_146.html) like that. But you could do two instructions, `jz label; js label` which would have the effect of branching to `label` when either flag is set. – Nate Eldredge Sep 13 '20 at 16:48
  • By the way, I don't see how your C code is equivalent; it's either backwards or behaves differently on equality to zero, depending on whether you're imagining a jump past the loop body (take jump if condition false) or back to the top (take jump if condition true). Could you clarify? Don't forget the sign flag is set when the result is *negative*. – Nate Eldredge Sep 13 '20 at 16:51

0 Answers0