======================================= Adder with Accumulator and Status Flags [TOC] ======================================= 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). ---- VIDEO ------------------------------ https://www.youtube.com/embed/sOM0y2LTsoU ----------------------------------------- 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$ | $\text{ZF}=0 \land \text{OF} = \text{SF}$ | | | | | | | _above_ / _not below equal_ | _greater_ / _not less equal_ | +-------------------------------------------+---------------------------------------+-----------------------------------------------+ | $a \geq b \Leftrightarrow a - b \geq 0$ | $\text{CF} = 0$ | $\text{OF} = \text{SF}$ | | | | | | | _above equal_ / _not below_ | _greater equal_ / _not less_ | +-------------------------------------------+---------------------------------------+-----------------------------------------------+ | $a < b \Leftrightarrow a - b < 0$ | $\text{CF} = 1$ | $\text{OF} \neq \text{SF}$ | | | | | | | _not above equal_ / _below_ | _not greater equal_ / _less_ | +-------------------------------------------+---------------------------------------+-----------------------------------------------+ | $a \leq b \Leftrightarrow a - b \leq 0$ | $\text{ZF=1} \lor \text{CF} = 1$ | $\text{ZF}=1 \lor \text{OF} \neq \text{SF}$ | | | | | | | _not above_ / _below equal_ | _not greater_ /_less equal_ | +-------------------------------------------+---------------------------------------+-----------------------------------------------+