Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q40 #63

Closed
Anding opened this issue Mar 7, 2018 · 1 comment
Closed

Q40 #63

Anding opened this issue Mar 7, 2018 · 1 comment

Comments

@Anding
Copy link

Anding commented Mar 7, 2018

label:
neg rax
jl label
; here rax holds its absolute value

jl responds to SF != OF. js seems to work as well and is a "logical" choice.

Is jl preferred for any reason?

@sayon
Copy link
Collaborator

sayon commented Mar 11, 2018

Thank you for a wonderful question!

  • jl jumps if: SF <> OF
  • js jumps if: SF = 1
  • neg 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:

Normal case: rax = -10

label:      ; rax = -10
neg rax  ; rax = 10, CF, PF, AF set
js label   ;  OK, no jump

Corner case: rax = -2^64 = 0x80 00 00 00 00 00 00 00

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.

@Anding Anding closed this as completed Mar 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants