How does the processor work


This article is part of the sequence The Basics You Won’t Learn in the Basics aimed at eager people striving to gain a deeper understanding of programming and computer science.

In the previous post on these series, I explained what computer memory is and what different types of memories there are. This time, we will cover how the processor works, what components he has and how he uses them.

What does the processor do?

Now that we know how to store data in a computer, we have to learn how does the computer manipulate it. For this purpose, processors are used. They could be also called CPUs (Central Processing Units). What one processor does is pretty straightforward. He executes instructions. But how does that happen? Well, I will give you a pretty basic idea.

First off, whenever we load a program, it is loaded into the RAM memory. Of course, we could execute it directly from the hard disk. But that won’t be very cool. Why? Check this out.

Now, the processor is a pretty hard-working guy. But he is not so smart. People call him the brain of the computer, but he has no brains at all! The only thing that he does is to execute the orders (instructions) of his boss (the running program). He never asks what those orders are, or even care about that. That is why, there is a middleman between the program and the CPU, called the operating system. Its job is to limit the freedom of the running program in order to avoid potential damage to the system.

The processor is really good in calculations. The part of it responsible for that is called the ALU (Arithmetic Logic Unit). Now, whenever the processor does the calculations, he needs some kind of temporary storage. That is why a processor has registers. They are pretty limited in size, but really fast as they are inside the processor.

What does the (G)Hz stand for?

Well, every CPU has a pretty straightforward, but important component. It is called the clock. Its only purpose, is to generate on and off signals in a consistent manner. Just like your heart beats once a second, so does the clock generate high and low voltage signals (on/off, 1/0, etc.) consistently. But your heart beats on average – once per second. The processor clock, on the other hand, beats a LOT faster. The exact speed is measured in Hz (Hertz). One Hz means one cycle per second. That is one up and down signal per second. 3 GHz means 3 000 000 000 cycles per second. Your heart, on the other hand has a speed of 1 Hz, since it beats once per second. So the more Hz-es a CPU has, the faster the clock ticks.

So if an instruction takes 5 cycles of the clock to finish, the faster the clock of the CPU is, the faster the instruction will be executed.

But how do these up and down signals help? Using them, the CPU synchronizes its operations. Imagine you working in a factory, filling a basket of apples. It won’t be really cool if you remove the basket before it gets filled with apples, right?

Of course, you can always increase the speed of the processor clock. That is actually called overclocking. Some people do that to make their CPU faster, which usually leads to the whole system becoming faster as well. But that may cause trouble if the processor is not designed to handle such high speed. Want to see what could happen when a CPU overclocks? This funny video captures it beautifully.

32 bit vs 64 bit?

You have probably heard the notion of an N bit processor. The most common being 32 bit and 64 bit processors. These bits determine what would be the size of the numbers coming in and out of the processor.

So basically, more bits mean that the processor can store bigger values. These values could represent several things, the most important being:

An address in memory.
The size of an instruction.
The size of a pointer in your code.

Effects of the different architectures on the computer system

An address in memory – This is perhaps the most important one. The bigger the size of the address you can represent, the bigger the size of memory you can support. What’s the point of having a big storage if you cannot use it all. 32 bits can represent values up to 4294967296 which could more simply be expressed as 4 GB. That is why in normal conditions, 32 bit processors cannot use more than 4GB of ram. The keyword in the previous sentence is “normal”. There are techniques, which are used in some 32 bit processors to handle this issue, but not all operating systems support that. If you are interested in this however, read on here. 64 bit processors, on the other hand, can easily support more than 4GB of RAM.

The size of an instruction – An instruction is a number mapped to an operation. A sample “instruction set” would be:
1 – Read this value
2 – Write this value
3 – Go to this place

And so on. The larger this number is, the more instructions it can support. So this allows the processor to have more “features”. But this could also mean that the instructions used for a 32 bit processor are different from these used for the 64 bit processor. This could lead to programs compiled for 32 bit processors, not working on 64 bit processors or even worse, programs who sometimes work and sometimes don’t.

The size of a pointer in your code – Pointers are addresses of objects in code. If you use a 32 bit processor, your pointers’ size would be 32 bits. Similarly 64 bit processors -> 64 bit pointers. That leads to overall increase in program size. Of course, that depends on the usage of pointers in code.

Optimizing the processor

There are several popular techniques for making the processor even faster than normal. They are used in all modern CPUs. I will cover perhaps the most important technique of all – caching.

Perhaps the biggest bottleneck of the computer system is reading the memory. Why is that? Well we saw that the CPU works pretty fast. Modern CPUs  have a frequency of more than 3 GHz. But reading from memory is a LOT slower than that (More than 10 times slower). That is why, processors try to stock up more memory than required close to them in order to be able to execute subsequent instructions faster. This technique is called caching.

Whenever CPUs stack up instructions, they try to predict what memory will the processing unit need next and stock them up in caches. There are different types of caches – L1, L2, L3. The L stands for level and the smaller the number of the level is, the faster the cache but it is smaller in size as well.

Conclusion

This concludes my gentle introduction to processors. I believe this would aid you in gaining a more broad view of software in general. If you are interested in digging in more on the topic, I suggest this tutorial. Next time, I will start a series of posts about one of my favorite topics – binary numbers.

Any thoughts on the article? Leave your comments below.

Site Footer

BulgariaEnglish