Section3.5Mathematical Meaning of C and V Flags.¶ permalink

The results from Section 3.2 and Section 3.4 yield the following rules when adding or subtracting two \(n\)-bit numbers:

If your algorithm treats the result as unsigned, the carry condition flag (C) is zero if and only if the result is within the \(n\)-bit range; V is irrelevant.

If your algorithm treats the result as signed, the overflow condition flag (V) is zero if and only if the result is within the \(n\)-bit range; C is irrelevant.

The CPU does not consider integers as either signed or unsigned. Both the C and V are set according to the rules of binary arithmetic by each arithmetic operation. The distinction between signed and unsigned is completely determined by the program. After each addition or subtraction operation the program should check the state of the C for unsigned integers or the V for signed integers and at least indicate when the sum is in error. Many high-level languages do not perform this check, which can lead to some obscure program bugs.

The codes used for both unsigned integers and signed integers are circular in nature. That is, for a given number of bits, each code “wraps around”. This can be seen pictorially in the “Decoder Ring” shown in Figure 3.5.1 for three-bit numbers.

Subsection3.5.1Exercises

1

Use the “Decoder Ring” in Figure 3.5.1 to perform the following arithmetic. Indicate whether the result is “right” or “wrong”.

For unsigned arithmetic, use the two inner rings and pay attention to passing over the top (C). For signed arithmetic, use the outer and inner rings and pay attention to passing over the bottom (V).

Start at the tic mark for \(1\text{,}\) move \(3\) tic marks CW, giving \(4 = \binary{100}_{2}\text{.}\) We did not pass the tic mark at the top, so C = \(\binary{0}\text{,}\) and the result is correct.

Start at the tic mark for \(3\text{,}\) move \(4\) tic marks CW, giving \(7 = \binary{111}_{2}\text{.}\) We did not pass the tic mark at the top, so C = \(\binary{0}\text{,}\) and the result is correct.

Start at the tic mark for \(5\text{,}\) move \(6\) tic marks CW, giving \(3 = \binary{011}_{2}\text{.}\) We passed the tic mark at the top, so C = \(\binary{1}\text{,}\) and the result is wrong.

Start at the tic mark for \(+1\text{,}\) move \(3\) tic marks CW, giving \(-4 = \binary{100}_{2}\text{.}\) We passed the tic mark at the bottom, so V = \(\binary{1}\text{,}\) and the result is wrong.

Start at the tic mark for \(-3\text{,}\) move \(3\) tic marks CCW, giving \(+2 = \binary{010}_{2}\text{.}\) We passed the tic mark at the bottom, so C = \(\binary{0}\text{,}\) and the result is wrong.

Start at the tic mark for \(+3\text{,}\) move \(4\) tic marks CCW, giving \(-1 = \binary{111}_{2}\text{.}\) We did not pass the tic mark at the bottom, so C = \(\binary{0}\text{,}\) and the result is correct.

2

Add the following pairs of 8-bit numbers (shown in hexadecimal) and indicate whether your result is “right” or “wrong.” First treat them as unsigned values, then as signed values (stored in two's complement format).

You will have two “right/wrong” answers for each sum. The computer performs only one addition, setting both the C and V according to the results of the addition. It is up to the program to test the appropriate flag depending on whether the numbers are being considered as unsigned or signed in the program.

Add the following pairs of 16-bit numbers (shown in hexadecimal) and indicate whether your result is “right” or “wrong.” First treat them as unsigned values, then as signed values (stored in two's complement format).

You will have two “right/wrong” answers for each sum. The computer performs only one addition, setting both the C and V according to the results of the addition. It is up to the program to test the appropriate flag depending on whether the numbers are being considered as unsigned or signed in the program.