Difference between JA and JG in assembly

115,450

Solution 1

As Intel's manual explains, JG interprets the flags as though the comparison was signed, and JA interprets the flags as though the comparison was unsigned (of course if the operation that set the flags was not a comparison or subtraction, that may not make sense). So yes, they're different. To be precise,

  • ja jumps if CF = 0 and ZF = 0 (unsigned Above: no carry and not equal)
  • jg jumps if SF = OF and ZF = 0 (signed Greater, excluding equal)

For example,

cmp eax, edx
ja somewhere ; will go "somewhere" if eax >u edx
             ; where >u is "unsigned greater than"

cmp eax, edx
jg somewhere ; will go "somewhere" if eax >s edx
             ; where >s is "signed greater than"

>u and >s agree for values with the top bit zero, but values with the top bit set are treated as negative by >s and as big by >u (of course if both operands have the top bit set, >u and >s agree again).

Solution 2

JA is used for jumping if the last "flag changing" instruction was on unsigned numbers. but on the other hand, JG is used for jumping if the last "flag changing" instruction was on signed numbers.

Share:
115,450

Related videos on Youtube

user3157687
Author by

user3157687

Updated on July 09, 2022

Comments

  • user3157687
    user3157687 almost 2 years

    Can you please tell me the difference between JUMP IF ABOVE AND JUMP IF GREATER in Assembly language? when do i use each of them? do they give me different results?

  • user3157687
    user3157687 over 10 years
    Could you give me an example please? does that mean JA ignores the negative sign?
  • harold
    harold over 10 years
    @user3157687 there is no sign. There are only condition flags. ja ignores the sign flag (SF) though. Example incoming..
  • user3157687
    user3157687 over 10 years
    sorry but what do you mean by u, s ?:)
  • harold
    harold over 10 years
    @user3157687 unsigned and signed
  • user3157687
    user3157687 over 10 years
    So, if i say for example CMP 5, -6 JA somewhere. it will not jump right? because 6>5 ?
  • harold
    harold over 10 years
    @user3157687 no, if you have cmp 5, -6 \ ja somewhere (ignore the syntax error), it will not jump (in that you are right), but the reason is that -6 (aka 0xfffffffa) is much bigger than 5, not that 6 is bigger than 5. cmp 5, -1 wouldn't jump either, -1 is even bigger than -6. "Unsigned bigger", of course.
  • harold
    harold over 10 years
    @user3157687 well maybe you could look at it like that, but really you should just look at 1) how it interprets the flags, and 2) what those flags mean in context
  • harold
    harold over 10 years
    Yes, 4 is clearly bigger than 2, signedness doesn't even matter
  • harold
    harold over 10 years
    @user3157687 Yes, and it would also jump if greater (-1 is both greater than -6 and above -6)
  • harold
    harold over 7 years
    @TeeSee Intel, AMD, they don't say vastly different things, mainly the same things in a different format, though there are some actual differences.