Hex subtraction and overflow/carry flags

  • Thread starter Thread starter nwilkins259
  • Start date Start date
AI Thread Summary
In the discussion on hex subtraction and overflow/carry flags, the subtraction of 71 (0x71) and A9 (0xA9) yields C8. The carry flag is set because the operation involves a negative number, which is treated using 2's complement. The processor cannot distinguish between signed and unsigned operands, leading to both carry and overflow flags being set. The result of the subtraction indicates an overflow condition, as the value exceeds the range of an 8-bit 2's complement representation. This highlights the importance of understanding how negative values are processed in assembly language on the 8051 microcontroller.
nwilkins259
Messages
9
Reaction score
0
Signed hex subteaction of 71-A9, i get C8. My question is whether the flags carry/borrow or overflow occurred. Its a positive number and a neg number so can it be overflow? Or is it the same as subtracting a neg number...?
 
Physics news on Phys.org
It will set the carry flag, for the 8051 anyway. The processor cannot tell if an operand is signed or unsigned.
 
Isn't the first thing the comp does when it sees minus is to take the 2's compliment? Wouldn't that make this 71+57, which woulld not carry but would be overflow?
 
When you mentioned a carry bit, I jumped into the assembly language level of thought. I reference an 8051, because it’s a common 8-bit machine.

The instructions:

mov A, #113
subb A, #-87

are the same as

mov A, #0x71
subb A, #0xA9

You are on the right track. In this case, when the assembler "sees" the negative sign, it represents the value using 2’s complement.

The result of the above instructions is that the A-register will contain 0xC8 (or, using 2's complement in reverse, -56), the carry bit is set, and the overflow bit is also set because the result would have been 200 which cannot be represented in an 8-bit 2’s complement system.
 
Back
Top