## Thursday, March 17, 2016

### 8051 Flags Bits

8051 has a flag register called Program Status Word. There are four flag bits: CY (carry), AC (auxiliary carry), P (parity), and OV (overflow).

Carry Flag (CY):
This flag is set whenever there is a carry out from the D7 bit. This flag bit is affected after an 8-bit addition or subtraction. It can also be set to 1 or 0 directly by an instruction such as “SETB C” and “CLR C”

Auxiliary Carry (AC):
If there is a carry from D3 to D4 during an ADD or SUB operation, this bit is set; otherwise, it is cleared. This flag is used by instructions that perform BCD (binary coded decimal) arithmetic.

Parity Flag (P):
The parity flag reflects the number of 1’s in the A (accumulator) register only. If the A register contains an odd number of 1’ss, then P = 1. Therefore, P = 0 if A has an even number of 1’s

Overflow Flag (OV):
This flag is set whenever the result of a signed number operation is too large, causing the high-order bit to overflow into the 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.

Instructions that Affect Flag Settings
InstructionFlag
COVAC
SUBBXXX
MUL0X
DIV0X
DAX
RRCX
RLCX
SETB C1
CLR C0
CPL CX
ANL C, bitX
ANL C, /bitX
ORL C, bitX
ORL C, /bitX
MOV C, bitX
CJNEX
• Affected flags are marked as X
• X denotes value of the flag
• Note: X can be 0 or 1

Examples:
InstructionFlag
COVAC
MOV A, #38H
011
MOV A, #9CH
110
MOV A, #88H