Adder with Accumulator and Status Flags

The bit width of the adder/subtractor will extended to 16 bits. We also add a 16-bit register (accumulator) for storing the result of the computation when we get a clock signal. Furthermore, status flags are provided (see also the description below).

Status Flags

After a clock signal the computed result gets stored in the accumulator and the following status flags are updated:

  • The zero flag (ZF) is set if and only if the computed bit pattern

  • The carry flag (CF) is set if and only if an unsigned integer overflow occurred.

    With \(a := u(a_15, \dots, a_0)\) and \(b := u(b_15, \dots, b_0)\) the condition for setting the flag can be described more formal:

    • In case of an addition an overflow is equivalent to \(a \oplus b = (a + b) \bmod 2^{16} \neq a + b\).

    • And in case subtraction equivalent to \(a \ominus b = (a - b) \bmod 2^{16} \neq a + b\)

  • The overflow flag (OF) is set if and only if a signed integer overflow occurred.

    With \(a := s(a_15, \dots, a_0), b := s(b_15, \dots, b_0)\) and \(\tilde{s} := s(s_15, \dots, s_0)\) the condition for a signed overflow

    • in an addition is equivalent to \(a < 0 \land b < 0 \land \tilde{s} \geq 0 \lor a \geq 0 \land b \geq 0 \land \tilde{s} < 0\).

    • And in a subtraction equivalent to \(a < 0 \land b \geq 0 \land \tilde{s} \geq 0 \lor a \geq 0 \land b < 0 \land \tilde{s} < 0\).

  • The sign flag (SF) is set if and only if the signed value of the computed bit pattern is negative, i.e. its most significant bit equals one.

Relational Conditions

When we have two integer values \(a\) and \(b\) we can use the status flags to check relational conditions, e.g. \(a = b\), \(a < b\). This always happens in two steps:

  • First the difference \(a - b\) is computed with a subtraction,

  • Second the status flags are checked.

Note that the bit pattern of the difference is not used. Only the status flags set by the subtractor.

Equality and Inequality

In this case it is not relevant whether the bit patterns are supposed to represent an unsigned or signed value.

Relational Condition

Unsigned / Signed

\( a = b \Leftrightarrow a - b = 0 \)

\(\text{ZF} = 1\)

equal/zero

\(a \neq b \Leftrightarrow a - b \neq 0 \)

\(\text{ZF} = 0\)

not equal/not zero

Ordered Relations

In this case one has to distinguish whether both operands are either representing an unsigned or signed value.

Relational Condition

Unsigned

Signed

\(a > b \Leftrightarrow a - b > 0\)

\(\text{ZF=0} \land \text{CF} = 0\)

above / not below equal

\(\text{ZF}=0 \land \text{OF} = \text{SF}\)

greater / not less equal

\(a \geq b \Leftrightarrow a - b \geq 0\)

\(\text{CF} = 0\)

above equal / not below

\(\text{OF} = \text{SF}\)

greater equal / not less

\(a < b \Leftrightarrow a - b < 0\)

\(\text{CF} = 1\)

not above equal / below

\(\text{OF} \neq \text{SF}\)

not greater equal / less

\(a \leq b \Leftrightarrow a - b \leq 0\)

\(\text{ZF=1} \lor \text{CF} = 1\)

not above / below equal

\(\text{ZF}=1 \lor \text{OF} \neq \text{SF}\)

not greater /less equal