Let's look at bit level what happens in an addition. We're going to add 1111
to 1000
.
- First, take the Least Significant Bits (LSB, the most right). We have a 1 and a 0. Summed, that makes 1. There's no overflow here.
- Now, the second bit. Again a 1 and a 0, so again a 1. No overflow, again.
- The same goes for the third bit.
- The Most Significant Bit (MSB, the most left) is different: two ones. This gives summed a 0, with an overflow. That would mean the fifth bit would get an extra 1, however, there's no fifth bit, so the overflow is placed in the carry.
The end result is thus: 0111
Now, let's have a look at the status bits.
- Z: "Indicates that the result of an arithmetic or logical operation (or, sometimes, a load) was zero." Is
0111
zero?
- V: "Indicates that the signed result of an operation is too large to fit in the register width using twos complement representation." Did we have an overflow?
- C: "used to indicate when an arithmetic carry or borrow has been generated out of the most significant ALU bit position." Was the MSB carried out?
- N: "Indicates that the result of a mathematical operation is negative." Is the result negative?
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):
movf var1, w ;get VAR1 into accumulator
addwf var2, w ;add VAR2 to it
movwf sum ;save the result
If instead VAR1, VAR2, and SUM were 32 bit variables, the code would be:
movf var1+0, w ;make byte 0 (low byte)
addwf var2+0, w
movwf sum+0
movf var1+1, w ;make byte 1
addwfc var2+1, w
movwf sum+1
movf var1+2, w ;make byte 2
addwfc var2+2, w
movwf sum+2
movf var1+3, w ;make byte 3 (high byte)
addwfc var2+3, w
movwf sum+3
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.
Best Answer
Please show how you are interconnecting the two devices, particularly the carry from the LSB to the MSB device. It looks like that may not be wired correctly.
The 74LS181 doesn't deal with signed numbers, just unsigned integers. It is up to you to interpret the values in the form you need.
The two devices should be interconnected similar to this: