This leaves BCD subtraction, which is a little more interesting. Of course, multiplication is just successive addition, so we can get there from here. In fact, we can get to division from here, too, but we still need subtraction. When working in binary, we often use the one's complement or two's complement formats to represent negative numbers. This allows us to perform subtractions by simply adding the two numbers. (Subtracting is just taking the negative of the number to be subtracted and then adding them, right?) This is true in decimal, too, only we use either nine's or ten's complement representations.
Wait, what are nine's and ten's complements? To be honest, I don't remember learning this in school. It works like this -- consider the following subtraction problem.
Another way to present this problem would be as follows.
And this is the same as performing the following actions.
Let's take this step by step: First, let's perform the following subtraction.
Simply put, 9,722 is the nine's complement of -277. This does not solve our subtraction problem in and of itself, since we still need to perform a subtraction to obtain the nine's complement. Fortunately, there will never be a borrow from a next-higher-power column. (I'll leave it to you to figure it out.) We can use a lookup table or something similar to derive each individual digit.
The nine's complement of -277 is 9,722 (the same as performing the subtraction). Now we add 1 to generate the tens's complement.
Next, we add the ten's complement of -277 to 4,337.
We still need to subtract 10,000 from our result, but we can easily achieve this by throwing away (ignoring) the most significant digit. Our result is 4,060. If the leftmost digit is 5 or greater, we know the number is negative. To convert to the proper BCD representation (for display), we just use our lookup table to get the nine's complement. Reversing the numbers, we get the following (with a negative sign, which we might need to monitor separately, depending on our implementation).
Because the first digit is greater than or equal to 5, we know this is a negative number. Taking the nine's complement of 5,940 gives 4,060, so our answer is -4,060. Problem solved.
I've expended a lot of hot air explaining something you probably already knew. At least we now have the tools to perform decimal math in BCD using addition, subtraction, multiplication, and division. I'll leave it to you to implement this in hardware. Granted, this is pretty inefficient in terms of hardware and memory (something like 40% more hardware), and it is considerably slower and more klunky. However, we gain exact representation of our digits, and there is no issue with rounding/truncation. (We'll talk about this in another installment, I think.) This could be useful, but your mileage may vary. As always, you need to take a close look at your problem and figure out the best solution.
In my next column, I think I'll talk about some simple floating-point stuff. Until then, if you have any questions, please post them below as comments.