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:

CYACF0RS1RS0OV--P
CYPSW.7Carry Flag
ACPSW.6Auxiliary Carry Flag
----PSW.5Available to the user for general purpose
RS1PSW.4Register Bank Selector bit 1
RS0PSW.3Register Bank Selector bit 0
OVPSW.2Overflow Flag
----PSW.1User Definable bit
PPSW.0Parity Flag. Set / Cleared by hardware each instruction cycle to indicate an Odd / Even number of 1 bits in the accumulator
RS1RS0Register BankAddress
00000H - 07H
01108H - 0FH
10210H - 17H
11318H - 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:

InstructionCY [Carry Flag]OV [Overflow Flag]AC [auxiliary carry flag]
ADDXXX
ADDCXXX
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  

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:

   9C10011100
+ 6401100100
= 10000000000

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]

More From Iamtechnical.com

Advertisement: