How does the processor perform the subtraction?

I have some basic doubts, but every time I sit to try my hand at interview questions, these questions and my doubts pop up.

Say A = 5, B = -2. Assuming A and B are 4 bytes, how does the CPU add A + B ?

I understand that A will have a signed bit (MSB) as 0 to denote a positive value, and B will have a signed bit as 1 to denote a negative integer.

Now that I want to print A + B in a C ++ program, the ALU (Arithmetic Logical Unit) addition module first checks the sign bit, and then decides to perform a subtraction and then follow the subtraction procedure. How subtraction will be my next question.

 A = 5 B = 2 

I want to do A - B computer will take 2 additions B and add A + 2 additions B and return it (after resetting the extra bit on the left)?

 A = 2 B = 5 

do A - B How does the computer do in this case?

I understand that any conditional logic such as if-then, etc. Everything will be done in hardware inside the ALU. calculating the complement of 2s, etc., excluding the extra bit, everything will be done in hardware inside the ALU. What does this ALU component look like?

+7
source share
5 answers

The whole reason we use the 2's complement is that adding the same numbers is positive or negative - there are no special cases to consider, as is the case with the 1's complement or sign-amplitude representations.

Thus, to find AB , we can simply negate B and add; that is, we find A + (-B) , and since we are using Supplement 2, we don’t worry if (-B) is positive or negative, because the addition algorithm works the same anyway.

+13
source

You are a little wrong about the sign bit. This is not just a sign bit - every negative number is converted to 2 additions. If you write:

 B = -2 

The compiler, when compiling into a binary file, will do this:

 1111 1111 1111 1111 1111 1111 1111 1110 

Now that he wants to add 5, ALU gets 2 numbers and adds them, a simple addition.

When the ALU receives a subtraction command, it is assigned 2 numbers - it does NOT for each bit of the second number, makes a simple addition and adds another 1 (because 2 additions are NOT for each bit +1).

Here it is necessary to remember that 2 supplements were chosen precisely in order not to do 2 separate procedures for 2+ 3 and 2+ (-3).

+7
source

Think in terms of two or three bits, and then understand that these things scale to 32 or 64 or any other bits.

Start with decimal

  99 +22 === 

To do this, we will have a few Migrate.

 11 99 +22 === 121 

9 plus 2 - 1, one, 1 plus 9 plus 2 - 2, one ...

The thing is, having noticed that to add two numbers I really need three lines, at least for some of them I may need to add three numbers. The same thing with an adder in alu, each column or bit strip, a one-bit adder, should be able to add two inputs plus a carry bit, and the result should be one bit and one bit carry.

Since you used 5 and 2, you can do 4 bit binary math

  0101 +0010 ===== 0111 

We did not need to transfer this, but you can see that the mathematics worked, 5 + 2 = 7.

And if we want to add 5 and -2

 11 0101 +1110 ===== 0011 

And the answer will be 3, as expected, not surprisingly, but we had the execution. And since it was an addition with a minus digit in two additions, it all worked, there was no sign bit, and then two additions were added, so we don’t just need to feed the bag with two operands.

Now, if you want to make a small difference, that if you want to subtract 2 from 5, you select the subtraction command, not add it. Well, we all learned that negation in a double complement means inverting and adding one. And we saw above that the two input adders really need a third input for the transfer, so that it can be cascaded, no matter how wide the adder is. Therefore, instead of doing two addition operations, invert and add 1, being the first addition of the real addition, all we need to do is invert and set the transfer:

Understand that there is no subtraction logic; it adds a negative result to the fact that you feed it.

  v this bit is normally zero, for a subtract we set this carry in bit 11 11 0101 five +1101 ones complement of 2 ===== 0011 

And what do you know, we get the same answer ... It doesn't matter what the actual values ​​are for any of the operands. if this is an add operation, you put zero on the carry bit and feed it into the adder. If this is a subtraction operation, you invert the second operand and put one on the transfer and feed it to the same adder. Everything that falls out falls out. If your logic has enough bits to store the result, then it all works, if you don't have enough space, then you overflow.

