Difference between JA and JG in assembly
Categories:
JA vs. JG in x86 Assembly: Understanding Conditional Jumps

Explore the crucial differences between the JA (Jump if Above) and JG (Jump if Greater) instructions in x86 assembly, and learn when to use each for effective conditional logic.
In x86 assembly language, conditional jump instructions are fundamental for controlling program flow based on the results of previous operations. Among these, JA
(Jump if Above) and JG
(Jump if Greater) are often confused due to their similar-sounding names. However, their distinction is critical and lies in how they interpret the values being compared: as unsigned or signed integers, respectively. Understanding this difference is paramount for writing correct and robust assembly code.
The Role of Flags in Conditional Jumps
Before diving into JA
and JG
, it's essential to understand the CPU's flag register. After an arithmetic or logical operation (like CMP
or SUB
), the CPU sets various status flags based on the result. These flags include:
- ZF (Zero Flag): Set if the result is zero.
- CF (Carry Flag): Set if an unsigned overflow occurred (e.g., a borrow in subtraction or a carry out of the most significant bit in addition).
- SF (Sign Flag): Set if the most significant bit of the result is 1 (indicating a negative signed number).
- OF (Overflow Flag): Set if a signed overflow occurred.
Conditional jump instructions like JA
and JG
inspect combinations of these flags to determine whether to transfer control to a different part of the program.
flowchart TD A[Operation (e.g., CMP)] --> B{Flags Set?} B -->|ZF=1| C[Result is Zero] B -->|CF=1| D[Unsigned Carry/Borrow] B -->|SF=1| E[Signed Result is Negative] B -->|OF=1| F[Signed Overflow] C & D & E & F --> G[Conditional Jump Instructions] G --> H{"JA" (Unsigned)} G --> I{"JG" (Signed)} H --> J[Jump if CF=0 and ZF=0] I --> K[Jump if SF=OF and ZF=0] J & K --> L[Program Flow Changes]
How CPU Flags Influence Conditional Jumps
JA (Jump if Above): Unsigned Comparison
The JA
instruction (Jump if Above) is used for comparing unsigned integers. It performs a jump if the first operand is strictly greater than the second operand, considering both as non-negative values. This means it checks if there was no carry (CF=0) and the result was not zero (ZF=0) after a comparison (typically CMP
).
Condition for JA: CF = 0
AND ZF = 0
Think of JA
as comparing magnitudes without regard for sign. For example, if you compare 0xFF
(255 decimal) with 0x01
(1 decimal), 0xFF
is 'above' 0x01
in an unsigned context.
MOV EAX, 0xFFFFFFFF ; EAX = 4,294,967,295 (unsigned)
MOV EBX, 0x00000001 ; EBX = 1 (unsigned)
CMP EAX, EBX ; Compare EAX with EBX
JA IsAbove ; Jump if EAX > EBX (unsigned)
; This jump WILL be taken because 0xFFFFFFFF is above 0x00000001 unsignedly.
JMP EndProgram
IsAbove:
; Code executes if EAX is above EBX (unsigned)
; ...
EndProgram:
; ...
Example of JA (Jump if Above) for unsigned comparison
JG (Jump if Greater): Signed Comparison
The JG
instruction (Jump if Greater) is used for comparing signed integers. It performs a jump if the first operand is strictly greater than the second operand, taking into account the sign bit. This involves checking the Sign Flag (SF) and the Overflow Flag (OF), along with the Zero Flag (ZF).
Condition for JG: (SF = OF)
AND ZF = 0
In signed arithmetic, 0xFFFFFFFF
represents -1, and 0x00000001
represents 1. In this context, -1 is not greater than 1. JG
correctly handles negative numbers and potential signed overflows during comparison.
MOV EAX, 0xFFFFFFFF ; EAX = -1 (signed)
MOV EBX, 0x00000001 ; EBX = 1 (signed)
CMP EAX, EBX ; Compare EAX with EBX
JG IsGreater ; Jump if EAX > EBX (signed)
; This jump WILL NOT be taken because -1 is NOT greater than 1 signedly.
JMP EndProgram
IsGreater:
; Code executes if EAX is greater than EBX (signed)
; ...
EndProgram:
; ...
Example of JG (Jump if Greater) for signed comparison
JA
is equivalent to JNBE
(Jump if Not Below or Equal), and JG
is equivalent to JNLE
(Jump if Not Less or Equal).Summary of Differences and Usage
The choice between JA
and JG
hinges entirely on whether the data you are comparing should be interpreted as unsigned or signed. Misusing them can lead to subtle and hard-to-debug logical errors in your assembly programs.
- Use
JA
(Jump if Above) when dealing with memory addresses, sizes, counts, or any data where negative values are not meaningful, and the entire bit pattern represents a positive magnitude. - Use
JG
(Jump if Greater) when dealing with mathematical quantities that can be positive or negative, such as temperatures, financial values, or general arithmetic results.

Key Differences Between JA and JG
Understanding these nuances is crucial for writing correct and efficient assembly code. Always consider the nature of your data (signed vs. unsigned) before choosing your conditional jump instruction.