PROWAREtech

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

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

Language Elements (More on Instructions: Boolean and Comparison Instructions: AND, OR, XOR, NOT, TEST, BT, BTC, BTR, BTS, CMP).

Conditional Processing

Boolean and Comparison Instructions

Boolean instructions AND, OR, XOR, NOT, TEST, BT, BTC, BTR, and BTS. Each of these instructions affects the CPU flags:

  • The Zero Flag is set when the result of an operation equals zero.
  • The Carry Flag is set when an instruction generates a result that is too large or too small for the destination operand.
  • The Sign Flag is a copy of the high bit of the destination operand, indicating that it is negative is set or positive if clear.
  • The Overflow Flag is set when an instruction generates and invalid signed result.
  • The Parity Flag is set when an instruction generates an even number of 1 bits in the low byte of the destination operand.

The AND instruction performs a boolean (bitwise) AND operation between each pair of matching bits in two operands and places the result in the destination operand:

	;usage: AND destination,source
	mov eax,01101010b
	and eax,00001000b ;EAX = 8
	mov eax,01101010b
	and eax,00000100b ;EAX = 0

	;    10111000
	;AND 10101001
	;    --------
	;    10101000

The AND instruction always clears the Overflow and Carry Flags. It modifies the Sign, Zero and Parity Flags according to the value of the destination operand.

The OR instruction performs a boolean OR operation between each pair of matching bits in two operands and places the result in the destination operand:

	;usage: OR destination,source
	mov eax,01101010b
	or  eax,00001001b ;EAX = 01101011b (6Bh)
	mov eax,00001000b
	or  eax,00000100b ;EAX = 12

	;   10111000
	;OR 10101001
	;   --------
	;   10111001

The OR instruction always clears the Carry and Overflow Flags. It modifies the Sign, Zero and Parity Flags according to the value of the destination operand. For example, you can OR a number with itself or zero to obtain information about its value. For example:

	or ebx,ebx
Zero FlagSign FlagThen value in EBX...
clearclearis greater than zero
setclearis equal to zero
clearsetis less than zero

The XOR instruction performs a boolean exclusive-OR operation between each pair of matching bits in two operands, and stores the result in the destination operand:


	;usage: XOR destination,source
	mov eax,01101010b
	xor eax,00001001b ;EAX = 01100011b (63h)
	mov eax,10001000b
	xor eax,10000100b ;EAX = 12

	;    10111000 <-- original bits
	;XOR 10101001
	;    --------
	;    00010001
	;XOR 10101001 <-- XOR the same value again
	;    --------
	;    10111000 <-- returns to original bits

The XOR instruction always clears the Overflow and Carry Flags. It modifies the Sign, Zero and Parity Flags according to the value of the destination operand.

The Parity Flag indicates whether the lowest byte of the result of a bitwise or arithmetic operation has an even or odd number of 1 bits. The flag is set when the parity is even. It is clear when the parity is odd. One way to check the parity of a number without changing its value is to XOR the number with zero:

	mov al,10010111b    ;5 bits is odd parity
	xor al,0            ;Parity Flag clear
	mov al,10011001b    ;4 bits is even parity
	xor al,0            ;Parity Flag set

The NOT instruction toggles all bits in an operand:

	mov eax,00001111b
	not eax             ;EAX = F0h (11110000b)

No flags are affected by the NOT instruction.

The TEST instruction performs an implied AND operation with the only difference being that TEST does not modify the destination operation. It only sets the flags accordingly.

	01010101 <-- input value
	00001101 <-- TEST value
	00000101 <-- Zero Flag (ZF)=0
	01010101 <-- input value
	10000010 <-- TEST value
	00000000 <-- Zero Flag (ZF) = 1

The TEST instruction always clears the Overflow and Carry flags. It modifies the Sign, Zero and Parity Flags just like the AND instruction.

The CMP (compare) instruction performs an implied subtraction of a source operand from a destination operand and neither operand is modified.

	;CMP destination,source
	cmp eax,2           ;EAX is not modified

CMP uses the same operand combinations as the AND instruction.

The CMP instruction changes the Overflow, Sign, Zero, Carry, Auxiliary Carry and Parity Flags according to the value the destination operand would have had if the SUB instruction were used.

CMP ResultsZero FlagCarry Flag
destination < source01
destination > source00
destination = source10

If the two operands being compared are assumed to be signed then the Sign, Zero and Overflow Flags indicate the following relations between operands.

CMP ResultsFlags
destination < sourceSF ≠ OF
destination > sourceSF = OF
destination = sourceZF = 1

CMP provides the basis for most conditional logic structures. When a conditional jump instruction follows CMP, the result is a higher-level language IF statement.

<<<[Page 9 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.
CLOSE