There are two types of overflow, unsigned and signed. Unsigned is just a carry bit. The signed overflow is related to comparing the carry bits in the msbit column with the execution bit for that column. For our maths above, you see that the carry and carry of this msbit column is the same, both are the same. And we know that in a 4-bit system there is enough space for the correct representation of the numbers +5, -2 and +3. A 4-bit system can display the numbers +7 to -8. Therefore, if you need to add 5 and 5 or -6 and -3, you will get a signed overflow.

 01 1 0101 +0101 ===== 1010 

Understand that the SAME add logic is used for signed and unsigned maths, it depends on your code, not the logic, to actually determine if these bits were considered two additions or without sign.

In the 5 + 5 example above, you see that the carry in the msbit column is 1, but the execution is 0, which means that the V flag, the signed overflow flag, will be set by logic. At the same time, the execution of this bit, which is the C flag, the carry flag will not be set. When 4-bit unsigned thinking can contain numbers from 0 to 15, so 5 + 5 = 10 is not overflowing. But when thinking, signed with 4 bits, can contain from +7 to -8, and 5 + 5 = 10 is an overflow with a signature, so flag V is set.

if / when you have the add with carry command, they accept the SAME scheme with summation, and instead of submitting the carry to zero, it gets the carry flag. Similarly, a subtraction with a borrow, instead of applying a carry of 1, has a value of 1 or 0 based on the status of the carry flag in the status register.

Multiplication is a whole different story, binary code makes multiplication much easier than when it is performed with decimal mathematics, but you must have different instructions without specifying a sign and a subscription. And separation is a separate, separate beast, so most instruction sets do not have a gap. Many of them have no breeding due to the number of windows or hours that it burns.

+7
source

In the entry of the 2nd supplement: not B = -B -1 or -B = (not B) + 1. It can be checked on a computer or on paper.

So, A - B = A + (not B) + 1, which can be done with:

1 bit not

1 increment

1 addition

There is a trick to inefficiently increase and decrease, using only nots and negatives. For example, if you start with the number 0 in the register and execute:

no, neg, not, neg, not, neg, ... the register will have values:

-1, 1, -2, 2, -3, 3, ...

Or as two more formulas:

not (-A) = A - 1

- (not A) = A + 1

+1
source

does the ALU (Arithmetic Logic Unit) addition module check the sign bit first, and then decides to perform a subtraction, and then follows the subtraction procedure

No, in one and two additions there is no difference between the addition / subtraction of a positive or negative number . ALU works the same for any combination of positive and negative values.

Thus, ALU basically does A + (-B) for A - B , but it does not need a separate negation step. Designers use a smart trick to make both add and sub adders in the same cycle length, adding only a multiplexer and NOT a gate together with a new Binvert input in order to conditionally invert the second input. Here is a simple ALU example that can do AND / OR / ADD / SUB

Full adder

Computer architecture - full adder

A real adder is simply a plus sign field inside that adds a with b or ~ b and carries over, producing the sum and result. This works by realizing that the two additions have -B = ~b + 1 , so a - b = a + ~b + 1 . This means that we just need to set the transfer to 1 (or cancel the transfer to borrow) and invert the second input (i.e. B). This type of ALU can be found in various books on computer architecture, such as

RISC-V ALU

In one addition, -B = ~b so that you do not set the carry when you want to subtract, otherwise the design will be the same. However, the two add-ons have another advantage: operations with signed and unsigned values ​​also work the same way, so you don’t even need to distinguish between signed and unsigned types. For one addition, you need to add the carry bit to the least significant bit, if the type is signed

With some simple simple modification of the aforementioned ALU, they can now do 6 different operations: ADD, SUB, SLT, AND, OR, NOR

6-function ALU

CSE 675.02: Introduction to Computer Architecture

Multiple -B it operations are performed by combining several separate -B it ALUs, as described above. In fact, ALUs are capable of performing much more operations, but they are designed to save space by a similar principle.

+1
source

All Articles