{"id":4902,"date":"2019-03-30T07:00:50","date_gmt":"2019-03-30T07:00:50","guid":{"rendered":"https:\/\/gtechbooster.com\/?p=4902"},"modified":"2023-03-21T13:17:29","modified_gmt":"2023-03-21T13:17:29","slug":"the-computer-central-processing-unit","status":"publish","type":"post","link":"https:\/\/gtechbooster.com\/the-computer-central-processing-unit\/","title":{"rendered":"The Computer Central Processing Unit"},"content":{"rendered":"\n<p>The real complexity of any computer system resides in the processor, but  do you know how it works? I mean how it really works? How does the code  that you write turn into something that does something? When you know  how, it&#8217;s not magic &#8211; just a matter of &#8220;fetch&#8221; and &#8220;execute&#8221;. <\/p>\n\n\n\n<div class=\"gtech-migrated-from-ad-inserter-placement-2\" style=\"text-align: center;\" id=\"gtech-2757620104\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-3040844228\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/17207\" rel=\"noopener\" class=\"notrack\" aria-label=\"26001\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg 1024w, https:\/\/gtechbooster.com\/media\/2023\/01\/26001-768x960.jpeg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" width=\"500\" height=\"625\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div><p>So far we have looked at the overall workings of a computer and\nspecifically the memory principle. Memories are devices that when given\none input automatically produce an associated output &#8211; reading or will\nautomatically store the output if it is also presented. The memory\nprinciple and memory mechanisms are fascinating but we need more we need\n something that can execute instructions making use of the memory &#8211; we\nneed a processor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Principles Of Execution<\/h2>\n\n\n\n<p>The processor is quite another level of difficulty.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The processor is the computer<\/h3>\n\n\n\n<p>There really is no question of the validity of this assertion.<\/p>\n\n\n\n<p>If you don\u2019t believe me try running a program written for a PC on a Mac.<\/p>\n\n\n\n<p>The point is that computers with different processors are different \u2013\n computers with the same processor are just faster or slower.<\/p>\n\n\n\n<p>The details of memory management and caching my be impressive but the\n real complexity of any computer system resides in the processor and it\nis time to look more closely at how it does what it does.<\/p>\n\n\n\n<p>Even if you think you already know you still might find the  explanation interesting. The reason is that many books and courses don\u2019t  really tell you the whole story. They stop short and leave you with a  sense that the processor is somehow magic even though you know the rough  outline of how it all should work.<\/p>\n\n\n\n<p> Elsewhere we have&nbsp; discovered that what makes a computer is the intimate connection between processor and memory.  When the processor places an address on the address bus a particular  memory location is selected and either stores the data on the data bus  or places the data stored in the location on the data bus. <\/p>\n\n\n\n<p>Notice that this isn&#8217;t magic. There isn&#8217;t a little humanoid that goes  and finds a particular memory location by address and then retrieves  the contents for the CPU. The action is as automatic as a key in a lock.  The CPU puts the address on the address bus and this selects and  activates a particular memory location. The read\/write line sets the  memory location&#8217;s behavior and it either places its content on the data  bus or it &#8220;latches&#8221; or stores the contents of the data bus. All automatic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program Counter<\/h3>\n\n\n\n<p>This might well be the major operating principle of a computer but it\n leaves out what the processor actually \u201cdoes\u201d with the data.<\/p>\n\n\n\n<p>After all it is called a \u201cprocessor\u201d so presumably it doesn\u2019t just\nstore and retrieve bit patterns. We already know how binary patterns can\n be used to represent numbers and we know how Boolean logic can be used\nto manipulate them \u2013 with addition and subtraction.<\/p>\n\n\n\n<p>But this is only part of what goes on. When you first start to\nconsider the workings of the processor it is usually arithmetic that the\n focus falls on. The reason is that we often, mistakenly, think of\ncomputers as \u201ccomputers\u201d but for the vast majority of the time a\ncomputer is actually doing something other than arithmetic.<\/p>\n\n\n\n<p>Once you start looking a little more closely the magic seems to be\nmore to do with how this lump of silicon, or whatever it is made from,\ncan obey the commands in a program. How on earth does it look at the\nnext instruction in a program, work out what it means and then arrange\nthe immutable hardware to do it? Software may be soft but hardware is\nhard and it doesn&#8217;t change depending on what the instruction wants it to\n do.<\/p>\n\n\n\n<p>Once again there is a tendency to think of a little humanoid sitting\nwhere the processor is waiting for the next instruction to appear and\nthen doing whatever it commands. This is, of course not how it happens\nand it is all just as automatic as the memory storage and retrieval.<\/p>\n\n\n\n<p>The \u201ctrick\u201d that the processor performs seems very complex but it is\nall based on building the complex from the simple and the very regular \u2013\n but isn\u2019t this always the principle when it comes to computers?<\/p>\n\n\n\n<p>The first thing a processor needs is some way of keeping track of  where it has reached in the program. This is using a single internal  memory location, usually called the \u201cProgram Counter\u201d or PC \u2013 and it  doesn&#8217;t count programs! All internal memory locations within the  processor are called \u201cregisters\u201d for historical reasons and to indicate  that they are generally just a little more than simple memory locations.  For example, the PC register has two operations that it can perform. It  can be initialized to a set value and it can be incremented, i.e. it  can add one to the value stored in it.<\/p>\n\n\n\n<!--nextpage-->\n\n\n\n<h2 class=\"wp-block-heading\">The Fetch cycle<\/h2>\n\n\n\n<p>As well as the PC register, a processor also has an instruction register which is used to store the current program instruction. A system clock generator provides pulses that synchronize what happens in the entire machine \u2013 it doesn\u2019t have to be this way but non-clock synchronized machines are much more difficult to build and, by for the same reason, to describe!<\/p>\n\n\n\n<p>What happens is that the PC register contains the address of the next instruction and on the first clock pulse this address is placed on the address bus and a read cycle transfers the instruction into the instruction register. Notice that we are already using \u201clittle human\u201d descriptions of what happens. The address isn\u2019t \u201cplaced on the bus\u201d by some intelligent intervention. Instead there is a logic gate that responds to the clock pulse by enabling other logic gates to allow the contents of the PC register to set the address bus.<\/p>\n\n\n\n<p>This is called the Fetch cycle.<\/p>\n\n\n\n\n\n<p>If you want a more detailed description of the fetch cycle you also have to include delays that are necessary for everything to settle down. So the complete fetch cycle might be something like:<br><\/p>\n\n\n\n<ol class=\"is-style-default wp-block-list\"><li>On the rising edge of the clock pulse the PC register drives the address bus and the instruction register is set to read whatever appears on the data bus \u2013 i.e the next instruction. However this doesn\u2019t appear on the data bus immediately as it takes time for the memory to respond to the new address.<br><\/li><li>During the clock pulse the address decoder selects the RAM location that is addressed. The fact that the read\/write line is set to read means that the memory location automatically places its contents on the data bus<br><\/li><li>On the falling edge of the clock pulse the instruction register latches whatever is on the data bus and the PC register adds one to its contents.<\/li><\/ol>\n\n\n\n<p>Notice that the fetch cycle is always the same and nothing ever varies, i.e. it is easy to implement this using nothing but logic gates. Let the PC drive the address bus, wait a while for everything to settle and let the instruction register latch what is on the data bas \u2013 easy!&nbsp;<\/p>\n\n\n\n<p>Once the instruction has been loaded into the instruction register the PC register is automatically incremented by one. This makes sure that at the start of the next fetch cycle the very next instruction is \u201cfetched\u201d and the program progresses from beginning to end.<\/p>\n\n\n\n<p>So far so good, but what happens to the instruction that is in the instruction register?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Execute cycle<\/h3>\n\n\n\n<p>At the moment, with only a fetch cycle, running a program amounts to transferring each program word into the instruction register in turn but nothing actually gets done!<\/p>\n\n\n\n<p>The solution is to add the \u201cexecute cycle\u201d.<\/p>\n\n\n\n<p>After each fetch cycle the next clock pulse initiates an execution phase.<\/p>\n\n\n\n<p>The usual way of explaining the execute cycle is to say that the instruction is \u201cdecoded\u201d and then acted upon.<\/p>\n\n\n\n<p>The trouble with this explanation is that it is once again almost mystical and it brings to mind the image of someone living in the machine that looks at the instruction and then does what it says. This is of course still nonsense! What really happens is as automatic, regular and non-intelligent as every other aspect of the working computer.<br><br>Every instruction is composed of a number of parts or fields. Exactly how many and what type depends on the architecture of the processor and can be decided by the hardware designer, but there are usually at least two.<\/p>\n\n\n\n<p>The first part is called the \u201cop code\u201d. This is a simple binary value that specifies what the instruction will do.<\/p>\n\n\n\n<p>Most processors have other registers as well as the program counter and the instruction register and these are generally the subjects of instructions. For historical reasons the first general-purpose register is usually called the A register \u2013 the \u201cA\u201d standing for Accumulator \u2013 and a typical instruction is to load the A register from some specified memory location. Any additional general-purpose registers are usually called B, C, D and so on.<br><br>Each instruction has a unique operation code and this not only serves to identify it but it actually causes the computer to carry out the operation. The way that this happens is that the instruction register has a section corresponding to the fixed location of the op code in the instruction.<\/p>\n\n\n\n<p>In our example, shown in the diagram, the op-code corresponds to the top four bits. Each of the bits in the op-code is connected to some combinatorial logic called the instruction decoder which causes the processor to do whatever the op-code corresponds to.<\/p>\n\n\n\n<!--nextpage-->\n\n\n\n<h2 class=\"wp-block-heading\">Op-code<\/h2>\n\n\n\n<p>The op-code isn\u2019t just any old number; it has a structure.<\/p>\n\n\n\n<p>In our example the first two bits of the op-code act as a mini address that selects which register the operation is going to use \u2013<\/p>\n\n\n\n<p>11&nbsp; A register,<br>10&nbsp; B register,<br>01&nbsp; C register and<br>00&nbsp; D register.<\/p><div class=\"gtech-mid-cont\" style=\"text-align: center;\" id=\"gtech-3484685638\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-2304663258\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/17207\" rel=\"noopener\" class=\"notrack\" aria-label=\"26001\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg 1024w, https:\/\/gtechbooster.com\/media\/2023\/01\/26001-768x960.jpeg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" width=\"500\" height=\"625\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div>\n\n\n\n<p>The top two bits give the operation to be performed on the register that is selected \u2013 e.g.<\/p>\n\n\n\n<p>10 = load the register,<br>11 = add to the value already in the register<br>&nbsp;&nbsp;&nbsp; and so on.<\/p>\n\n\n\n<p>You can see that there is nothing magic in the instruction decoder. The lower two bits are fed into a set of And\/Or gates that provide a selection signal to the correct register, i.e. one of the four possible.<\/p>\n\n\n\n<p>The upper two bits are similarly decoded using And\/Or gates to derive a signal that makes the register \u201clatch\u201d or load whatever is on the data bus.<\/p>\n\n\n\n\n\n<p>As the processor becomes more complex there are more registers to select from and more operations to set up. However the operation of the instruction decoder is always the same. It picks out parts of the op-code and decodes them to a small number of control lines which change what logic elements are selected or deselected within the CPU.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Address Field<\/h3>\n\n\n\n<p>So now you can see that the execute phase works in an entirely automatic way to load the A register, but from where?<\/p>\n\n\n\n<p>The answer is usually that there is a second field in the op-code which is treated as an address. The address portion of the op-code is placed on the address bus at the start of the execute cycle and this determines which memory location the register in question is loaded from or stored to.<\/p>\n\n\n\n\n\n<p>Notice that what gets placed on the address bus depends on what register is active. During the fetch cycle it is the PC register that is active and it drives the address bus and the instruction register latches what is on the data bus. During the execute cycle it is the instruction register that is active and it drives the address bus and one of the registers latches what is on the data bus.<\/p>\n\n\n\n<p>It really is very simple.<\/p>\n\n\n\n<p>All we need to do now is put it all together.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fetch-Execute Cycle<\/h3>\n\n\n\n<p>Now we have a complete picture of what happens during the execute phase and we can even add to the description the necessary delays while everything settles down. Everything happens at times determined by the system or processor clock.&nbsp; Exactly when everything happens varies according to the particular type of processor but usually the rising and falling edges of the clock pulse are used to mark the moment things happen.<\/p>\n\n\n\n<p>For a register load instruction that has just been placed in the instruction register by the previous fetch this would be:<\/p>\n\n\n\n<ol class=\"is-style-default wp-block-list\"><li>On the rising edge of the execute clock pulse the register select bits are decoded and one of the register select lines is activated. The \u201caction\u201d part of the op-code is decoded and one of the register control lines \u2013 load in this case- is activated. The address portion on the instruction drives the address bus. Notice that all of this happens at the same time as there are separate logic gates for each part of the action.<br><\/li><li>During the clock pulse the memory decodes the address bus and the addressed location drives the data bus \u2013 and everything is given time to settle down.<\/li><li>On the falling edge of the clock pulse the selected register latches the data bus.<\/li><\/ol>\n\n\n\n<p>And, of course, after the execute phase there follows another fetch and so on until the program is complete.<\/p>\n\n\n\n<p>This is the basic principle of the computer and the way that the CPU works.<\/p>\n\n\n\n<p>You don\u2019t need any more hardware or additional operating principles to make a machine that does most of the things you need.<\/p>\n\n\n\n<p>For example, to add two numeric values you don\u2019t need a special instruction that adds two memory locations together. All you need is the instruction that loads the A register and one that adds the contents of a memory location to the A register\u2019s current contents. Notice that you don\u2019t need an additional sort of clock cycle in addition to the fetch and execute cycles. The add hardware is implemented so that instead of loading the register with the contents of the data bus the value on the data bus is added to the register.<\/p>\n\n\n\n<p>The operation of repeatedly adding values to the A register, i.e. \u201caccumulating\u201d a sum, is where the A register derives its name from.<\/p>\n\n\n\n<p>The extra hardware needed to make the A register into an accumulator is simply a full adder that adds the value on the data bus to the value on the output of the A register. The output of the full adder is fed back into the input of the A register.<\/p>\n\n\n\n\n\n<p>If you don\u2019t want to make things complicated you can even use the same hardware arrangement to implement a \u201cLoad A\u201d operation by simply blocking the output of the A register during an \u201cAdd to A\u201d operation. In practice, though, a modern processor will have a number of internal buses connecting the registers to the Arithmetic and Logic Unit&nbsp; or ALU \u2013 of which our full adder is just the beginning.<\/p>\n\n\n\n<p>Question: what does a full adder want to be when it grows up?<\/p>\n\n\n\n<p>Answer: an ALU\u2026<\/p>\n\n\n\n<!--nextpage-->\n\n\n\n<h2 class=\"wp-block-heading\">Jumps<\/h2>\n\n\n\n<p>Finally just to show you that everything you could possibly want can be easily included in this simple processor architecture consider how you might implement a \u201cjump to xxxx\u201d instruction.<\/p>\n\n\n\n<p>Normally the next instruction to be executed is in the next sequential memory location but a jump makes the instruction at xxxx the next one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How can this be achieved? Easy!<\/h3>\n\n\n\n<p>Just make the PC register correspond to register address 00 (rather than general purpose register D as suggested earlier). Now consider what&nbsp; \u201cload PC from address aaaa\u201d does. It loads the PC register with the value stored in \u201caaaa\u201d and so makes this the next instruction.<\/p>\n\n\n\n<p>Not quite what was required but it isn\u2019t difficult see how it can be modified to make it work exactly as specified.<\/p>\n\n\n\n<p>But this brings us to the interesting topic of addressing modes and that\u2019s another story.<\/p>\n\n\n\n<p>To make a computer capable of doing everything you need a computer to do you also need to add to jumps a conditional jump. A basic jump instruction is used for form loops \u2013 sections of code that repeat. Conditional jumps are used to implement conditionals which we know better as if statements.<\/p>\n\n\n\n<p>Extending our design to this is very easy. You need another register, the condition code register, and now when a register is loaded you provide the logic to set bits in the condition code register. If the register is zero then you set the zero bit, if negative the negative bit and if positive the postitive bit.<\/p>\n\n\n\n<p>You now modify the jump instruction to include logic that can test the condition bits. So now you have conditional jump instructions like jmpeq for jump if equal to zero, jmpn for jump if negative and so on.<\/p>\n\n\n\n<p>You should be able to see how to implement this \u2013 the condition bits simply enable or disable the loading of the PC register.<\/p>\n\n\n\n<p>With conditional jumps our computer can now do everything \u2013 it is Turing Complete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Beyond Fetch-Execute<\/h3>\n\n\n\n<p>If you know about modern processors you might be raising objections that this is not how they work. This is partly true. The first computers worked exactly as described but over time the fetch-execute cycle has been tweaked to make the machine work faster.<\/p>\n\n\n\n<p>In particular the fetch cycle, decoding and execution cycle are generally stretched out so that on a clock pulse an instruction is fetched while earlier instructions are decoded, and executed at the same time.<\/p>\n\n\n\n<p>This is generally called a pipeline and it is use by most modern machines but it is just a development on fetch-execute.<\/p>\n\n\n\n<p>Modern processors are so developed that you might have trouble seeing the simplicity of how they work but at the bottom it is still a fetch execute cycle that is responsible for computation.<\/p>\n<div class=\"gtech-end-cont\" id=\"gtech-3397674739\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-3287674486\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/17207\" rel=\"noopener\" class=\"notrack\" aria-label=\"26001\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg 1024w, https:\/\/gtechbooster.com\/media\/2023\/01\/26001-768x960.jpeg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" width=\"500\" height=\"625\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>The real complexity of any computer system resides in the processor, but  do you know how it works? I mean how it really works? How does the code  that you write turn into something that does something? When you know  how, it&#8217;s not magic &#8211; just a matter of &#8220;fetch&#8221; and &#8220;execute&#8221;. <\/p>\n","protected":false},"author":7,"featured_media":4903,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1062,208,540,1257],"class_list":["post-4902","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-features","tag-computing","tag-cpu","tag-computer-memory","tag-processor"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts\/4902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/comments?post=4902"}],"version-history":[{"count":0,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts\/4902\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/media\/4903"}],"wp:attachment":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/media?parent=4902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/categories?post=4902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/tags?post=4902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}