# CPU has built-in circuitry to do simple arithmetic operations

## Main Question or Discussion Point

Hi

They say that a CPU has built-in circuitry to do simple arithmetic operations such as addition, subtraction, etc. and logical operation such as greater than, less than, etc. I don't understand how could some circuit(s) do these things? Could you please explain it with some simple example? Thank you for your time and help.

Related Programming and Computer Science News on Phys.org
Well the CPU is made of billions of transistors. Those transistors are combined into different logical elements, called gates, that perform logical functions(AND, OR, NAND, NOT, etc). Different combinations of different gates can be made to perform a specific function such as those you have listed. In the CPU that part is called the ALU - arithmetic logic unit.

That is the simple explanation. I'm sure there are people here who know more about this stuff than me that can explain it in further detail.

Thanks. Matrix.

It would be nice and really helpful if someone could show me that how a CPU add 1+1 to 2 or subtract 1-1 to 0.

jtbell
Mentor
Thanks, JT. If that's what I'm supposed to know first, then I will read them and come back soon. I have some knowledge of the gates but don't understand half-adder and full-adder circuits.

OmCheeto
Gold Member
I just whipped up something that can add 1 plus 1:

Does this make sense?

Mark44
Mentor
It would be nice and really helpful if someone could show me that how a CPU add 1+1 to 2 or subtract 1-1 to 0.
It's probably helpful to look at this stuff at different levels. For instance, at the level of CPU instructions, you can do addition by using the ADD instruction, as in the following x86 assembly code:
Code:
mov ax, 1  ; Move the value 1 into the AX register
add ax, 2   ; Add 2 to the value in the AX register
The text after each semicolon is a comment.
After the second instruction above, the value 3 will be in the AX register.

Subtraction can be done using the SUB instruction, like so:
Code:
mov ax, 1  ; Move the value 1 into the AX register
sub ax, 1   ; Subtract 1 from the value in the AX register
After the second instruction executes, the value in the AX register will be 0.

If you're interested in knowing how the CPU actually carries out the addition and subtraction, then you need to look into things at the level of logic gates and adders, as jtbell suggests. Speaking for myself, my interest is more on the programming side than the electrical/electronic engineering side. I take it on faith that the engineering guys know how to make a chip add, subtract, and so on. This is usually, but not always, a safe bet. In the case of the Pentium 1 processor back in about 1992 or so, the microcode for division had an error that was difficult to detect. Fixing the problem by recalling these chips cost Intel about \$1.5 Billion.

chiro
Hi

They say that a CPU has built-in circuitry to do simple arithmetic operations such as addition, subtraction, etc. and logical operation such as greater than, less than, etc. I don't understand how could some circuit(s) do these things? Could you please explain it with some simple example? Thank you for your time and help.
For arithmetic you use what is known as "adder" circuits. They work just like the way you add numbers in base 10 except that a computer uses base 2. It takes two bits computes the result and also the carry bit. You basically have a chain of these to calculate the full result and the number of adders is basically the number of bits that is the size of the number your adding.

Things that compare use logic. Lets say you want to see if a number is a certain number and if its you do something. You can take the value in some register and subtract your 'number' from it. If it equals zero then you do something. One way we can test this is to invert the bits of the result and then compute the result bit to be (bit0 AND bit1 AND bit2 ... AND bitn) which will give 1 if true and 0 if not. Then this bit is used to do something.

A way to say compare if one number is greater than the other with unsigned bits is to simply do a subtraction involving two words with the CPU using n+1 bits and then checking the sign bit at the end of the computation. If you want to know how signed bits work look at "2's complement" on wikipedia.

So in a nutshell arithmetic is done using adders and with multiplication you just use AND gates as well as adders in exactly the same way you do multiplication using pen and paper.

When you use comparisons you basically use a formula that sets a bit to some state (1 or 0) to represent if the comparison was true or false.

Thank you, everyone.

Please let me remind you again that I'm a very much layman in these techno subject, so please be as much specific as possible. Thanks.

Here K2 really helped me to understand what a logic gate actually is:
https://www.physicsforums.com/showpost.php?p=3016758&postcount=9

The logic gates AND, OR, etc. are simply 'mathematical' or 'boolean' objects and in real life are implemented through circuits made from transistors. Right?

So, if this is possible to explain to me with the limited extent of knowledge, then would you please tell me how some circuits would be implemented to get "01" (which stands for '2') once the two inputs of the circuits are '1' and '1', i.e. 1+1=01 (in binary)? Could you please help me? If this seems a complex problem to explain then you could choose some less complex example to explain what I seek to know. Thanks a lot for the guidance and your time.

rcgldr
Homework Helper
The logic gates AND, OR, etc. are simply 'mathematical' or 'boolean' objects and in real life are implemented through circuits made from transistors.
AND, OR, and XOR logic can be created from NAND or NOR gates.

http://en.wikipedia.org/wiki/Nand_gate
http://en.wikipedia.org/wiki/NAND_gate

The same thing could be done with NOR gates (although NAND gate based logic would be more common):

http://en.wikipedia.org/wiki/Nor_gate
http://en.wikipedia.org/wiki/NOR_logic

To store and hold data you need a latch (flip flop) for each bit:

http://en.wikipedia.org/wiki/Latch_(electronics)

To implement a true multi-bit adder, you need an adder that includes 3 bit inputs, the two input bits plus the carry bit from the lower order add bit.

Although it's unlikely you'll run into this, some old computers used different names for these operations in their instruction sets.

selective_set
(control) OR (data)
for each 1 bit in control or data word, the data bit is set in the output word

selective_copy
(control) AND (data)
for each 1 bit in control word, a bit from the data word is copied, otherwise the bit is set to 0

selective_complement
(control) XOR (data)
for each 1 bit in control word, a bit from the data word is complemented, otherwise the bit is copied

selective_clear (few computers have this instruction)
(~control) AND (data)
for each 1 bit in control word, a bit is cleared, otherwise the bit is copied

Last edited:
PhilKravitz
So, if this is possible to explain to me with the limited extent of knowledge, then would you please tell me how some circuits would be implemented to get "01" (which stands for '2') once the two inputs of the circuits are '1' and '1', i.e. 1+1=01 (in binary)? Could you please help me? If this seems a complex problem to explain then you could choose some less complex example to explain what I seek to know. Thanks a lot for the guidance and your time.
see post #6

i want cpu circuit to do arithmetic operations and logical operations

anyone can help me to design cpu