Closed Anding closed 6 years ago
Thank you for a wonderful question!
jl
jumps if: SF <> OFjs
jumps if: SF = 1neg
changes CF (if operand = 0, CF = 0) , OF, SF, ZF, AF, and PF.Usually, js
will do the trick except for one case: if rax
is equal to the smallest signed integer. It is because the range of 64-bit integers is asymmetric: -2^64 ... 2^64 - 1. When rax
is the smallest negative integer, its 2 complement (computed by neg
) is the same and as the result we get stuck in a loop. Let's study these two examples:
label: ; rax = -10
neg rax ; rax = 10, CF, PF, AF set
js label ; OK, no jump
label: ; rax = -2^64
neg rax ; rax = -2^64, CF, PF, SF, OF are set
js label ; `rax` is still negative and won't change its sign.
Now we are stuck performing neg
(which has no effect) and js
till the end of the world.
However, notice, that both SF and OF are set in this case. This is the exact situation when jl
will prevent us from being stuck in a loop.
jl
responds to SF != OF.js
seems to work as well and is a "logical" choice.Is
jl
preferred for any reason?