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?
Asked
Active
Viewed 1.4e+01k times
50
-
5Above (`ja`) is unsigned, Greater (`jg`) is signed. See [**Understanding Carry vs. Overflow conditions/flags** for signed vs. unsigned](http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt) to learn more about how exactly they get set that way by `cmp`, `sub`, `add`, or other instructions. See also [`jcc` in the instruction set reference](http://x86.renejeschke.de/html/file_module_x86_id_146.html). Other links in the [x86 tag wiki](https://stackoverflow.com/tags/x86/info). – Peter Cordes Dec 02 '17 at 05:31
-
Possible duplicate of [Assembly - JG/JNLE/JL/JNGE after CMP](https://stackoverflow.com/questions/9617877/assembly-jg-jnle-jl-jnge-after-cmp) – Ciro Santilli OurBigBook.com Jun 15 '19 at 20:37
-
[Assembly Language: difference between ja and jg?](https://stackoverflow.com/q/7510381) has an 8-bit example pointing out that `0x80` is `128` 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 for `jl` / `jg` and compare less than any number without that bit set. – Peter Cordes Jan 29 '22 at 22:11
2 Answers
65
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).

Peter Cordes
- 328,167
- 45
- 605
- 847

harold
- 61,398
- 6
- 86
- 164
-
Could you give me an example please? does that mean JA ignores the negative sign? – user3157687 Jan 03 '14 at 15:16
-
@user3157687 there is no sign. There are only condition flags. `ja` ignores the sign flag (SF) though. Example incoming.. – harold Jan 03 '14 at 15:17
-
-
1
-
So, if i say for example CMP 5, -6 JA somewhere. it will not jump right? because 6>5 ? – user3157687 Jan 03 '14 at 15:26
-
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. – harold Jan 03 '14 at 15:30
-
@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 Jan 03 '14 at 15:33
-
-
@user3157687 Yes, and it would also jump if greater (-1 is both greater than -6 and above -6) – harold Jan 03 '14 at 16:44
-
@TeeSee [Intel](https://software.intel.com/en-us/articles/intel-sdm), [AMD](http://developer.amd.com/resources/developer-guides-manuals/), they don't say vastly different things, mainly the same things in a different format, though there are some actual differences. – harold Feb 21 '17 at 08:43
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.

Rahimi0151
- 395
- 3
- 11
-
For people like me that don't get what `JA` and `JG` stands for: `JA` - **J**ump **A**bove `JG` - **J**ump **G**reater. – ScienceDiscoverer Aug 29 '23 at 15:18