Difference between JA and JG in assembly
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 ifCF = 0
andZF = 0
(unsigned Above: no carry and not equal) -
jg
jumps ifSF = OF
andZF = 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.
Related videos on Youtube
user3157687
Updated on July 09, 2022Comments
-
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?
-
Peter Cordes over 6 yearsAbove (
ja
) is unsigned, Greater (jg
) is signed. See Understanding Carry vs. Overflow conditions/flags for signed vs. unsigned to learn more about how exactly they get set that way bycmp
,sub
,add
, or other instructions. See alsojcc
in the instruction set reference. Other links in the x86 tag wiki. -
Ciro Santilli OurBigBook.com about 5 yearsPossible duplicate of Assembly - JG/JNLE/JL/JNGE after CMP
-
Peter Cordes over 2 yearsAssembly Language: difference between ja and jg? has an 8-bit example pointing out that
0x80
is128
as unsigned, and-128
as signed. Good dup target for questions where the problem is not realizing that numbers with their high bit set are negative forjl
/jg
and compare less than any number without that bit set.
-
-
user3157687 over 10 yearsCould you give me an example please? does that mean JA ignores the negative sign?
-
harold over 10 years@user3157687 there is no sign. There are only condition flags.
ja
ignores the sign flag (SF) though. Example incoming.. -
user3157687 over 10 yearssorry but what do you mean by u, s ?:)
-
harold over 10 years@user3157687 unsigned and signed
-
user3157687 over 10 yearsSo, if i say for example CMP 5, -6 JA somewhere. it will not jump right? because 6>5 ?
-
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 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 over 10 yearsYes, 4 is clearly bigger than 2, signedness doesn't even matter
-
harold over 10 years@user3157687 Yes, and it would also jump if greater (-1 is both greater than -6 and above -6)
-
harold over 7 years