Site Search

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.