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
Examples:
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
Instruction | Flag | ||
---|---|---|---|
C | OV | AC | |
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 |
- Affected flags are marked as X
- X denotes value of the flag
- Note: X can be 0 or 1
Examples:
Instruction | Flag | ||
---|---|---|---|
C | OV | AC | |
MOV A, #38H ADD A, #2FH | 0 | 1 | 1 |
MOV A, #9CH ADD A, #64H | 1 | 1 | 0 |
MOV A, #88H ADD A, #93H | 1 | 0 | 0 |
Related topics:
8051 Register Bank and Stack | 8051 Program Flow | 8051 Timers | 8051 Serial Port | 8051 Interrupts
List of topics: 8051
No comments:
Post a Comment