Processor Status
and the FLAGS Register
Module 5
CS 272
Sam Houston State University
Dr. Tim McGuire
FLAGS Register
-
Individual bits control the action or represent the status of the processor
-
Control flags (TF, IF, DF)
-
Determine how the processor responds to certain situations
-
Status flags (CF, PF, AF, ZF, SF, OF)
-
Set to represent the result of certain operations
-
Used to control conditional jump instructions
FLAG Register Bits
| Bit |
Name |
Symbol |
| 0 |
Carry flag |
cf |
| 2 |
Parity flag |
pf |
| 4 |
Auxiliary carry flag |
af |
| 6 |
Zero flag |
zf |
| 7 |
Sign flag |
sf |
| 8 |
Trace flag |
tf |
| 9 |
Interrupt flag |
if |
| 10 |
Direction flag |
df |
| 11 |
Overflow flag |
of |
Control Flags
-
DF - Direction flag
-
STD: direction = down
-
CLD: direction = up
-
IF - Interrupt enable
-
STI: enable external interrupts
-
CLI: disable maskable external interrupts
-
TF - Trace flag
-
Interrupt 1 after executing instruction, if set
Status Flags
-
Carry
-
carry or borrow at MSB in add or subtract
-
last bit shifted out
-
Parity
-
low byte of result has even parity
-
Auxiliary
-
Zero
-
Sign
-
Overflow
-
signed overflow occurred during add or subtract
The Carry Flag (CF)
-
CF = 1 if there is a carry out from the msb (most significant bit) on addition,
or there is a borrow into the msb on subtraction
-
CF = 0 otherwise
-
CF is also affected by shift and rotate instructions
The Parity Flag (PF)
-
PF = 1 if the low byte of a result has an even number of one bits (even
parity)
-
PF = 0 otherwise (odd parity)
The Auxiliary Carry Flag (AF)
-
AF = 1 if there is a carry out from bit 3 on addition, or there is a borrow
into the bit 3 on subtraction
-
AF = 0 otherwise
-
AF is used in binary-coded decimal (BCD) operations
The Zero Flag (ZF)
-
ZF = 1 for a zero result
-
ZF = 0 for a non-zero result
The Sign Flag (SF)
-
SF = 1 if the msb of a result is 1; it means the result is negative if
you are giving a signed interpretation
-
SF = 0 if the msb is 0
The Overflow Flag (OF)
-
OF = 1 if signed overflow occurred
-
OF = 0 otherwise
(Signed) Overflow
-
Can only occur when adding numbers of the same sign (subtracting with different
signs)
-
Detected when carry into MSB is not equal to carry out of MSB
-
Easily detected because this implies the result has a different sign than
the sign of the operands
-
Programs can ignore the Flags!
Signed Overflow Example
10010110
+ 10100011
00111001
Carry in = 0, Carry out = 1
Neg+Neg=Pos
Signed overflow occurred
OF = 1 (set)
00110110
+ 01100011
10011001
Carry in = 1, Carry out = 0
Pos+Pos=Neg
Signed overflow occurred
OF = 1 (set)
Examples of No Signed Overflow
10010110
+ 01100011
11111001
Carry in = 0, Carry out = 0
Neg+Pos=Neg
No Signed overflow occurred
OF = 0 (clear)
10010110
+ 11110011
10001001
Carry in = 1, Carry out = 1
Neg+Neg=Neg
No Signed overflow occurred
OF = 0 (clear)
Unsigned Overflow
-
The carry flag is used to indicate if an unsigned operation overflowed
-
The processor only adds or subtracts - it does not care if the data is
signed or unsigned!
10010110
+ 11110011
10001001
Carry out = 1
Unsigned overflow occurred
CF = 1 (set)
Instructions and Flags
-
MOV and XCHG - no flags are changed
-
ADD and SUB - all flags affected
-
INC and DEC - all except CF
-
NEG - all flags affected
-
CF=0 only if value is 0
-
OF=1 only if value is -MAXINT