Intel 80x86 Conditional and Unconditional Branching
Intel 80x86 Unconditional Branch Instructions
JMP dest is the instruction for an unconditional branch to address
dest.
However, there are actually 4 different types of unconditional branches.
-
Short Jump (EB) : dest is an 8-bit offset which is sign extended
to 16 bits and added to the IP register. This gives a range of between
-126 and +129 bytes from the address of the JMP instruction (the IP register
is first incremented).
-
Near Jump (E9) : dest is a 16-bit signed offset which is
added to the IP register allowing a branch to any location in the current
Code Segment.
-
Near Jump Indirect (FF) : The effect of the Near Jump Indirect is
similar to a Near Jump except that dest is a 16 bit "pointer" which
is loaded (not added) into the IP register. This instruction is used to
implement "jump tables".
-
Far Jump (EA) : dest is two 16-bit values (4 bytes) which
are loaded into the IP and CS registers respectively. This allows a branch
to any address in memory. A Far Jump is needed for branches to locations
outside of the current Code Segment
1. The Flag Register
Each execution of an instructions sets or clears one or more flags depending
on the instruction and the results generated by the instruction. For example,
arithmetic operations set or clear the Carry, Sign, Zero and/or Overflow
flags depending on whether the result had a carry out, resulted in a negative
number, resulted in a zero, or overflowed.
F E D C B A 9 8 7 6 5 4 3 2 1 0
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| | | | | O| D| I| T| S| Z| | A| | P| | C|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
C - Carry Flag : Set if Carry Out
Z - Zero Flag : Set if Zero Result
S - Sign Flag : Set if Negative Result
O - Overflow Flag : Set if Carry In != Carry Out
D - Direction Flag : If Set then Up; If Clear then Down
P - Parity Flag : Set if Parity Even; Clear if Parity Odd
A - Auxiliary Carry : Used for BCD arithmetic
I - Interrupt : If Set Enable Interrupts;
If Clear Disable Interrupts
T - Trap : If Set CPU executes single
instruction followed by INT 01
2. The Comparison Instruction CMP
Conditional branching is done by subtracting two operands to set/clear
one or more flags then jumping on one or more flags being set or cleared.
For example
sub al, 0dh ; subtract ASCII code for CR from AL
je Done ; if equal branch to Done
However, the subtraction operation above is destructive in that
it overwrites the operand in the AL register. Hence the CMP insturction
which is a non-destructive subtraction.
CMP destination, source
subtracts source from destination
sets flags
does not alter destination
In this case, to compare the contents of the AL register with 0dh
cmp al, 0dh ; compare AL with ASCII CR
je Done ; if equal branch to done
3. Unsigned Conditional Branching
CF and ZF Flag Configurations for Unsigned Comparisons
destination > (above) source : CF = 0 ZF = 0
destination = (equal) source : CF = 0 ZF = 1
destination < (below) source : CF = 1 ZF = 0
je jump if equal ZF = 1
same as jz
jne jump if not equal ZF = 0
same as jnz
ja jump if above CF = 0 and ZF = 0
same as jnbe
jae jump if above or equal CF = 0
same as jnb
jb jump if below CF = 1
same as jnae
jbe jump if below or equal CF = 1 or ZF = 1
same as jna
4. Signed Conditional Branching
OF and SF Flags Configurations for Signed Comparisons
if destination > source then
a. no overflow if OF = 0 then SF = 0
b. on overflow if OF = 1 then SF = 1
if destination < source then
a. no overflow if OF = 0 then SF = 1
b. on overflow if OF = 1 then SF = 0
jg jump if greater than (ZF = 0) and (SF = OF)
same as jnle
jge jump if greater than or equal SF = OF
same as jnl
jl jump if less than SF != OF
same as jnge
jle jump if less than or equal (ZF = 1) or (SF != OF)
same as jng
Note. The testing of equality and non-equality is the same for both signed
and unsigned integers.
5. Flag Testing Conditional Branching
jo jump on overflow OF = 1
jno jump on no overflow OF = 0
jc jump on carry CF = 1
jnc jump on no carry CF = 0
js jump on sign negative SF = 1
jns jump on sign positive SF = 0
jpe jump on parity even (same as jp) PF = 1
jpo jump on parity odd (same as jnp) PF = 0
jcxz jump on CX register = 0 CX = 0000h
6. Counting Loop
loop dest decrement CX and
jump to dest if CX != 0
loope dest loop on equal decrement CX and
jump to dest if CX != 0 and ZF = 1
loopne dest loop on not equal decrement CX and
jump to dest if CX != 0 and ZF = 0
A bottom test loop is easily implemented using the CX register as the loop
counter. Use the jcxz instruction before the loop to insure that CX > 0
mov cx, n ; initialize CX
jcxz EndLoop1 ; make sure CX > 0
Loop1: ; loop body begins here
loop Loop1
EndLoop1: ; 1st instruction after Loop1
In addition to terminating a loop if CX = 0, the loope and loopne
instructions allow a loop to be terminated if the Zero Flag is set (or
cleared). For example a loop ending with
cmp ax, bx
loopne Loop1
EndLoop1:
would terminate if CX equal to 0 or AX equal to BX.
Return
to Comp 255 Home Page