Tag Archives: subtraction

Mum, I’m building a CPU – Performing Subtraction via Negation

This week, we published the latest episode in our video blog covering our journey constructing a CPU.  In this episode (embedded below), Charlie explains how we perform subtraction using our 8-bit full adder board.  If you just want to see the video, keep scrolling down, however if you’re looking for a quick simple explanation of some of the concepts (2’s complement and signed binary), take a look at this:

Let’s say we want to perform 7 – 5.  This is the same as 7 + (-5).  If we can represent the -5 in binary, we can simply put both through our added.  Luckily, we can because our CPU uses Signed Binary Values.

What does this mean?  Basically, we use the first (most significant bit) as an indicator as to whether the number is positive (msf = 0) or negative (msf = 1).

00000000 = 0
11111111 = -1
11111110 = -2

Let’s say we have a binary representation for positive number x.  To convert this to a negative number, we will flip every bit (1 <= 0, 0 <= 1), and then add one.  For example:

00000101 = 5
11111011 = -5

Initially, we performed this operation using not gates, however as Charlie explains in the video it’s much more efficient to use XOR’s!

Note, I did write a similar (and still relevant) post about this earlier, but the information in that is no longer completely correct, hence this post.

If you’re new to the series, you can catch up here!

Mum, I’m Building a CPU – Performing Subtraction

Andrei’s 8-bit full (ripple) adder is now complete (see below), so Charlie and I have been able to look at subtraction.

Full Adder and Negator

Bottom: Full adder (4 bits on each board) – Top: Negator (now replaced).

To support subtraction, Charlie and I have build a negation board. When a data-value is sent to the addition board, one copy of it will go through a series of not gates whilst the other does not. Both then go through a multiplexer (74245) which selects which one to send on to the adder board. We did realise on Saturday however that the whole thing can be re-built with eight XOR gates, where we can choose whether to invert each bit by putting either a 0 or 1 on one of the inputs.

One thing that has taken some thinking about with this board is how we accurately perform subtractions – the problem lays with Binary. Lets take a simple example (most-significant-bit is a sign bit, b = binary):

00010011b = 19
now let’s swap the 0’s and 1’s
11101100b = 236
236-(2^8) = -20 (since we have 8 bits)

So, you see our result is one less than the desired one. The solution that we are working on is a negation of the carry flag before it is sent to the adder.  Once I’ve looked more at this I’ll update this post with a Truth-Table.