Close

More about subtraction

A project log for 4 bit TTL ALU

Alternative for the 74LS181 ALU, built in One Square Inch

roelhroelh 01/01/2019 at 11:391 Comment

The ALU has an ADD function, that can also be used for subtraction.

Suppose we want to subtract a number A from the number B. We can simply take "minus A" and add it to number B with the adder that we already have.

How do we get "minus A" ? In the 2-complement form that is mostly used, we can make a number negative by inverting all bits, and then add one to the result. But this would need an extra adder just for adding one ! There must be a better way....

To subtract, we can also add the inverted bits of A to B and add the extra one afterwards. But the ADD function has a carry input, When the carry input is high, one is added to the result. So for subtraction, all we have to do is to invert al bits of A, and add it to B while the carry input is high !

How does this work when two 4-bit ALU's are cascaded to form a 8-bit ALU ? Especially, how does the carry signal, that connects both 4-bit ALU's, behave ? As an example I will use two 8-bit numbers A and B, in groups of four bits.

A = 0000 0011  (decimal 3)
B = 0010 1001  (decimal 41)

A is bitwise inverted, producing   minus A = 1111 1100

Adding the lowest four bits of minus A to the lowest 4 bits of B, and also adding one because the carry is high for subtracting, we get:

lowest 4 bits: 1100 + 1001 + 1 = 0110  (with Cout=1)

The sum of 1100 and 1001 does not fit in 4 bits, so an output carry is generated ! The output carry is high, and is connected to the carry-in of the next 4 bits. That is convenient, because we already saw that for subtraction, the input carry must be high. 

So now the upper 4 bits are added, together with the carry:

upper 4 bits: 1111 + 0010 + 1 = 0010  (with Cout=1).

Total result 0010 0110 (decimal 38). So the result is correct for 41 - 3.

BORROW

In the previous example, the lower 4 bits of A represented the value 3 and the lower 4 bits of B represented the value 9. 3 can be subtracted from 9, there will be no borrow. What if A is bigger than B (for these 4 bits) ? For example:

A = 0000 0101  (decimal 5)
B = 0010 0010  (decimal 34)

 Again, A is bitwise inverted, now giving 1111 1010.  Adding the lowest 4 bits to the lowest for bits of B and adding one, gives:

lower 4 bits: 1010 + 0010 + 1 = 1101  (with Cout=0).

 This time, no carry is generated ! This means, that a borrow has to take place. So now, the upper 4 bits are added without adding one:

upper 4 bits: 1111 + 0010 = 0001  (with Cout=1)

 Total result 0001 1101 (decimal 29). The result is correct for 34 - 5.

I hope this will clarify how the subtraction function works !

Discussions

Yann Guidon / YGDES wrote 01/03/2019 at 21:37 point

This has do be reminded :-)

  Are you sure? yes | no