articles » current » assembly » x86 » tutorial » page-10

Intel IA-32 Assembly Tutorial - A Guide to the Basics of x86 Assembly - Page 10

Language Elements (CPU Flags, More on Instructions: Conditional Jumps).

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)
	and dl,10110000b
	jnz label2       ;jump if NOT zero (ZF=0)

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
JZjump if zeroZF = 1
JNZjump if not zeroZF = 0
JCjump if carryCF = 1
JNCjump if not carryCF = 0
JOjump if overflowOF = 1
JNOjump if not overflowOF = 0
JSjump if signedSF = 1
JNSjump if not signedSF = 0
JPjump if parity (even)PF = 1
JNPjump if not parity (odd)PF = 0

Jumps Based On Equality
JEjump if equal
JNEjump if not equal
JCXZjump if CX = 0
JECXZjump if ECX = 0

Jumps Based On Unsigned Comparisons
JAjump if above
JNBEjump if not below or equal (same as JA)
JAEjump if above or equal
JNBjump if not below (same as JAE)
JBjump if below
JNAEjump if not above or equal (same as JB)
JBEjump if below or equal
JNAjump if not above (same as JBE)

Jumps Based On Signed Comparisons
JGjump if greater
JNLEjump if not less than or equal (same as JG)
JGEjump if greater than or equal
JNLjump if not less (same as JGE)
JLjump if less
JNGEjump if not greater than or equal (same as JL)
JLEjump if less than or equal
JNGjump 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.

<<<[Page 10 of 15]>>>

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.