CPU Flags and Boolean Instructions Sahar Mosleh California
CPU Flags and Boolean Instructions Sahar Mosleh California State University San Marcos Page 1
The CPU flags • Each instruction affects the CPU flags. • The zero flag is set when the result of an operation equal 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 if set and positive if clear. • The overflow flag is set when an instruction generates an 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 Sahar Mosleh California State University San Marcos Page 2
Zero and Sign Flags: • The zero flag is set when the destination operand of an arithmetic instruction is assigned a value of zero. • example: mov cx, 1 sub cx, 1 ; cx = 0, ZF = 1 mov ax, 0 FFFFh Inc ax ; ax = 0, ZF = 1 Inc ax ; ax = 1, ZF = 0 • The sign flag is set when the result of an arithmetic operation is negative Sahar Mosleh California State University San Marcos Page 3
• Example: mov cx, 0 sub cx, 1 add cx, 2 ; CX = -1, ; CX= 1, SF=1 SF = 0 Carry Flag: • Carry flag is significant only when the CPU performs unsigned arithmetic. • The result of an unsigned addition operation is too large (or too small) for the destination operand, the carry flag is set. • Example: mov al, 0 FFh add al, 1 Sahar Mosleh ; al = 00, CF= 1 California State University San Marcos Page 4
• The following diagram shows what happens at the bit level 1 Carry: 1 1 1 1 + 0 0 0 0 1 0 0 0 0 • On the other hand, if we add 1 to 00 FFh in ax, the sum easily fits into 16 bits and the carry flag is cleared Sahar Mosleh California State University San Marcos Page 5
mov ax, 00 FFh add ax, 1 ; CF = 0, ax = 0100 h • If we add 1 to FFFFh in the ax register, a carry is generated out of the high bit of ax. mov ax, 0 FFFFh add ax 1 ; CF = 1, ax = 0000 h • If we subtract large unsigned integer from a smaller one, the carry flag is set and the value in al is invalid. mov al, 1 sub al, 2 Sahar Mosleh ; CF = 1 California State University San Marcos Page 6
The Overflow flag: • The overflow flag is relevant only when performing signed arithmetic. • It is set when an arithmetic operation generates a signed result that can not fit in the destination operand. • Example: mov al, +127 add al, 1 ; OF = 1 • Similarly, mov al, -128 sub al, 1 Sahar Mosleh ; OF = 1 California State University San Marcos Page 7
• Overflow has occurred if: • Two positive operands were added and their sum is negative • Two negative operands were added and their sum is positive • Overflow never occurs, when the sign of two addition operands are different. • Example: • When adding the binary values 10000000 and 11111110, there is no carry from bit 6 to bit 7 but there is a carry from bit 7 into the carry flag • Overflow has occurred as displayed in the following 7 6 5 CF = 1 1 0 0 0 0 + 1 1 1 1 0 0 1 1 1 0 Sahar Mosleh California State University San Marcos Page 8
Neg Instruction and flag • This can produce an invalid result if the destination operand can not be stored correctly • Example, if we move -128 to al and try to negate it, the value + 128 can not stored in al. • This causes the overflow flag to be set, and an invalid value to be moved to al mov al, -128 ; al = 10000000 b neg al ; al = 10000000 b, OF=1 • On the other hand 1 f +127 is negated, the result is valid and the overflow flag is clear. mov al, +127 ; al = 01111111 b neg al ; al = 1000001 b, OF = 0 Sahar Mosleh California State University San Marcos Page 9
Boolean and comparison instruction • We are going to begin the study of the conditional processing by working at the binary level, using the four basic operations from the boolean algebra AND , OR, XOR, and NOT. • These operations are used in the design of the computer hardware and software. • The IA-32 instruction set contains the AND, OR, XOR, NOT, TEST, and BTop instruction which directly implement boolean operations between bytes, words and doublewords. Sahar Mosleh California State University San Marcos Page 10
Operation Description AND Boolean AND operation between a source operand the destination operand OR Boolean OR operation between a source operand the destination operand XOR Boolean XOR operation between a source operand the destination operand NOT Boolean NOT operation on a destination operand TEST Implies boolean AND operation between a source and destination operand, setting the CPU flags appropriately BT, BTC, BTR, BTS Copy bit n from the source operand to the carry flag and complement/reset/set the same bit in the destination operand Sahar Mosleh California State University San Marcos Page 11
• The AND instruction performs a boolean (bitwise) AND operation between each pair of matching bits in 2 operands and place the result in the destination operand AND destination, source • The following operand combination are permitted • AND reg, reg • AND reg, mem • AND reg, imm • AND mem, reg • AND mem, imm Sahar Mosleh California State University San Marcos Page 12
• The operand can be 8, 16, or 32 bits, and they must be the same size. • The following truth table labels the input bits x and y. The third column shows the value of expression x AND y Sahar Mosleh X Y X AND Y 0 0 1 1 1 California State University San Marcos Page 13
• The AND instruction is often used to clear selected bits and preserve others. • In the following example, the upper 4 bits are cleared and the lower 4 bits are unchanged 00111011 AND 00001111 ------00001011 mov and a 1, 00111011 b a 1, 00001111 b • The lower 4 bits might contain useful information while we don’t care about the upper 4 bits • This technique is bit extraction because the lower 4 bits are pulled from AL • The AND instruction always clears the overflow and carry flag. It modifies the sign, zero, parity flag according to the value of the destination operand Sahar Mosleh California State University San Marcos Page 14
• Example: • Converting characters to upper case: • The AND instruction provides an easy way to translate a letter from a lower case to upper case. • If we compare the ASCII code for A and a, it becomes clear that only bit 5 is different 001100001= 61 h (‘a’) 001000001= 41 h (‘A’) • The rest of the alphabetic characters have the same relationship. • If we AND any character with 11011111 binary, all bits are unchanged except for the bit 5 which is clear. Sahar Mosleh California State University San Marcos Page 15
• The OR instruction performs a boolean (bitwise) OR operation between each pair of matching bits in 2 operands and place the result in the destination operand. OR destination, source • The following operand combination are permitted. OR reg, reg OR reg, mem OR reg, imm OR mem, reg OR mem, imm Sahar Mosleh California State University San Marcos Page 16
• The operand can be 8, 16, or 32 bits, and they must be the same size. • The following truth table labels the input bits x and y. The third column shows the value of expression x OR y Sahar Mosleh X Y X OR Y 0 0 1 1 1 0 1 1 California State University San Marcos Page 17
• The OR instruction is often used to set selected bits and preserve others. • In the following example, 3 Bh is ORed with 0 Fh. The lower 4 bits of the result are set and the high 4 bits are unchanged OR 00111011 00001111 ------00111111 • The OR instruction can be used to convert a byte containing an integer between 0 and 9 into an ASCII digit. • To do this, you must set bits 4 and 5 if for example AL=05 h, you can OR it with 30 h to convert it to ASCII code for the digit 5 (35 h) Sahar Mosleh California State University San Marcos Page 18
• Example: OR 00000101 00110000 ------00110101 05 h 30 h 35 h, ‘ 5’ • The assembly language instruction to do this are as follows: mov d 1, 5 or d 1, 30 h ; binary value ; convert to ASCII • Flags: The OR instruction always clears the carry and overflow flags. It modifies the sign, zero, and parity flag according to the value of the destination operand Sahar Mosleh California State University San Marcos Page 19
• The XOR instruction performs a boolean (bitwise) XOR operation between each pair of matching bits in 2 operands and place the result in the destination operand. XOR destination, source • The following operand combination are permitted. XOR reg, reg XOR reg, mem XOR reg, imm XOR mem, reg XOR mem, imm Sahar Mosleh California State University San Marcos Page 20
• The operand can be 8, 16, or 32 bits, and they must be the same size. • The following truth table labels the input bits x and y. The third column shows the value of expression x XOR y Sahar Mosleh X Y X+Y 0 0 1 1 1 0 California State University San Marcos Page 21
• Special Quality for XOR is that it reverses itself when applied twice to the same operand. X Y 0 0 1 1 0 1 X + Y (X + Y) + Y 0 1 1 0 0 0 1 1 • This reversible property of XOR makes it ideal tool for a simple form of data encryption • Flag: 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 Sahar Mosleh California State University San Marcos Page 22
- Slides: 22