## Site Search

# SECTION VI - THE PROGRAM STATUS WORD AND FLAG BITS IN 8051

**SECTION VI - THE PROGRAM STATUS WORD AND FLAG BITS IN 8051**

The 8051 has a flag register to indicate arithmetic conditions such as the carry bit. The flag register in the 8051 is called the "Program Status Word [PSW]" register.

__PSW [Program Status Word] Register__:

The program status word [PSW] is an 8-bit register. Program status word is also referred to as the *flag register*. The PSW register is 8 bits wide, but only 6 bits of it are used by the 8051 microcontroller. The remaining two unused bits are user-definable flags. From the 6 bits, the four of the flags are called *conditional flags. *These conditional flags indicate some conditions that resulted after an instruction was expected. These four conditional flags are CY [carry], AC [auxiliary carry], P [Parity], and OV [overflow]. The other 2 bits are designated as RS0 and RS1, and are used to change the bank registers. The PSW.5 and PSW.1 bits are general purpose status flag bits and can be used by programmer for any purpose. They are user definable.

__Bits of the Program Status Word Register__:

CY | AC | F0 | RS1 | RS0 | OV | -- | P |
---|

CY | PSW.7 | Carry Flag |
---|---|---|

AC | PSW.6 | Auxiliary Carry Flag |

---- | PSW.5 | Available to the user for general purpose |

RS1 | PSW.4 | Register Bank Selector bit 1 |

RS0 | PSW.3 | Register Bank Selector bit 0 |

OV | PSW.2 | Overflow Flag |

---- | PSW.1 | User Definable bit |

P | PSW.0 | Parity Flag. Set / Cleared by hardware each instruction cycle to indicate an Odd / Even number of 1 bits in the accumulator |

RS1 | RS0 | Register Bank | Address |
---|---|---|---|

0 | 0 | 0 | 00H - 07H |

0 | 1 | 1 | 08H - 0FH |

1 | 0 | 2 | 10H - 17H |

1 | 1 | 3 | 18H - 1FH |

The 4 flags bit of the program status word register which are CY [carry flag], AC [auxiliary carry flag], P [parity flag] and OV [overflow flag]. Lets examine the impact of instructions on these registers in the discussion given below.

**CY [Carry Flag]:**

** **The carry flag [CY] is set whenever there is a carry out from the D7 bit. The carry flag is affected after an 8-bit addition or subtraction. The carry flag also be set to 1 or 0 directly by an instruction such as "SETB C" and "CLR C". The "SETB C" stands for "set bit carry" and "CLR C" for "clear carry". The carry flag is used to detect errors in unsigned arithmetic operations.

**AC [Auxiliary Carry Flag]:**

The auxiliary carry [AC] bit is set during an ADD or SUB if there is a carry from D3 to D4, otherwise auxiliary carry bit is remain cleared. The auxiliary carry flag bit is used by instructions that perform BCD [Binary Coded Decimal] arithmetic.

**P [Parity Flag]:**

The parity flag [P] reflects the number of 1's in the A [accumulator] register only. If the accumulator register contains an odd number of 1's, then parity flag P=1. If accumulator register contains an even number of 1's, then parity flag P=0.

**OV [Overflow Flag]:**

The overflow flag [OV] is set whenever the result of a signed number operation is too large, causing the high-order bit to overflow into sign bit. The carry flag is used to detect errors in unsigned arithmetic operations. The overflow flag is only used to detect errors in signed arithmetic operations.

__Impact of ADD instruction and Program Status Word [PSW]__:

Lets examine the impact of the ADD instruction on the flag bits CY, AC, and P of the program status word register [PSW]. The ADD instruction affect CY [carry flag], P [parity flag], AC [auxiliary carry flag], and OV [overflow flag].

**Instructions that Affect Flag Bits:**

Instruction | CY [Carry Flag] | OV [Overflow Flag] | AC [auxiliary carry flag] |
---|---|---|---|

ADD | X | X | X |

ADDC | X | X | X |

SUBB | X | X | X |

MUL | 0 | X | |

DIV | 0 | X | |

DA | X | ||

RRC | X | ||

RLC | X | ||

SETB C | 1 | ||

CLR C | 0 | ||

CPL C | X | ||

ANL C,bit | X | ||

ANL C,/bit | X | ||

ORL C,bit | X | ||

ORL C,/bit | X | ||

MOV C,bit | X | ||

CJNE | X |

Note: X can be '0' or '1'.

__Example # 1__:

Lets show the status of the CY, AC, and P flags after the addition of 37H and 2EH in the following instructions:

MOV A,#38H |

ADD A,#2FH ;after the addition A = 67H , CY = 0 |

**Answer:**

38 | 00111000 |

+ 2F | + 00101111 |

= 67 | = 01100111 |

Carry Flag [CY] = 0 , As there is no carry beyond the D7 bit

Auxiliary Carry Flag [AC] = 1, As there is a carry from the D3 to the D4 bit

Parity Flag [P] = 1, As the accumulator [A] has an odd number of 1's [it has five 1's]

__Example # 2__:

Let us see the status of the CY, AC and P flags after the addition of 9CH and 64H in the following instructions.

MOV A,#9CH |

ADD A,#64H ;after addition A=00 and CY=1 |

**Answer:**

9C | 10011100 |

+ 64 | 01100100 |

= 100 | 00000000 |

Carry Flag [CY]=1, since there is a carry beyond the D7 bit

Auxiliary Carry Flag [AC] = 1, since there is a carry from the D3 to the D4 bit

Parity Flag [p] = 0, since the accumulator has an even number of 1's [it has zero 1's]

**Example # 3:**

Lets show the status of the CY, AC, and P flags after addition of 88H and 93H in the following instructions below:

MOV A, #88H |

ADD A,#93H ;after the addition A=1BH,CY=1 |

**Answer:**

88 | 10001000 |

+ 93 | + 10010011 |

= 11B | = 00011011 |

Carry Flag [CY] = 1, As there is a carry beyond the D7 bit

Auxiliary Carry Flag [AC] = 0 , As there is no carry from the D3 to the D4 bit

Parity Flag = 0, As the accumulator has an even number of 1's [it has four 1's]