I am wanting to design a simple calculator like what you would find in the dollar store. Something like this:
This little guy can display a number 8 digits long, meaning 99999999 is the largest number that can be displayed in this format.
I am familiar with how a simple ALU works with two registers as input.
Now, if I where to use a 32bit register for calculations, then everything would be OK since the register can hold 99999999 just fine. However, I want to understand the process of using two smaller registers to hold bigger numbers and how that works with an ALU?
Best Answer
You can manipulate arbitrarily wide numbers using a finite width ALU. However, multiple operations are required when the number is wider than the ALU.
To illustrate this, I'll use a PIC 18 as example. This architecture can manipulate only 8 bit numbers directly. To add the 8 bit quantities VAR1 and VAR2 and put the result into SUM would take code like this (assuming banking isn't needed or the bank is already properly set up):
If instead VAR1, VAR2, and SUM were 32 bit variables, the code would be:
Each byte is handled separately. Note the use of ADDWFC instead of ADDWF for all but the first byte. This instruction adds the two bytes but also adds the carry from the previous ALU operation.
Hopefully you can see that this approach can be used to add two arbitrarily large numbers. Wide arithmetic is possible, just that it takes longer and requires multiple trips thru the ALU to perform the whole operation.
Operations like AND, OR, XOR, etc, are bit-wise independent. Each output bit can be computed given only the corresponding input bits. Such operations scale linearly with the width of the values being processed. Addition requires the two input bits but also the result of the carry from the next lower bit. That requires one bit of state to be kept between successive operations, but still scales linearly with the width of the number.
Multiply scales with the square of the width of the number since each chunk of one number must be multiplied by all chunks of the other number. Multiplying two 32 bit numbers, which can produce up to a 64 bit result, takes a lot longer than 8 x 8 on a 8 bit ALU, but it's all possible.