Every loop, every branch and every repeated action in an 8086 program is built from a small set of control-flow instructions. JMP goes somewhere unconditionally. The conditional jumps go somewhere only if the flags say so. LOOP combines a counter and a jump into one instruction.
JMP: the unconditional jump
JMP target
JMP transfers control to the label without looking at any flag. It is the instruction for skipping over code, for the end of an if/else chain, and for infinite loops that are exited some other way, such as a key press or a counted LOOP. A JMP to a nearby label is encoded as a short jump; the assembler handles the encoding, but the idea is the same: execution continues at the label, not at the next instruction.
Conditional jumps: reading the flags
Conditional jumps are almost always paired with CMP, or with an instruction that sets ZF such as TEST or SUB. The most used ones are:
| Jump | Meaning | Reads |
|---|---|---|
| JE / JZ | Jump if equal / zero | ZF = 1 |
| JNE / JNZ | Jump if not equal / not zero | ZF = 0 |
| JA / JB | Jump if above / below (unsigned) | CF and ZF |
| JAE / JBE | Jump if above or equal / below or equal (unsigned) | CF and ZF |
| JG / JL | Jump if greater / less (signed) | SF, OF and ZF |
| JGE / JLE | Jump if greater or equal / less or equal (signed) | SF, OF and ZF |
| JC / JNC | Jump if carry / no carry | CF |
| JS / JNS | Jump if sign / no sign | SF |
Conditional jumps are short-range: the target must be within about 127 bytes of the jump. If a branch target is too far away, the assembler reports an out-of-range error. The standard fix is to invert the condition and jump over a JMP: instead of a long conditional jump, write the opposite condition jumping over a short distance to a nearby JMP that reaches the far target.
LOOP: a counter and a jump in one instruction
MOV CX, 10 again: ; ... body ... LOOP again
LOOP subtracts one from CX and jumps to the label if CX is not zero. When CX reaches zero, execution falls through to the next instruction. LOOPE (also written LOOPZ) jumps only if CX is not zero and ZF is set; LOOPNE (LOOPNZ) jumps only if CX is not zero and ZF is clear. JCXZ jumps if CX is already zero, which is useful before entering a loop that must run zero times rather than 65,536 times.
Worked example: add the numbers 1 to 10
ORG 100h MOV AX, 0 ; running total MOV CX, 10 ; loop counter add_next: ADD AX, CX ; add the current counter value LOOP add_next ; CX = CX - 1, repeat while CX is not zero ; AX is now 55. RET
Trace it by hand once: the first pass adds 10, the next adds 9, and so on down to 1, for a total of 55. Notice that LOOP itself does not affect the flags, so the ADD flags from the last pass are still intact after the loop finishes, although this program does not use them.
Common mistakes
- Using a signed jump for unsigned data, or the reverse. See the ADD and CMP post for the difference.
- Putting a MOV between CMP and its conditional jump and assuming the flags are lost. MOV preserves flags, so that particular worry is unfounded; an ADD or SUB in between is the real danger.
- Forgetting that LOOP counts down. A loop written as if CX counted up will run a surprising number of times.
- Letting a conditional jump target drift more than about 127 bytes away after edits, and getting a range error that seems to come from nowhere.
Further reading
- 8086 Instruction Set and Opcode Reference
- Emu8086 Home
- ADD and CMP in 8086 Assembly
- MOV in 8086 Assembly
- 8086 Addressing Modes and Flags Explained
New to assembly? Start with Your First 8086 Program: Hello World, then return here.
