Flag Register
NOTE:
ONE OR MORE FLAGS MAY GET SET OR RESET AFTER AN ARITHMETIC LOGIC INSTRUCTION
MOV INSTRUCTION DOESN’T AFFECT ANY FLAG
When executing arithmetic instructions, we often want to know
something about the result. Is it negative, positive, or zero? Is it too large
or too small to fit into the destination operand? Answers to such questions can
help us detect calculation errors that might otherwise cause erratic program behavior. We use the values of CPU status
flags to check the outcome of arithmetic operations.
We also use status flag values to activate conditional branching instructions, the basic tools of program logic. Here’s a quick overview of the status flags.
We also use status flag values to activate conditional branching instructions, the basic tools of program logic. Here’s a quick overview of the status flags.
• The
Carry flag indicates unsigned integer overflow. For example, if an instruction
has an 8-bit destination operand but the instruction
generates a result larger than 11111111 binary, the
Carry flag is set.
Carry flag is set.
• The Overflow flag indicates signed integer overflow. For example, if an instruction has a 16-bit destination operand but it generates a negative result smaller than 32,768 decimal, the
Overflow flag is set.
• The Zero flag indicates that an operation produced zero. For example, if an operand is subtracted from another of equal value, the Zero flag is set.
• The Sign flag indicates that an operation produced a negative result. If the most significant bit (MSB) of the destination operand is set, the Sign flag is set.
• The Parity flag indicates whether or not an even number of 1 bits occurs in the least significant byte of the destination operand, immediately after an arithmetic or Boolean instruction
has executed.
• The
Auxiliary Carry flag is set when a 1 bit carries out of position 3 in the least
significant
byte of the destination operand.
byte of the destination operand.
Unsigned operations: Zero, Carry and Auxiliary Carry
The
Zero flag is set when the result of an arithmetic operation equals zero. The
following examples show the state of the destination register and Zero flag
after executing the SUB, INC, and DEC instructions:
MOV CX,1
SUB CX,1 ; CX = 0, ZF = 1
MOV AX,0FFFFH
INC AX ; AX = 0, ZF = 1
INC AX ; AX = 1, ZF = 0
DEC AX ; AX = 0, ZF = 1
Addition and the Carry Flag
The Carry flag’s operation is easiest to explain if we consider addition
and subtraction separately. When adding two unsigned integers, the Carry flag
is a copy of the carry out of the most significant bit of the destination
operand. Intuitively, we can say CF=1 when the sum exceeds the storage size of
its destination operand. In the next example, ADD sets the Carry flag because
the sum (100h) is too large for AL:
MOV AL,0FFH
ADD AL,1; AL = 00, CF = 1
Figure below shows
what happens at the bit level when 1 is added to 0FFh. The carry out of the highest
bit position of AL is copied into the Carry flag.
Figure Adding 1 to 0FFh sets the Carry flag.
On the other hand, if 1 is added to 00FFh in AX, the sum easily fits into 16 bits and the Carry flag is clear:
MOV AX,00FFH
ADD AX,1; AX = 0100H, CF = 0
But adding 1 to FFFFh in the AX register generates a Carry out of
the high bit position of AX:
MOV AX,0FFFFH
ADD AX,1; AX = 0000, CF = 1
Subtraction and the Carry Flag
A subtract operation sets the Carry flag when a larger unsigned integer is subtracted from a smaller
one. Figure below shows what happens when we subtract 2 from 1, using 8-bit
operands. Here is the corresponding assembly code:
MOV
AL,1
SUB AL,2 ; AL =
FFH, CF = 1
Auxiliary Carry
The Auxiliary Carry (AC) flag indicates a carry or borrow out of
bit 3 in the destination operand. It is
primarily used in binary coded decimal (BCD) arithmetic, but can be used in other contexts. Suppose we
add 1 to 0Fh. The sum (10h) contains a 1 in bit position 4 that was carried out of bit
position 3:
MOV AL,0FH
ADD AL,1 ; AC = 1
Here is the arithmetic:
0 0 0 0 1 1 1 1
+ 0 0 0 0 0 0 0 1
------------------
0 0 0 1 0 0 0 0
Parity
The Parity flag (PF) is set when
the least significant byte of the destination has an even number of 1 bits. The following ADD and SUB instructions alter the parity of
AL:
MOV AL,10001100B
ADD AL,00000010B
; AL = 10001110, PF = 1
SUB AL,10000000B
; AL = 00001110, PF = 0
After the ADD
instruction executes, AL contains binary 10001110 (four 0 bits and four 1
bits), and PF=1. After the SUB instruction executes,
AL contains an odd number of 1 bits, so the Parity flag equals 0.
Signed Operations: Sign and Overflow Flags
Sign Flag:
The Sign flag
is set when the result of a signed arithmetic operation is negative. The next example subtracts a larger integer (5)
from a smaller one (4):
MOV AX,4
SUB AX,5 ; AX =
-1, SF = 1
From a mechanical point
of view, the Sign flag is a copy of the destination operand’s high bit. The next example shows
the hexadecimal values of BL when a negative result is generated:
mov bl,1 ; BL = 01h
sub bl,2 ; BL = FFh
(-1), SF = 1
Overflow Flag:
The Overflow flag is
set when the result of a signed arithmetic operation overflows or under flows
the destination operand. For example, from Chapter 1 we know that the largest possible integer
signed byte value is 127; adding 1 to it causes overflow:
MOV AL, +127
ADD AL,1; OF = 1
Similarly, the smallest
possible negative integer byte value is 128. Subtracting 1 from it causes underflow. The
destination operand value does not hold a valid arithmetic result, and the
Overflow flag is set:
MOV AL, -128
SUB AL,1; OF = 1
The Addition Test There
is a very easy way to tell whether signed overflow has occurred when adding two
operands. Overflow occurs when:
• Adding two positive
operands generates a negative sum
• Adding two negative
operands generates a positive sum
Overflow never occurs
when the signs of two addition operands are different.
How the Hardware Detects Overflow:
The CPU uses an interesting mechanism to determine
the state of the
Overflow flag after an addition or subtraction operation. The value that
carries out of the highest bit position is exclusive ORed with the carry
into the high bit of the result. The resulting value is placed in the Overflow
flag. In Figure shown below, we show that adding the 8-bit binary integers
10000000 and 11111110 produces CF = 1, with carry in (bit7) = 0. In other
words, 1 XOR 0 produces OF = 1.
Source: Assembly Language Intel based Computers, 4th Edition, by
KIP, R, IRVINE
No comments:
Post a Comment