since unsigned and signed integers uses same components to compute then how does the overflow and carry flags are set?
1 Answer 1
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.
-
$\begingroup$ In general, it depends on the ISA. For example, MIPS has "add" and "addu" instructions, the latter being unsigned addition. $\endgroup$2025年03月17日 07:30:53 +00:00Commented Mar 17 at 7:30
-
$\begingroup$ @Pseudonym, Thank you for the correction! I didn't realize that. $\endgroup$2025年03月17日 08:12:11 +00:00Commented 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$2025年03月18日 02:01:07 +00:00Commented Mar 18 at 2:01