Hex subtraction and overflow/carry flags

  • Thread starter Thread starter nwilkins259
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the signed hexadecimal subtraction of 71 and A9, specifically focusing on the implications for carry and overflow flags in the context of an 8051 microcontroller. Participants explore the behavior of these flags when dealing with signed and unsigned numbers, as well as the effects of two's complement representation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant calculates the result of the subtraction as C8 and questions whether carry or overflow flags are set, given the nature of the operands.
  • Another participant asserts that the carry flag will be set in the 8051, noting that the processor does not differentiate between signed and unsigned operands.
  • A different participant suggests that the computation involves taking the two's complement, implying that the operation can be viewed as addition, which could lead to overflow but not carry.
  • A later reply clarifies the assembly language perspective, explaining that the negative operand is represented in two's complement, resulting in a specific outcome for the carry and overflow flags, while also noting the limitations of 8-bit representation.

Areas of Agreement / Disagreement

Participants express differing views on the implications of carry and overflow flags, with no consensus reached on whether overflow occurs in this specific case.

Contextual Notes

Participants reference the limitations of 8-bit two's complement representation and the ambiguity in how the processor interprets signed versus unsigned values, which may affect the understanding of carry and overflow in this context.

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.
 

Similar threads

Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
16K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K