Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8051 flags

Status
Not open for further replies.
4electros said:
hello,what's the difference in function between AC(auxiliary carry flag) and CY(carry flag) and overfow flag (OV) cause i mix them.

thanks
AC is the carry from bit 3 to bit 4. It's used when doing BCD(Binary Coded Decimal) arithmetic.

CY is the cary from bit 7, the most significant bit in an 8-bit byte

OV tells you about the result of adding or subtracting signed 8-bit quantities. If the sign of the result does not match the signs(s) of the operands then there is overflow.
Code:
Example
 
7F + 7F = FE  => POS + POS = NEG
There is no carry, but there is overflow
 
AC is set if there is a carry from bit 3 to bit 4. It is useful when doing BCD arithmetic.
OV is set when bit 7 changes. It is useful when doing signed arithmetic. Adding 120+50 will give a negative result and set the OV flag.
Carry flag is the 9th bit of the answer in arithmetic operations. Also becomes the 9th bit in some shift operations.

HTH

Mike.
 
Papabravo said:
AC is the carry from bit 3 to bit 4. It's used when doing BCD(Binary Coded Decimal) arithmetic.

BCD is binary coded decimal which means that it extends from 0000 to 1001 (0 to 9 in decimal) but when exceeds 9 it becomes 1010 and so on....so how AC does match BCD arithmetic as it sets only when exceeds 1111(15d).

could you give me any example or clarification?!
 
4electros said:
BCD is binary coded decimal which means that it extends from 0000 to 1001 (0 to 9 in decimal) but when exceeds 9 it becomes 1010 and so on

No it doesn't, with BCD it will increase from 1001 to 10000, that's the whole point of it, and that's the bit 3 to bit 4 move that triggers the flag.

I'm presuming the 8051 does BCD?, as I'm not familiar with it - but the ancient 6502 managed to do it OK!.
 
Auxiliary carry is the carry from 1st nibble to 2nd nibble.
Ex:
0000 0000 0000 1000
+0000 0000 0000 1100
=0000 0000 0001 0100

The underlined bit is auxiliary carry.

CY is carry from MSB, In 8051 it is carry from 15th bit.
Ex:
1000 0000 0000 0000 (8000)
+ 1100 0000 0000 0000 (C000)
= 0100 0000 0000 0000 (1400) With carry 1

Note that the addition above i have done is unsigned addition.

Overflow occurs only while doing signed addition. When Added result is not in the range then we say overflow has occured. In signed addition MSB(most significant bit) is taken as sign bit.
In case of 16bit signed addition (i.e in case of 8051) The range is +32768 to -32767, i.e +7FFF to -7FFE. 15th bit is sign bit.

I will give some examples,
1000 0000 0000 0000 (-0000) (To get it in hex take 2's compliment since
+ 1111 0000 0000 0000 (-1000) ( 15th bit is '1' and hence number is
0111 0000 0000 0000 (+7000) ( negative

Here overflow occured. Both additive elements are negative, but the result is positive since the 15th bit is '0'. When We add (-0000) and (-1000) Ans should be (-1000) But here it is +7000. Hence it is Overflow

0111 0000 0000 0000 (+7000)
+0100 0000 0000 0000 (+4000)
=1011 0000 0000 0000 (-5000)

here also over flow occured, both additives are positive but the result is negative,

Note 1: If 15th bit is '0' then number is positive and you should not take 2's compliment.

Note 2: In both example observe that sign change occured from Additive elements to Result. In 1st example both numbers were negative but Ans is positive. In 2nd example both numbers were positive but Ans is negative.
And hence one can conclude that When there is such a sign change overflow is occured.

Note 3: When the additive elements are of opposite sign, overflow never can occur. Its obvious and my promise. :)
 
Nigel Goodwin said:
I'm presuming the 8051 does BCD?, as I'm not familiar with it - but the ancient 6502 managed to do it OK!.

yeah it does.Any binary value produces AC or C flag automatically added with (06h) by DAA(decimal adjust accumulator after addition) in order to keep it in BCD.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top