1
$\begingroup$

since unsigned and signed integers uses same components to compute then how does the overflow and carry flags are set?

asked Mar 16 at 16:32
$\endgroup$

1 Answer 1

2
$\begingroup$

The processor stores bits in each register. It has no idea whether those bits are intended to represent a signed integer or an unsigned integer. It is up to the program to use operations that interpret those bits as a signed integer, or as an unsigned integer.

On some (many?) processors, the processor generates both the carry flag and the overflow flag, after every operation. If the operands were intended to be interpreted as unsigned integers, then the carry flag is meaningful and useful (and the overflow flag should be ignored); if the operands were intended to be interpreted as signed integers, then the overflow flag is meaningful and useful (and the carry flag should be ignored). It is up to the program to pay attention to one or the other, depending on whether the program intends the bits in the operands to be interpreted as signed or unsigned integers.

Please read standard sources (e.g., Wikipedia) on the carry flag and overflow flag to learn more.

answered Mar 17 at 5:19
$\endgroup$
3
  • $\begingroup$ In general, it depends on the ISA. For example, MIPS has "add" and "addu" instructions, the latter being unsigned addition. $\endgroup$ Commented Mar 17 at 7:30
  • $\begingroup$ @Pseudonym, Thank you for the correction! I didn't realize that. $\endgroup$ Commented Mar 17 at 8:12
  • $\begingroup$ MIPS, of course, doesn't have flag registers, so this is why I added it as a comment rather than an answer. The purpose of addu and subu is so that the operation traps on overflow or underflow which, in the days before modern branch prediction, was a highly desired feature when implementing languages with boxed integers. $\endgroup$ Commented Mar 18 at 2:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.