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?
2 Answers
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).
-
Could you give me an example please? does that mean JA ignores the negative sign? Jan 3, 2014 at 15:16
-
@user3157687 there is no sign. There are only condition flags.
ja
ignores the sign flag (SF) though. Example incoming..– haroldJan 3, 2014 at 15:17 -
-
1
-
1@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.– haroldJan 3, 2014 at 15:30
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.
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.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.