A question about how CPUs work

Davekenroy

New Member
I'm wondering if anyone can help me on understanding how a CPU "knows," if I may say, how to add, subtract, multiply, divide, etc.

To clairify further, I know that the CPU uses the the binary code to do those calculations. I just can't understand where or how it stores the information on how it does those operations.

Thanks in advance.
 
Thanks for responding, but that article doesn't explain how the CPU knows what the binary code is. Also to clarify further, I know that someone built a code system for the CPU. I just cannot understand how or where this information is stored. For example: Say I put in 7 into the AX register and 2 into the BX register, tell it to put the answer into the CX register. Does it actually do the calculation, or does it just come up with the binary code for 9?

If I'm completely off, please let me know. But also if you could let me know how it does it, that would be great.
 
I'm not a programmer. I thought that article would give you the info you needed. Sorry.
 
The CPU doesn't receive the instruction as a mnemonic, but rather as a binary string. It is then up to the CPU architecture, as in the physical composition of the thousands of switches, to decide what to do with the code.

The CPU receives the data, it takes the first byte (8 bits) which represents the instruction and decodes this in order to take appropriate action, next are values which may or may not be needed depending on the instruction, they represent memory addresses. The last order of bits is a specified value. It may be a value to store or a value to process (add, subtract, send to another process.) The timer inside the CPU assures that instructions are processed properly. E.G you don't want an instruction to access the wrong data.

In answer to your question, If you take 7 and put it in the AX register, the CPU will receive a 32bit binary sequence (or 64 or whatever your processor does) and it will process this. The way the switches are logically designed,the CPU will know to take the value 7 and store it in the AX register. The same goes for storing 2 in the BX register. Lastly, the CPU will receive the instruction to add value to AX in the 8-bit word. The next bits tell it to add the value currently in BX and the next bit sequence will tell it to store it in CX.

SHORT ANSWER: Yes, the CPU does the calculation. That, when you break through all of the mess of instruction codes and such, is the basic function of the CPU. To apply take binary input, perform some arithmetic on it and output the result. In diagrams of CPUs, you'll see there are whole sections dedicated to performing specific mathematical tasks.

I hope this helps. Basically, the logic of the switches determines how the CPU will act depending on whether each switch receives a '1' or a '0' at any one time. It's hard to explain and takes a whole text-book to learn, really. I would recommend studying binary arithmetic and boolean logic, then moving onto applying this to logic gates and switches. This should help explain the physical aspect of the CPU.

Are you doing a course in computer engineering? If not I would recommend it. It gets rather complex but it's to do with clocks and switches and a whole lot of boolean arithmetic.

Take a look at this.
 
Last edited:
Actually I'm studying A+ right now. I went through the microprocessors part and it didn't explain how the CPU "knows" how to do those operations. Would you suggest for me to take something else before I continue in A+?

Also I looked at the article you gave me, and I'm not understanding hardly any of how those logic gates work. I guess it may be because I haven't learned boolean logic yet.

Lastly, your answer is confusing a little bit, only because I don't have the background of how logic gates work and all that. I'm sure it is difficult to explain, and I was unaware that there were books on that topic. Actually I was unaware of logic gates until you had given me that link.

And thank you for trying to explain it to me, I have a little bit better understanding of how it works.
 
If you take 7 and put it in the AX register, the CPU will receive a 32bit binary sequence (or 64 or whatever your processor does) and it will process this. The way the switches are logically designed,the CPU will know to take the value 7 and store it in the AX register. The same goes for storing 2 in the BX register. Lastly, the CPU will receive the instruction to add value to AX in the 8-bit word. The next bits tell it to add the value currently in BX and the next bit sequence will tell it to store it in CX.
AX, BX and CX are 16 bit. Add an E to the start (EAX, EBX, etc) to get the 32bit version. Luckily the command you pass does that work for you. ADD AX, CX will take care not to have you add the full 32 bit registers as if you don't set the entire register, you don't know what's in it, you will get funny answers. Try using an uninitalized integer as a counter in C. ie
Code:
int main(){
int x;

for (int y=0;y<10;y++){
x++;
}

printf("x=%d", x);
}
You don't always get 10. You might sometimes but there's no gurantee.


To try and keep how it works somewhat simple, a CPU has an instruction decoder that it uses so it knows where to send an instruction. To the integer unit or floating point unit or SSE engine, etc. Then the part of the CPU that does the actual work goes about doing what ever it's told to do (basically it adds things together). The logic gate takes care of the rest. A logic gate is essentially a group of transistors that combines 2 values a certain way. It takes the on/off set in a register and does what it's set to do (add, multiply, divide, compare, bit shifting, etc) which usually involves another register. It's obviously more complicated than that but that's sort of the gist of it. I've typed this out in several sittings as I got distracted a few times. I think I've got it all down right but I'm sure someone will come along and say something if I don't :P

For A+ you really don't need to understand how a CPU does what it does. If you want to start designing ICs then I would suggest starting with a much simpler processor than something from a PC.
 
Thanks for that, I was pretty sure my explanation was terrible. I was tripping all over myself (7 in the morning or something it was, hadn't been to bed yet.)
 
Thanks for explaining, I do have a better understanding of it now. Though I don't understand how logic gates work, I'm sure that it would take a whole book to explain the whole thing. Not to mention I have no experience in boolean logic.

Also I'm not planning on getting into IC, I was just trying to learn all that I could about CPUs before continuing on in the rest of my A+. But now that I know it's not that simple, I think I'll leave that for later.

Thanks again for everyones help in this.
 
Back
Top