[Music] welcome to another video from explaining computers this time i'm going to answer a question that i'm very frequently asked which is how do cpus how do central processing units actually work so let's go and delve into the inner functions of the microprocessor cpus or central processing units are single chip digital machines for executing a sequence of instructions called a program cpus are mainly built from millions or billions of miniature transistors but to understand their operation it's best to focus on their internal structure or architecture today most cpus form part of a von neumann architecture
this includes a control unit that coordinates the processor's activities as well as an arithmetic logic unit that performs mathematical and logical operations as we're about to see cpus in the von neumann architecture need to constantly exchange data with a computer's memory whilst also interacting with input and output peripherals personally whenever i think about cpu operations i remember the z80 cpu that was found in the sinclair zx81 and other early micro computers the zx81 was the first computer i ever owned and back in the 1980s i learned to program it z80 cpu in machine code machine
code is the most basic form of programming as it takes direct control of individual cpu components so if you want to deeply understand how a cpu works you need to delve into machine code here we see the internal architecture of the z80 modern cpus are far more complicated but by looking at the z80 we can more easily highlight a cpu's basic components just like in the von neumann block diagram the z80 has a control unit to coordinate its operations as well as an arithmetic logic unit or alu there are also a number of registers which
are very small memory banks just a few bits in size here on the z80 all registers have either an 8 or 16 bit capacity although in a modern processor registers are typically 32 64 80 128 256 or 512 bits in size as we will see in the next section registers can be used to store instructions or data as well as memory locations which are known as addresses in order to interact with the rest of the computer a cpu has three communications channels called buses firstly we have the address bus which is used to communicate memory
addresses to ram or other components secondly there is the data bus which is used to communicate data and finally there is the control bus which is used to exchange control signals with other components so for example if a cpu needs to read the value in a particular memory location it will use its address bus to communicate the location address the control bus to indicate what it wants to read the data from it and the data bus to actually transfer the data value in order to run programs cpus rely on a clock signal as is generated
by a clock chip and a quartz crystal located somewhere on the computer's motherboard today clock signals are measured in gigahertz with modern cpus able to process billions of program instructions per second the most fundamental aspect of cpu operation is the fetch decode instruction cycle each part of this cycle is triggered by a clock pulse signal with the cpu first fetching an instruction from memory before decoding it executing it and moving on to repeat the process of fetching decoding and executing the next instruction and so on to keep track of what it's doing and to perform
its work a cpu relies on several different registers at the most basic level there is a program counter also known as the instruction pointer which contains the address of the next instruction to be fetched when the fetch decode execute cycle begins the cpu reads the instruction held at this memory location and stores it in its instruction register ready to be decoded and executed the program counter is also incremented ready for the next instruction cycle it's important to appreciate that in machine code instructions are simply binary numbers that correspond to a particular command the decode part
of the fetch decode execute cycle therefore looks up what command the value held in the instruction register represents many instructions perform arithmetic or logical operations on one or more registers whilst others move data between registers and memory or jump to other parts of the program if certain logical conditions are met note that here all registers data values and memory addresses are illustrated as being eight bits in size to keep our graphics as clear as possible in order to make machine code easier to write it is often expressed in a human-friendly form known as assembly language
this is then converted into machine code by a program known as a compiler so it can be run by a cpu to provide an example of assembly language and related cpu operation let's imagine that we have two numbers held into consecutive memory addresses and that we want to add them together and store the result in the next memory address assuming that we already have the memory location of the first number stored in register 10 the assembly language to make this happen could look something like this what we see here is a small snippet of x86
assembly language in which the registered names are prefixed by a percent sign and memory addresses are contained in brackets what the code does is to move the value held at the memory address stored in register 10 to register 12 to increment the value in register 10 so that it points to the next memory address and to move the value contained at this memory address to register 13. the program then adds together the values held in registers 12 and 13 with the result ending up in register 13 before incrementing the address location held in register 10
once again and finally writing the result held in register 13 back to this memory location note that in x86 machine code it is possible to add a register value directly to a value in a memory location so this example could be coded in other ways and if all of this seems rather tedious then it very much is but moving data and addresses into and out of registers and performing mathematical and logical operations on them or what the inner workings of a cpu are all about so far in this video i've simplified matters as much as
possible with cpu architectures that only include a handful of registers and other basic elements and which execute just one instruction at a time however most modern cpus are far more complex for start most modern cpus have many cores each of which can loop through its own fetch decode execute cycles in order to allow many instructions to be processed at the same time in addition modern cpus contain several levels of cache which is very fast memory into which data and instructions are loaded before being accessed by the rest of the cpu modern cpus also contain complex
predictive algorithms that try to anticipate the memory values that a cpu is likely to require so they can be pre-loaded from ram to cache before they are needed in addition to their control and arithmetic logic units many modern cpus also contain other components including a floating point unit or fpu for carrying out highly accurate mathematical calculations also included in most modern cpus are display controllers or graphics processing units as well as media encoders some modern cpus even have integrated neural accelerators or intelligent processing units to assist with machine learning applications even so at the most
basic level the way that all cpus work is as previously described with data and addresses being loaded into registers from memory or other storage before being subject to mathematical or logical manipulation and written back to storage or used to trigger control signals modern microprocessors are some of the most amazing machines ever created and have become the bedrock of human civilization hopefully in this video i've given you some idea about how microprocessors about how cpus actually work and if you'd like me to do the same for other computing components please let me know down in the
comments section but now that's it for another video if you've enjoyed what you see there please press that like button if you haven't subscribed please subscribe and i hope to talk to you again very soon you