PROWAREtech
Intel IA-32 Assembly Tutorial - A Guide to the Basics of x86 Assembly - Page 10
Setting and Clearing CPU Flags
and al,0 ;this sets the Zero Flag (ZF=1)
or al,1 ;this clears the Zero Flag (ZF=0)
or al,80h ;this sets the Sign Flag (10000000b) (SF=1)
and al,7Fh ;this clears the Sign Flag (01111111b) (SF=0)
stc ;this sets the Carry Flag (CF=1)
clc ;this clears the Carry Flag (CF=0)
mov al,7Fh ;AL = 127
inc al ;AL = -128 (80h) this sets the Overflow Flag (OF=1)
or eax,0 ;this clears the Overflow Flag (OF=0)
Conditional Jumps
One can implement any logic structure, no matter how complex, using a combination of comparisons and jumps.
Two steps are involved in executing a conditional statement; first, an operation such as CMP
,
AND
or SUB
modify the CPU flags. Seconds, a conditional jump instruction tests the
flags and causes a branch to a new address.
cmp al,0 ;compare AL to zero
jz label1 ;jump if zero (ZF=1)
.
.
label1:
and dl,10110000b
jnz label2 ;jump if NOT zero (ZF=0)
.
.
label2:
A conditional jump instruction branches to a destination label when a flag condition is true. If the flag condition is false then the instruction immediately following the conditional jump is executed.
Jumps Based On Flag Values | ||
Mnemonic | Description | Flags |
JZ | jump if zero | ZF = 1 |
JNZ | jump if not zero | ZF = 0 |
JC | jump if carry | CF = 1 |
JNC | jump if not carry | CF = 0 |
JO | jump if overflow | OF = 1 |
JNO | jump if not overflow | OF = 0 |
JS | jump if signed | SF = 1 |
JNS | jump if not signed | SF = 0 |
JP | jump if parity (even) | PF = 1 |
JNP | jump if not parity (odd) | PF = 0 |
Jumps Based On Equality | |
Mnemonic | Description |
JE | jump if equal |
JNE | jump if not equal |
JCXZ | jump if CX = 0 |
JECXZ | jump if ECX = 0 |
Jumps Based On Unsigned Comparisons | |
Mnemonic | Description |
JA | jump if above |
JNBE | jump if not below or equal (same as JA) |
JAE | jump if above or equal |
JNB | jump if not below (same as JAE) |
JB | jump if below |
JNAE | jump if not above or equal (same as JB) |
JBE | jump if below or equal |
JNA | jump if not above (same as JBE) |
Jumps Based On Signed Comparisons | |
Mnemonic | Description |
JG | jump if greater |
JNLE | jump if not less than or equal (same as JG) |
JGE | jump if greater than or equal |
JNL | jump if not less (same as JGE) |
JL | jump if less |
JNGE | jump if not greater than or equal (same as JL) |
JLE | jump if less than or equal |
JNG | jump if not greater (same as JLE) |
The LOOPZ
(loop if zero) instruction permits a loop to continue while the Zero Flag is set and
the unsigned value of ECX is greater than zero. The destination label must be between -128 and +127 bytes
from the location of the following instruction. The LOOPE
(loop if equal) instruction is equivalent
to LOOPZ
.
The LOOPNZ
(loop if not zero) instruction is the counter part to LOOPZ
. The loop
continues while the unsigned value of ECX is greater than zero and the Zero Flag is clear. The LOOPNE
(loop if not equal) instruction is equivalent to LOOPNZ
.