4
\$\begingroup\$

I am curious, generally speaking, as to what are the physical processes behind the workings of a CPU. I understand that the reason why computers use binary is because it means that data can be stored magnetically by direction, or electrically by charged/discharged.

For example, if I have two bytes 01110000 and 00001111, what electrically happens when, say, the second is subtracted from the first?

(I understand that a fully-detailed answer to this would be company secret, but I am asking out of curiosity and not for anything specific to a given manufacturer.)

\$\endgroup\$
  • \$\begingroup\$ Some topics that can help you: digital logic, combinational logic, CMOS/transistor logic. From here, you should be able to see how a combination of logic gates can be formed to do something like a binary 'add' or 'subtract'. \$\endgroup\$ – Jon L Nov 4 '11 at 20:43
  • 2
    \$\begingroup\$ visual6502.org/JSSim/index.html \$\endgroup\$ – starblue Nov 5 '11 at 8:31
  • 5
    \$\begingroup\$ Building a CPU: From Nand to Tetris in one semester \$\endgroup\$ – davidcary Nov 7 '11 at 3:53
  • \$\begingroup\$ +1 on davidcary's link. That course is fascinating, and will answer your question in as much detail as you could wish. \$\endgroup\$ – Nick Johnson Nov 1 '12 at 9:49
  • \$\begingroup\$ amazon.com/Code-Language-Computer-Hardware-Software/dp/… does a good job of painting the picture starting with known or commonly understood items and ideas and walking you through into logic gates and such \$\endgroup\$ – old_timer Nov 1 '12 at 19:07
9
\$\begingroup\$

A detailed answer is not so much a company secret, but rather the subject of several semesters at a University. Don't expect to get a complete answer here.

One of the best books on the topic is this one: http://www.amazon.com/Computer-Architecture-Fifth-Quantitative-Approach/dp/012383872X/ref=ntt_at_ep_dpt_1

There are also numerous books on digital logic design that would be good to read as well.

\$\endgroup\$
  • \$\begingroup\$ +1 for hennessy and patterson, I think I read the 3rd edition at university (showing my age) \$\endgroup\$ – dave Nov 1 '12 at 11:47
7
\$\begingroup\$

Bytes are manipulated in a CPU using an ALU (Arithmetic Logic Unit), which can typically perform both arithmetic (e.g. add) and logical (and, or, not, xor) operations between two operands, which may be fetched from either memory, or registers, or both memory and a register depending on the architecture of the computer.

Subtraction is usually performed via addition using two's complement arithmetic.

You might be interested in the book Code, by Charles Petzold,which talks about ALU's and much much more in his highly-rated book that descirbes how the insides of computers work starting with basic concepts.

\$\endgroup\$
4
\$\begingroup\$

You asked a simple question but the answer to "How does a CPU work?" can fill books. Yes modern processor use binary to represent numbers internally, and that is because it's easier to have and detect a voltage that is either high or low than various levels in between.

How binary numbers are added is no secret. The basic building block is a hardware logic circuit called a half adder. These are chained together to add any number of bits and to deal with the carries from the addition of lower order bits. Half adders and how they are chained together to make whole multi-bit adders is well covered out there, so I won't try to explain the details here. I think with the search terms "half adder" and "binary adder" you will easily find more than you ever wanted to know about them out there.

In practise, many adders are more complicated than a bunch of half adders chained together. This is soley to increase speed. Once you have looked up binary adder, you will see that information ripples from the low bit to the next bit to the next bit, etc. For wide addition, this takes time. There are various schemes to make this happen faster at the expense of more gates. The search term "lookahead carry" should yeild more than you want to know about this.

Hopefully this gets you started. Ask more specific questions if you want to know more after doing some reading.

Added:

Just to see what's out there I entered "half adder" and "binary adder" into Google. Lots of relevant-looking hits. Here is one that seems to explain it well enough. I'm sure there are others. This happened to be the first I clicked on, but it looks good enough:

www.play-hookey.com/digital/adder.html

\$\endgroup\$
2
\$\begingroup\$

To understand how addition and other operations are done by the ALU you can look at Nand2tetris course - chapter 2. They provide a simple ALU implementation (truth table and hints to build it yourself).

\$\endgroup\$
0
\$\begingroup\$

Actually it depends a LOT on architecture. I can only teach you Von Neumann. You first have an Aritmethic Logic Unit or ALU, some memory (registers and RAM), other type of program memory (ROM) and I/O stuff. You might also have a CU but I dont find it usefull unless you're using CISC instruction set. The ALU is what does all the actual processing, ALU englobes EVERY SINGLE boolean function you can possibly think of. Then this results get stored into registers or RAM (depends on whatcha wanna do) Registers are used commonly for inputing a specific component like the ALU or for outputing. RAM is a memory that can be used to save stuff and then spit just 1 one out at a time. Then The ROM is the programs. Which are just a bunch of electrical pulses commanding components. Doing stuff simpler than 1st grade math. So yeah. That's a quick overview of how CPUs work :) Also if you wanna learn more I suggest you a book called Elements Of Computing Systems, made by Noam Nisan and Shimon Shocken. Great book :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.