Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\texttt} \newcommand{\hex}{\texttt} \newcommand{\octal}{\texttt} \newcommand{\prog}{\texttt} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section3.4Arithmetic Errors—Signed Integers

The number of bits used to represent a value is determined at the time a program is written. So when performing arithmetic operations you cannot simply add more digits (bits) if the result is too large, as you can do on paper. You saw in Section 3.1 that the C indicates when the sum of two unsigned integers exceeds the number of bits allocated for it.

In Section 3.3 you saw that carry is irrelevant when working with signed integers. In this section you will see that adding two signed numbers can also produce a result that exceeds the range of values that can be represented in the allocated number of bits.

The flags register, CPSR, provides a bit, the overflow condition flag (V), for detecting whether the sum of two \(n\)-bit, signed numbers stored in the two's complement code has exceeded the range allocated for it. Each operation that affects the overflow condition flag sets the bit equal to the exclusive or of the carry into the highest-order bit of the operands and the ultimate carry. For example, when adding the two 8-bit numbers, \(\hex{15}\) and \(\hex{6f}\text{,}\) we get:

carry \(\longrightarrow\) \(\binary{0}\) \(\binary{1} \longleftarrow\) penultimate carry
\(\binary{0001}\) \(\binary{0101}\) \(\longleftarrow\) \(x\)
\(+\) \(\binary{0110}\) \(\binary{1111}\) \(\longleftarrow\) \(y\)
\(\binary{1000}\) \(\binary{0100}\) \(\longleftarrow\) sum

In this example, there is a carry of zero and a penultimate carry of one. The V flag is equal to the exclusive or of carry and penultimate carry:

V = C ^ penultimate carry

where ‘^’ is the exclusive or operator. In the above example:

V = 0 ^ 1 = 1

There are three possibilities when adding two numbers:

  1. The two numbers are of opposite sign.

    We will let \(x\) be the negative number and \(y\) the positive number. Then we can express \(x\) and \(y\) in binary as: \begin{gather*} x = \binary{1} \dots\\ y = \binary{0} \dots \end{gather*} That is, the high-order bit of one number is \(\binary{1}\) and the high-order bit of the other is \(\binary{0}\text{,}\) regardless of what the other bits are.

    Now, if we add \(x\) and \(y\text{,}\) there are two possible results with respect to carry:

    1. If the penultimate carry is zero:
      carry \(\longrightarrow\) \(\binary{0}\) \(\binary{0} \longleftarrow\) penultimate carry
      \(\binary{1} \ldots\) \(\longleftarrow\) \(x\)
      \(+\) \(\binary{0} \ldots\) \(\longleftarrow\) \(y\)
      \(\binary{1} \ldots\) \(\longleftarrow\) sum
      This addition would produce V = 0 ^ 0 = 0.
    2. If the penultimate carry is one:

      carry \(\longrightarrow\) \(\binary{1}\) \(\binary{1} \longleftarrow\) penultimate carry
      \(\binary{1} \ldots\) \(\longleftarrow\) \(x\)
      \(+\) \(\binary{0} \ldots\) \(\longleftarrow\) \(y\)
      \(\binary{0} \ldots\) \(\longleftarrow\) sum

      This addition would produce V = 1 ^ 1 = 0.

    We conclude that adding two integers of opposite sign always yields \(\binary{0}\) for the overflow condition flag.

  2. Both numbers are positive.

    Since both are positive, we can express x and y in binary as: \begin{gather*} x = \binary{0} \dots\\ y = \binary{0} \dots \end{gather*} That is, the high-order bit of both numbers is \(\binary{0}\text{,}\) regardless of what the other bits are.

    Now, if we add \(x\) and \(y\text{,}\) there are two possible results with respect to carry:

    1. If the penultimate carry is zero:

      carry \(\longrightarrow\) \(\binary{0}\) \(\binary{0} \longleftarrow\) penultimate carry
      \(\binary{0} \ldots\) \(\longleftarrow\) \(x\)
      \(+\) \(\binary{0} \ldots\) \(\longleftarrow\) \(y\)
      \(\binary{0} \ldots\) \(\longleftarrow\) sum

      This addition would produce V = 0 ^ 0 = 0. The high-order bit of the sum is zero, so it is a positive number, and the sum is within range.

    2. If the penultimate carry is one:

      carry \(\longrightarrow\) \(\binary{0}\) \(\binary{1} \longleftarrow\) penultimate carry
      \(\binary{0} \ldots\) \(\longleftarrow\) \(x\)
      \(+\) \(\binary{0} \ldots\) \(\longleftarrow\) \(y\)
      \(\binary{1} \ldots\) \(\longleftarrow\) sum

      This addition would produce V = 0 ^ 1 = 1. The high-order bit of the sum is one, so it is a negative number. Adding two positive numbers cannot yield a negative sum, so the sum has exceeded the allocated range.

  3. Both numbers are negative. \begin{gather*} x = \binary{1} \dots\\ y = \binary{1} \dots \end{gather*} That is, the high-order bit of both numbers is \(\binary{1}\text{,}\) regardless of what the other bits are.

    Now, if we add \(x\) and \(y\text{,}\) there are two possible results with respect to carry:

    1. If the penultimate carry is zero:

      carry \(\longrightarrow\) \(\binary{1}\) \(\binary{0} \longleftarrow\) penultimate carry
      \(\binary{1} \ldots\) \(\longleftarrow\) \(x\)
      \(+\) \(\binary{1} \ldots\) \(\longleftarrow\) \(y\)
      \(\binary{0} \ldots\) \(\longleftarrow\) sum

      This addition would produce V = 1 ^ 0 = 1. The high-order bit of the sum is zero, so it is a positive number. Adding two negative numbers cannot yield a positive sum, so the sum has exceeded the allocated range.

    2. If the penultimate carry is one:

      carry \(\longrightarrow\) \(\binary{1}\) \(\binary{1} \longleftarrow\) penultimate carry
      \(\binary{1} \ldots\) \(\longleftarrow\) \(x\)
      \(+\) \(\binary{1} \ldots\) \(\longleftarrow\) \(y\)
      \(\binary{1} \ldots\) \(\longleftarrow\) sum

      This addition would produce V = 1 ^ 1 = 0. The high-order bit of the sum is one, so it is a negative number, and the sum is within range.