|
|
|
|
State Flags
Let's start with the Zero Flag ZF, as it is the easiest to understand. If the result of an arithmetic or logical instruction which has been performed equals zero, then ZF is set, otherwise ZF is cleared. The means that after the instruction SUB AX, AX AX is equal to zero and the Zero
Flag is set, ZF becomes equal to 1 which is denoted as ZR. ADD AX, 1 AX equals 1 and ZF is equal to NZ (cleared), ADD AX, 0FFFFh AX contains zero and ZF is equal to ZR (set) again.
The next flag to consider is the Sign Flag SF which is especially important for instructions which process signed numbers. The Sign Flag can also be influenced by other instructions. The rule for this flag is: If the highest bit of a result of an instruction which has been performed equals 1, then SF is set. Otherwise SF is clear. For example, after the instruction SUB AX, AX AX = 0 and the Sign Flag is equal
to PL (cleared, positive), SUB AX, 1 AX contains 0FFFFh and SF is now
equal to NG (set, negative), ADD AX, 2 AX contains 1 and SF equals to PL (cleared).
Next in line is the Carry Flag CF which indicates the carry from, or the borrow to, the highest bit of the result of an arithmetic instruction. The value of the Carry Flag (1 if set or 0 if clear) is also used in performing arithmetic operations with long binary numbers. Let's look at these simple
examples. SUB AX, AX The Carry Flag is equal to NC (is cleared, nor carry), MOV AX, 0FFFFh The Carry Flag remains unchanged, ADD AX, 1 the value of the Carry Flag is CY (set, carry - yes!)., ADD AX, 0 The Carry Flag takes the value NC (cleared). The Carry Flag can also be used as an indicator by certain DOS and BIOS routines. In this case its value is changed by special instructions - STC (SeT Carry) and CLC (CLear Carry). Later when BIOS and DOS services are covered, we'll consider the use of the Carry Flag.
The Auxiliary Carry Flag AF is similar to the Carry Flag. It is designed for operations with BCD numbers, so it marks whether there have been carries from or borrows to bit #3d (counting from the right - don't forget about bit #0d) of the result. For example the instruction SUB AX, AX makes AF equal to NA (cleared), MOV AX, 8 does not change the AF flag, ADD AX, 8 makes it equal to AY (set), and ADD AX, 0 makes the Auxiliary Carry Flag equal to NA (cleared).
The Overflow Flag OF, together with other state flags, reflects peculiarities of arithmetic operations.
The sequence of instructions below should help you to understand the OF flag: SUB AX, AX ;Clear AX - OF must be NV (cleared) MOV AL, 128 ;OF is unchanged ADD AX, 128 ;OF is set (equal to OV)
The next flag - the Parity Flag is rarely used nowadays, which is probably why it is often misunderstood. The rule for the Parity Flag PF is a bit unusual, but very simple: If the lower byte of the
result of the instruction, which has just been performed has an
even number of bits equal to 1, This means that the instruction MOV AX, 0F001h does not change the Parity Flag, ADD AX, 2 sets the PF flag to PE (set), ADD AL, 5h makes the PF flag equal to PO (cleared).
Later, in another Chapter, I will use these codes with real examples while I demonstrate the use of CodeView Debugger.
Revised - Tuesday, October 26, 1999 06:52 PM Central Daylight Time |
|
|