<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ cpu - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ cpu - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 31 May 2026 14:26:25 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/cpu/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Retrieve System Information Using The CPUID Instruction ]]>
                </title>
                <description>
                    <![CDATA[ When developing a bootloader/kernel, understanding the underlying architecture is crucial for optimizing performance and compatibility between software and hardware. One important yet sometimes overlooked tool available to engineers for querying and ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/retrieve-system-information-using-cpuid/</link>
                <guid isPermaLink="false">66fe6e2eac038fabde9a34bd</guid>
                
                    <category>
                        <![CDATA[ Kernel ]]>
                    </category>
                
                    <category>
                        <![CDATA[ operating system ]]>
                    </category>
                
                    <category>
                        <![CDATA[ cpu ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Nikolaos Panagopoulos ]]>
                </dc:creator>
                <pubDate>Thu, 03 Oct 2024 10:13:02 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/JMwCe3w7qKk/upload/bb94515f8210b64d35039199912a3b6c.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When developing a bootloader/kernel, understanding the underlying architecture is crucial for optimizing performance and compatibility between software and hardware.</p>
<p>One important yet sometimes overlooked tool available to engineers for querying and retrieving system information is the CPUID instruction.</p>
<h3 id="heading-what-is-the-cpuid-instruction">What is the CPUID Instruction?</h3>
<p>The CPUID instruction is a low level instruction, inside the heart of every modern x86 and x86-64 processor that allows the software to query the CPU for information about the processor and its supported features.</p>
<p>By invoking this instruction, you can gather information such as the processor’s model, family, internal cache sizes, and supported features like <a target="_blank" href="https://en.wikipedia.org/wiki/Single_instruction,_multiple_data">SIMD</a> or hardware virtualization. This can help you optimize performance and dynamically enable or disable supported features.</p>
<p>For bootloader or kernel developers, understanding what features a processor supports—such as hardware virtualization, cache sizes, or SIMD instructions—can ensure that the system runs efficiently and that the code you write is compatible across different CPUs. By utilizing the CPUID instruction, you can dynamically adjust your kernel’s behavior based on the specific processor it is running on.</p>
<p>In this article you will learn how to check if the CPUID instruction is available for your system, how it works and what information you can get from using it.</p>
<h3 id="heading-prerequisites">Prerequisites</h3>
<ul>
<li><p>Some knowledge of assembly language (for this example I use FASM)</p>
</li>
<li><p>Some knowledge of operating systems/kernels</p>
</li>
<li><p>Access to low-level debugging tools (for example, GDB) or hardware emulators like QEMU to test your bootloader/kernel on various platforms.</p>
</li>
</ul>
<h2 id="heading-step-1-check-for-cpuid-availability">Step 1: Check for CPUID Availability</h2>
<p>Before executing the CPUID instruction, it's important to determine whether the processor supports it, as not all CPUs are guaranteed to have this functionality. The following code checks the availability of the CPUID instruction by modifying and testing the ID bit (bit 21) in the EFLAGS register.</p>
<p>Here’s a picture from <a target="_blank" href="https://wiki.osdev.org/Expanded_Main_Page">wiki.osdev.org</a> that shows each bit of the EFLAGS register:</p>
<p><a target="_blank" href="https://wiki.osdev.org/CPU_Registers_x86"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727637307676/82ad4bf5-3906-49a3-a12a-6cb83cc852db.png" alt="82ad4bf5-3906-49a3-a12a-6cb83cc852db" class="image--center mx-auto" width="478" height="632" loading="lazy"></a></p>
<p>If the processor allows this bit to be toggled, CPUID is supported; otherwise, it is not. Here's how the detection process works:</p>
<p>(most people think that in Real mode 32 registers are not accessible. That is not true. All 32bit registers are usable)</p>
<pre><code class="lang-plaintext">cpuid_check:
    pusha                                ; save state
    pushfd                               ; Save EFLAGS
    pushfd                               ; Store EFLAGS
    xor dword [esp],0x00200000           ; Invert the ID bit in stored EFLAGS
    popfd                                ; Load stored EFLAGS (with ID bit inverted)
    pushfd                               ; Store EFLAGS again (ID bit may or may not be inverted)
    pop eax                              ; eax = modified EFLAGS (ID bit may or may not be inverted)
    xor eax,[esp]                        ; eax = whichever bits were changed
    popfd                                ; Restore original EFLAGS
    and eax,0x00200000                   ; eax = zero if ID bit can't be changed, else non-zero
    cmp eax,0x00
    je .cpuid_instruction_not_is_available
.cpuid_instruction_is_available:
    ;handle CPUID exists
.cpuid_instruction_not_is_available:
    ;handle CPUID isn't supported
.cpuid_check_end:
    popa                                  ; restore state
    ret
</code></pre>
<p><code>pusha</code>: Saves all the general purpose registers to ensure the original state can be restored at the end.</p>
<p><code>pushfd</code>: Saves the current EFLAGS register.</p>
<p><code>pushfd</code>: Stores a copy of the EFLAGS.</p>
<p><code>xor dword [esp], 0x00200000</code>: The code flips the ID bit (21) of the EFLAGS using the XOR operator.</p>
<p><code>popfd</code>: Restores the modified EFLAGS with the ID bit inverted.</p>
<p><code>pushfd</code>: Pushes the modified EFLAGS back to the stack.</p>
<p><code>pop eax</code>: Puts the modified EFLAGS (ID bit may or may not be inverted) in the EAX register.</p>
<p><code>xor eax, [esp]</code>: After the XOR operation, the EAX will contain the bits that were changed.</p>
<p><code>popfd</code>: Restores the original EFLAGS.</p>
<p><code>and eax, 0x00200000</code>: The <code>and</code> operation isolates the 21st bit (ID bit) by masking all other bits. After this operation the EAX register will contain either 0x00200000 (if 21 bit was changed which means CPUID is supported) or 0×00 (21 bit hasn’t changed, CPUID not supported).</p>
<p><code>cmp eax, 0x00</code>: The CMP instruction checks the result of the previous operation. If EAX equals 0×00, it means that the ID bit cannot be modified and the processor doesn’t support the CPUID instruction. If it is not zero, it means that the ID bit was flipped and your processor supports the CPUID instruction.</p>
<h2 id="heading-step2-how-to-use-the-cpuid-instruction">Step2: How to Use The CPUID Instruction</h2>
<h3 id="heading-get-cpu-features">Get CPU Features</h3>
<p>The CPUID instruction will return different information with different values in the EAX register.</p>
<pre><code class="lang-plaintext">mov eax, 0x1
cpuid
</code></pre>
<p>With EAX set to 1, the CPUID will return a bitfield in EDX, which will contain the following values. Different brands may give different meaning to these (source <a target="_blank" href="https://wiki.osdev.org/CPUID">https://wiki.osdev.org/CPUID</a>)</p>
<pre><code class="lang-c"><span class="hljs-keyword">enum</span> {
    CPUID_FEAT_ECX_SSE3         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">0</span>,
    CPUID_FEAT_ECX_PCLMUL       = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">1</span>,
    CPUID_FEAT_ECX_DTES64       = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">2</span>,
    CPUID_FEAT_ECX_MONITOR      = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">3</span>,
    CPUID_FEAT_ECX_DS_CPL       = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">4</span>,
    CPUID_FEAT_ECX_VMX          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">5</span>,
    CPUID_FEAT_ECX_SMX          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">6</span>,
    CPUID_FEAT_ECX_EST          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">7</span>,
    CPUID_FEAT_ECX_TM2          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">8</span>,
    CPUID_FEAT_ECX_SSSE3        = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">9</span>,
    CPUID_FEAT_ECX_CID          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">10</span>,
    CPUID_FEAT_ECX_SDBG         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">11</span>,
    CPUID_FEAT_ECX_FMA          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">12</span>,
    CPUID_FEAT_ECX_CX16         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">13</span>,
    CPUID_FEAT_ECX_XTPR         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">14</span>,
    CPUID_FEAT_ECX_PDCM         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">15</span>,
    CPUID_FEAT_ECX_PCID         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">17</span>,
    CPUID_FEAT_ECX_DCA          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">18</span>,
    CPUID_FEAT_ECX_SSE4_1       = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">19</span>,
    CPUID_FEAT_ECX_SSE4_2       = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">20</span>,
    CPUID_FEAT_ECX_X2APIC       = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">21</span>,
    CPUID_FEAT_ECX_MOVBE        = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">22</span>,
    CPUID_FEAT_ECX_POPCNT       = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">23</span>,
    CPUID_FEAT_ECX_TSC          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">24</span>,
    CPUID_FEAT_ECX_AES          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">25</span>,
    CPUID_FEAT_ECX_XSAVE        = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">26</span>,
    CPUID_FEAT_ECX_OSXSAVE      = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">27</span>,
    CPUID_FEAT_ECX_AVX          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">28</span>,
    CPUID_FEAT_ECX_F16C         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">29</span>,
    CPUID_FEAT_ECX_RDRAND       = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">30</span>,
    CPUID_FEAT_ECX_HYPERVISOR   = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">31</span>,

    CPUID_FEAT_EDX_FPU          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">0</span>,
    CPUID_FEAT_EDX_VME          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">1</span>,
    CPUID_FEAT_EDX_DE           = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">2</span>,
    CPUID_FEAT_EDX_PSE          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">3</span>,
    CPUID_FEAT_EDX_TSC          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">4</span>,
    CPUID_FEAT_EDX_MSR          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">5</span>,
    CPUID_FEAT_EDX_PAE          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">6</span>,
    CPUID_FEAT_EDX_MCE          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">7</span>,
    CPUID_FEAT_EDX_CX8          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">8</span>,
    CPUID_FEAT_EDX_APIC         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">9</span>,
    CPUID_FEAT_EDX_SEP          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">11</span>,
    CPUID_FEAT_EDX_MTRR         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">12</span>,
    CPUID_FEAT_EDX_PGE          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">13</span>,
    CPUID_FEAT_EDX_MCA          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">14</span>,
    CPUID_FEAT_EDX_CMOV         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">15</span>,
    CPUID_FEAT_EDX_PAT          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">16</span>,
    CPUID_FEAT_EDX_PSE36        = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">17</span>,
    CPUID_FEAT_EDX_PSN          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">18</span>,
    CPUID_FEAT_EDX_CLFLUSH      = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">19</span>,
    CPUID_FEAT_EDX_DS           = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">21</span>,
    CPUID_FEAT_EDX_ACPI         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">22</span>,
    CPUID_FEAT_EDX_MMX          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">23</span>,
    CPUID_FEAT_EDX_FXSR         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">24</span>,
    CPUID_FEAT_EDX_SSE          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">25</span>,
    CPUID_FEAT_EDX_SSE2         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">26</span>,
    CPUID_FEAT_EDX_SS           = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">27</span>,
    CPUID_FEAT_EDX_HTT          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">28</span>,
    CPUID_FEAT_EDX_TM           = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">29</span>,
    CPUID_FEAT_EDX_IA64         = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">30</span>,
    CPUID_FEAT_EDX_PBE          = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">31</span>
};
</code></pre>
<p>A brief explanation of the CPU features above:</p>
<ul>
<li><p><code>PCLMUL, AES</code>: Cryptographic instruction sets for fast encryption and decryption.</p>
</li>
<li><p><code>VMX, SMX</code>: Virtualization support for running virtual machines.</p>
</li>
<li><p><code>SSE3, SSSE3, SSE4.1, SSE4.2, AVX</code>: SIMD instruction sets for faster multimedia, math, and vector processing.</p>
</li>
<li><p><code>FMA</code>: Fused Multiply-Add, improves performance in floating-point calculations.</p>
</li>
<li><p><code>RDRAND</code>: Random number generator.</p>
</li>
<li><p><code>X2APIC</code>: Advanced interrupt handling in multiprocessor systems.</p>
</li>
<li><p><code>PCID</code>: Optimizes memory management during context switches.</p>
</li>
<li><p><code>FPU</code>: Hardware floating-point unit for faster math operations.</p>
</li>
<li><p><code>PAE</code>: Physical Address Extension, allows addressing more than 4 GB of memory.</p>
</li>
<li><p><code>HTT</code>: Allows a single CPU core to handle multiple threads.</p>
</li>
<li><p><code>PAT, PGE</code>: Memory management features for controlling caching and page mapping.</p>
</li>
<li><p><code>MMX, SSE, SSE2</code>: Older SIMD instruction sets for multimedia processing.</p>
</li>
</ul>
<h3 id="heading-get-cpu-vendor-string">Get CPU Vendor String</h3>
<p>If you want to get the CPU vendor string, EAX should be set to 0×0 before invoking the CPUID instruction.</p>
<pre><code class="lang-plaintext">mov eax, 0x0
cpuid
</code></pre>
<p>The vendor string is a unique identifier that CPU vendors like AMD and Intel use. Examples are: GenuineIntel (for Intel processors) or AuthenticAMD (for AMD processors). It basically specifies the manufacturer of the CPU.</p>
<p>The vendor string allows the kernel to identify the CPU manufacturer which is very useful because different manufacturers implement certain features differently. Also, software or drivers can interact differently based on the CPU manufacturer to ensure compatibility.</p>
<p>When used like this, the vendor id string will be returned in EBX, EDX, ECX registers. You can write them to a buffer and get the full 12 character string.</p>
<p>Example code:</p>
<h3 id="heading-step-1-the-buffer">Step 1: The Buffer</h3>
<p>Create a buffer that can hold 12 bytes:</p>
<pre><code class="lang-plaintext">buffer: db 12 dup(0), 0xA, 0xD, 0
</code></pre>
<h3 id="heading-step-2-print-the-buffer">Step 2: Print the Buffer</h3>
<p>We start by creating a string printing function.</p>
<p>This assembly code reads a string character by character and prints it to the screen using BIOS interrupt 0x10. The <code>print</code> function loops through the string and uses the <code>lodsb</code> instruction to load each character in the <code>al</code> register.</p>
<p>Then the <code>print_char</code> function uses the interrupt 0×10 to print it on the screen. When the code reaches the end of the string (null terminator), the loop ends.</p>
<pre><code class="lang-plaintext">print_string:
    call print
    ret
print:
.loop:  
    lodsb   ;read character to al and then increment
    cmp al ,0 ;check if we reached the end
    je .done  ;we reached null terminator, finish
    call print_char ;print character
    jmp .loop   ;jump back into the loop
.done:
    ret
print_char:
    mov ah, 0eh
    int 0x10
    ret
</code></pre>
<h3 id="heading-step-3-fill-the-buffer-and-print-it">Step 3: Fill the Buffer and Print it</h3>
<p>Here, after saving the current state using the <code>pusha</code> instruction and calling <code>cpuid</code> with 0×0 passed in the EAX register, we can store the contents of <code>ebx</code>, <code>edx</code>, <code>ecx</code> to the buffer. Then we call <code>print_string</code> to print it.</p>
<pre><code class="lang-plaintext">get_cpu_vendor:
    pusha
    mov eax, 0x0
    cpuid
    mov [buffer], ebx
    mov [buffer + 4], edx
    mov [buffer + 8], ecx
    mov si, buffer 
    call print_string
    popa
    ret
</code></pre>
<p>A video from my YouTube channel where I implement and explain the code above in detail</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/K0Rxq2AIMmo" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<p>More information about what information CPUID instruction can give you according to the value passed in the EAX register, can be found here: <a target="_blank" href="https://gitlab.com/x86-cpuid.org/x86-cpuid-db">https://gitlab.com/x86-cpuid.org/x86-cpuid-db</a></p>
<h3 id="heading-epilogue">Epilogue</h3>
<p>By understanding and using the CPUID instruction, you can make your bootloader/kernel more adaptable to a wide range of processors. Knowing how to detect the instruction's availability and retrieve crucial system information—such as CPU features, cache sizes, and supported technologies—can significantly enhance performance and compatibility.</p>
<p>After reading this article, you should have the tools and knowledge to start exploring the CPUID instruction and how you can use it in your own project!</p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How Does a CPU Work Internally? From Transistors to Instruction Set Architecture ]]>
                </title>
                <description>
                    <![CDATA[ The CPU (Central Process Unit) is the brain of a computer, and the main connection between software and hardware. It makes it possible to operate software on hardware. However, how does it work in deep detail? And how can it connect programs to certa... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-does-cpu-work-internally/</link>
                <guid isPermaLink="false">66ba5326f4ac8da2b2c2e847</guid>
                
                    <category>
                        <![CDATA[ Computers ]]>
                    </category>
                
                    <category>
                        <![CDATA[ cpu ]]>
                    </category>
                
                    <category>
                        <![CDATA[ hardware ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Tiago Capelo Monteiro ]]>
                </dc:creator>
                <pubDate>Wed, 10 Jul 2024 19:14:05 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/07/1.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>The CPU (Central Process Unit) is the brain of a computer, and the main connection between software and hardware. It makes it possible to operate software on hardware.</p>
<p>However, how does it work in deep detail? And how can it connect programs to certain computer hardware?</p>
<p>This article aims to make you understand this connection by deeply explaining how a CPU works. This topic is often familiar only to those with a background in computer hardware design from college.</p>
<p>Often, many computer science graduates never have a class in advanced digital logic. So even very experienced programmers may lack an understanding of how a CPU actually processes information.</p>
<p>Although we won't be designing <a target="_blank" href="https://www.homemade-circuits.com/how-to-make-logic-gates-using-transistors/">logic gates from transistors</a> or <a target="_blank" href="https://www.techspot.com/article/1830-how-cpus-are-designed-and-built-part-2/">CPU components from logic gates</a>, we will cover the key concepts needed to understand how a CPU processes data created by a program written in a programming language.</p>
<p>We will see:</p>
<ul>
<li><a class="post-section-overview" href="#heading-analogy-introduction-to-what-makes-cpus-work">Analogy: Introduction to What Makes CPUs Work</a></li>
<li><a class="post-section-overview" href="#heading-the-memory-hubs-understanding-ram-and-rom">The Memory Hubs: Understanding RAM and ROM</a></li>
<li><a class="post-section-overview" href="#heading-the-roadways-of-data-navigating-the-cpu-data-path">The Roadways of Data: Navigating the CPU Data Path</a></li>
<li><a class="post-section-overview" href="#heading-traffic-controllers-the-role-of-state-machines-in-cpus">Traffic Controllers: The Role of State Machines in CPUs</a></li>
<li><a class="post-section-overview" href="#heading-daily-routines-the-fetch-execute-cycle-explained">Daily Routines: The Fetch-Execute Cycle Explained</a></li>
<li><a class="post-section-overview" href="#heading-the-rulebook-decoding-the-instruction-set-architecture-isa">The Rulebook: Decoding the Instruction Set Architecture (ISA)</a></li>
<li><a class="post-section-overview" href="#heading-from-programming-languages-to-machine-code">From programming languages to machine code</a></li>
<li><a class="post-section-overview" href="#heading-city-challenges-addressing-cpu-problems">City Challenges: Addressing CPU Problems</a></li>
<li><a class="post-section-overview" href="#heading-conclusion-better-control-units-and-data-parts">Conclusion: Better control units and data parts</a></li>
</ul>
<p>I will use the Intel 8008 as a reference. </p>
<h2 id="analogy">Analogy: Introduction to What Makes CPUs Work</h2>

<p>To understand deeply how a computer works, let's imagine a city as our real-life scenario. We'll compare computer elements to parts of this city.</p>
<p>This way, you get a clearer view of different CPU parts and why they are important. Afterwards, we will look in depth to each of the components</p>
<h3 id="heading-the-memory-hubs-understanding-ram-and-rom">The Memory Hubs: Understanding RAM and ROM</h3>
<p>RAM (Random access memory) is like a city public library: it stores books and information for people to borrow and return as needed.</p>
<p>In a computer, the RAM loads data and instructions from the computer memory needed by the CPU to process data.</p>
<p>ROM (read Only Memory) is like a historical archive in the city: It only stores records that will never change and never be borrowed from the public.</p>
<h3 id="heading-the-roadways-of-data-navigating-the-cpu-data-path">The Roadways of Data: Navigating the CPU Data Path</h3>
<p>The CPU data path is the network of roads in the city. The buses and registers of the CPU data path act like the city's road network.</p>
<p>Just as roads help cars and people move, the CPU data path guarantee the data travels in a efficient manner in the CPU</p>
<h3 id="heading-traffic-controllers-the-role-of-state-machines-in-cpus">Traffic Controllers: The Role of State Machines in CPUs</h3>
<p>States machines act as the traffic control systems.</p>
<p>The traffic control system manages the flow of vehicles, and the states machines manage the flow of data according to the instructions provided to the CPU.</p>
<h3 id="heading-daily-routines-the-fetch-execute-cycle-explained">Daily Routines: The Fetch-Execute Cycle Explained</h3>
<p>The fetch-execute cycle is the daily commute for city residents.</p>
<p>Every day, people decide where they are going, travel there, perform their tasks and return home. This process is always repeated.</p>
<p>In the same way, the CPU fetches instructions, decodes them, and executes them in a repetitive cycle.</p>
<h3 id="heading-the-rulebook-decoding-the-instruction-set-architecture-isa">The Rulebook: Decoding the Instruction Set Architecture (ISA)</h3>
<p>The instruction set architecture is like the city transportation law.</p>
<p>The city transportation law shows what is legal to do in the city in relation to the transportation of people.</p>
<p>The instruction set architecture is the set of rules and instructions that the CPU can execute.</p>
<h2 id="memory">The Memory Hubs: Understanding RAM and ROM</h2>

<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/3.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Photo by Valentine Tanasovich: https://www.pexels.com/photo/black-and-gray-computer-motherboard-2588757/</em></p>
<p><a target="_blank" href="https://www.freecodecamp.org/news/how-to-access-and-read-ram-contents/">RAM stands for Random Access Memory and can be used to read and write data.</a></p>
<p>The CPU gets data from the computer memory to the RAM first to avoid long waiting times.</p>
<p>Then, it uses the data from the RAM to complete the instructions.</p>
<p>They are used in computers and in many electronic devices due to the memory being volatile. It means that the data is only there while the computer is turned on, making it ideal for temporal storage while the device works.</p>
<p>ROM stands for Read Only Memory. In there, there only exist data added during computer manufacturing.</p>
<p>They are widely used in <a target="_blank" href="https://www.freecodecamp.org/news/what-is-firmware/">firmware</a> for devices, BIOS and small embedded systems.</p>
<p>This is because ROM is non-volatile memory. This means that it remains in memory when the device is powered off, making it very important for permanent data storage.</p>
<h2 id="roadways">The Roadways of Data: Navigating the CPU Data Path</h2>

<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/4.png" alt="Image" width="600" height="400" loading="lazy">
<em>Photo by Rogeer Marques: https://www.pexels.com/photo/close-up-shot-of-a-chip-processor-11272008/</em></p>
<p>The CPU data path is a complex digital circuit with many components that work with one another, such as:</p>
<ul>
<li><strong>Arithmetic Logic Unit (ALU):</strong> Performs arithmetic and logical operations inside the CPU data part.</li>
<li><strong>Registers:</strong> Small, fast storage locations for temporary data retrieved from the RAM.</li>
<li><strong>Buses:</strong> Data, control and address buses are wires used inside the CPU data path to transfer information.</li>
</ul>
<p>While CPUs have changed a lot since the Intel 8008, these are some of the components that still serve as the foundation for all CPUs.</p>
<p>Thanks to them, it is possible to let data flow, but not control the actual flow. This is the job of the control unit in the CPU, created in the Intel 8008 as state machines.</p>
<h2 id="traffic">Traffic Controllers: The Role of State Machines in CPUs</h2>

<p><a target="_blank" href="https://www.freecodecamp.org/news/state-machines-basics-of-computer-science-d42855debc66/">A state machine is a system that transitions between different states in order to perform tasks.</a></p>
<p>They are composed of a number of states and transitions. They were used in the Intel 8008 to create the control unit due to its structure and effective way to manage the sequence of operations needed to process instructions</p>
<p>Each of the states can activate one or many CPU components to process a certain assembly instruction.</p>
<p>This way, certain CPU data path parts are activated for an instruction to be completed.</p>
<p>Additionally, thanks to these state machines, the CPU is complete and can perform all instructions a user wants in a continuous loop called the fetch-execute cycle.</p>
<h2 id="fetch">Daily Routines: The Fetch-Execute Cycle Explained</h2>

<p>The state machine in the CPU controls how the CPU data path works together to perform a given instruction.</p>
<p>Nowadays, every computer receives millions of instructions per second. This way, the state machines act as a loop to get the instructions and execute them.</p>
<p>This process is known as the fetch-execute cycle, where the CPU retrieves and executes instructions:</p>
<ul>
<li><strong>Fetch:</strong> The CPU fetches the instruction from memory.</li>
<li><strong>Decode:</strong> The fetched instruction is decoded to determine the required action.</li>
<li><strong>Execute:</strong> The decoded instruction is executed using the appropriate CPU components.</li>
<li><strong>Write-back:</strong> The result of the execution is written back to memory or a register.</li>
</ul>
<p>In the fetch stage, the control unit tells the RAM to give the next instruction to the CPU.</p>
<p>In the decode stage, the CPU interprets the instruction, and in the execution stage, it performs the operation. Afterwards, the write-back stage ensures the result is stored correctly.</p>
<p>This cycle continues while the PC is on. This way, in modern processors, processing billions of instructions per second.</p>
<h3 id="heading-but-what-about-data-from-the-keyboard-or-mouse">But What About Data from the Keyboard or Mouse?</h3>
<p>This data does not come from RAM but is handled through a mechanism called interrupts. While the CPU runs instructions, it can detect when data comes from peripherals.</p>
<p>If this happens, the CPU stops its current task and prioritizes the instructions from the peripherals. Afterwards, the CPU resumes its previous tasks.</p>
<p>There are many ways to manage interrupts, with some of the most popular being:</p>
<ol>
<li><strong>Polled Interrupts</strong>: The CPU periodically checks if an interrupt has occurred.</li>
<li><strong>Vectored Interrupts</strong>: The interrupting device directs the CPU to the appropriate interrupt service routine.</li>
<li><strong>Prioritized Interrupts</strong>: Interrupts are assigned different priority levels, ensuring critical tasks are handled first.</li>
</ol>
<p>This way, with these mechanisms, the CPU maintains its performance while interacting the peripherals.</p>
<h2 id="instruction">The Rulebook: Decoding the Instruction Set Architecture (ISA)</h2>

<p>With the control unit, the complete CPU and RAM, it is possible to perform many instructions.</p>
<p>But what instructions can be performed on a given CPU? And how many? This is what the Instruction Set Architecture (ISA) solves.</p>
<p>The ISA defines a set of instructions that a certain CPU can execute. It is what allows programmers to understand what a processor can and cannot do without having to understand all the digital logic hardware inside it.</p>
<p>This way, it acts as an interface between software and hardware.</p>
<p><strong>Key Aspects of ISA:</strong></p>
<ul>
<li><strong>Instruction Types:</strong> Includes arithmetic, logical, control, and data transfer instructions.</li>
<li><strong>Addressing Modes:</strong> Methods for specifying operands of instructions.</li>
<li><strong>Registers:</strong> The set of registers available for use by instructions.</li>
</ul>
<p><strong>Common ISAs:</strong></p>
<ul>
<li><strong>x86:</strong> Widely used in desktop and server processors.</li>
<li><strong>ARM:</strong> Dominant in mobile and embedded devices due to its power efficiency.</li>
<li><strong>RISC-V:</strong> An open standard ISA designed for a wide range of applications.</li>
</ul>
<p>Each CPU often has its own version of the instruction-set architecture. And the instruction set architecture is very often defined with the assembly programming languages.</p>
<p>This is why there are so <a target="_blank" href="https://www.freecodecamp.org/news/what-are-assembly-languages/">many versions</a> of the assembly programming language.</p>
<p>Since each CPU has its own hardware specifications, each will have similar components to other CPUs and, thus, similar assembly programming languages associated.</p>
<p>The choice of ISA impacts the CPU's design, performance, and compatibility with software.</p>
<p>For instance, the complexity of x86 allows for powerful desktop applications, while ARM's simplicity favors energy-efficient mobile devices.</p>
<h2 id="programming">From Programming Languages to Machine Code</h2>

<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/3-1.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Photo by luis gomes: https://www.pexels.com/photo/close-up-photo-of-programming-of-codes-546819/</em></p>
<p>While each processor has its own assembly language, managing code in assembly and writing code in assembly to make big programs can be complex.</p>
<p>It is very complicated, and may lead to wasting time on correcting things and details instead of, in a faster and easier way, managing the development of a program and actually developing it.</p>
<p>To solve this problem, many programming languages were created from assembly. We write the code in the programming languages, and it is then converted to assembly.</p>
<p>This way, instead of spending time on details, it is possible to focus on more important things like system development and algorithm design.</p>
<p>This is the process by which most programming languages convert their code into assembly:</p>
<ol>
<li>Convert the code to assembly code through either a compiler or interpreter.</li>
<li>The assembly code is then converted to raw machine code and executed by the CPU.</li>
<li>A specific loop in the CPU's state machine is completed.</li>
<li>Afterward, the CPU fetches and executes the next instruction.</li>
</ol>
<p>Let's see two examples of programming languages doing this!</p>
<h3 id="heading-c-programming-language">C Programming Language</h3>
<p>The C programming language was created from assembly in the early 1970s. It was created to provide a higher-level language for efficient system-level programming that also allows hardware manipulation.</p>
<p>With a compiler, the C code is converted to assembly and then processed by the complete CPU.</p>
<p>Thanks to this conversion, by writing programs in the C programming language, we can address many problems in a more efficient manner, such as:</p>
<ul>
<li>Memory management errors</li>
<li>Buffer overflows</li>
<li>Manual optimization issues</li>
</ul>
<p>Nowadays, even for simpler tasks, the assembly code converted from C compiler is far more efficient and reliable than a human writing the assembly code.</p>
<p>If you want to learn more about the C compiler you can check out:</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.freecodecamp.org/news/what-is-a-compiler-in-c/">https://www.freecodecamp.org/news/what-is-a-compiler-in-c/</a></div>
<h3 id="heading-python-programming-language">Python Programming Language</h3>
<p>The Python programming language was created from C in the late 1980s.</p>
<p>Its goal was to provide a user-friendly, high-level programming language that emphasizes readability and simplicity, allowing for rapid application development.</p>
<p>In Python, an interpreter converts the Python code to bytecode line by line.</p>
<p>And this bytecode is converted to machine code in the CPU and processed in the fetch-execute cycle.</p>
<p>This way, it is possible for people to program in an easier way and focus on bigger programs, such as:</p>
<ul>
<li>Artificial intelligence models</li>
<li>Web apps</li>
<li>Data analysis</li>
<li>Scientific computing</li>
</ul>
<p>However, the challenge with the CPUs in all programming languages is that it processes data sequentially.</p>
<h2 id="problems">City Challenges: Addressing CPU Problems</h2>

<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/4-1.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Photo by Peng LIU: https://www.pexels.com/photo/timelapse-photography-of-vehicle-on-concrete-road-near-in-high-rise-building-during-nighttime-169677/</em></p>
<p>The traditional one core CPU processes data sequentially, instruction after instruction. This becomes a limitation if we have many instructions to process.</p>
<p>This is what GPUs (Graphics processing units) came to fix.  Thanks to GPUs, we can process instructions in parallel, thereby reducing computing time significantly.</p>
<p>With these parallel processing capabilities, it is possible to achieve a much faster computation and improved efficiency in a wide range of applications.</p>
<h2 id="conclusion">Conclusion: Better Control Units and Data Parts</h2>

<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/5.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Photo by Miguel Á. Padriñán: https://www.pexels.com/photo/green-circuit-board-343457/</em></p>
<p>In addition to modern CPUs being multicore, advancements in control units and data paths play a critical role in improving processor performance. </p>
<p>Control units are often designed using microprogramming or hardwired control units. </p>
<p>Microprogramming offers greater flexibility and easier updates to the control logic, while hardwired control units provide faster performance by directly implementing control signals.</p>
<p>Another significant advancement is the exploration of new materials for transistors in logic gates. </p>
<p>Instead of relying solely on silicon, researchers are investigating alternative materials to create faster and more efficient processors.</p>
<p>As technology continues to advance, understanding these fundamental concepts will remain essential for both enthusiasts and professionals in the field.</p>
<p>Keeping up with these developments ensures the continued innovation and improvement of CPU design and functionality.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What is Antimalware Service Executable? Why is it High CPU Disk Usage? ]]>
                </title>
                <description>
                    <![CDATA[ Antimalware service executable is a component of Windows Security that runs in the background. But sometimes antimalware service executable can impact Windows 10 computers negatively by using too much CPU. In this guide, I will show you what antimalw... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-antimalware-service-executable-why-is-it-high-cpu-disk-usage/</link>
                <guid isPermaLink="false">66adf2687550d4f37c2019d9</guid>
                
                    <category>
                        <![CDATA[ cpu ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Security ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Windows 10 ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Thu, 10 Feb 2022 17:31:27 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/02/cpu-4393376_1920.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Antimalware service executable is a component of Windows Security that runs in the background.</p>
<p>But sometimes antimalware service executable can impact Windows 10 computers negatively by using too much CPU.</p>
<p>In this guide, I will show you what antimalware service executable is, why it uses so much CPU, and how you can optimize your Windows 10 computer to make sure it doesn't use too much CPU.</p>
<h2 id="heading-what-is-antimalware-service-executable">What is Antimalware Service Executable?</h2>
<p>Antimalware service executable is a Windows Security process that executes real-time protection against malware.</p>
<p>Also known as msmpeng.exe, antimalware service executable runs in the background so it can scan files and programs from time to time.</p>
<p>When an antimalware service executable detects a virus or other malicious attacks, it deletes them or quarantines them.</p>
<h2 id="heading-why-does-antimalware-service-executable-use-a-lot-of-cpu">Why does Antimalware Service Executable use a lot of CPU?</h2>
<p>The main reason that antimalware service executable uses too much CPU is that it runs constantly in the background.</p>
<p>While running in the background, it actively scans programs and files and carries out the appropriate actions whenever it detects anything malicious.</p>
<p>In addition, the antimalware service executable uses too much CPU because it scans its own folder - <code>C:\Program Files\Windows Defender</code>.</p>
<p>So, stopping antimalware service executable from scanning its own folder is one of the ways you can make it use less CPU.</p>
<h2 id="heading-how-to-stop-antimalware-service-executable-from-using-too-much-cpu">How to Stop Antimalware Service Executable from using too Much CPU</h2>
<p>The 2 main ways you can stop antimalware service executable from using too much CPU is to reschedule Windows Security scans and prevent it from scanning its own folder.</p>
<p>Rescheduling scans won't make scans happen all the time, and preventing the executable from scanning its own folder will disable real-time protection.</p>
<h2 id="heading-solution-1-prevent-antimalware-service-executable-from-scanning-its-own-folder">Solution 1: Prevent Antimalware Service Executable from Scanning its Own Folder</h2>
<p><strong>Step 1</strong>: Press the WIN key on your keyboard and select the gear icon to open the Settings app.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/opensettings.jpg" alt="opensettings" width="600" height="400" loading="lazy"></p>
<p><strong>Step 2</strong>: Click on “Update and Security” from the menu tiles.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-2-3.png" alt="ss-2-3" width="600" height="400" loading="lazy"></p>
<p><strong>Step 3</strong>: Select “Windows Security”, then click on “Virus and threat protection”.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-3-2.png" alt="ss-3-2" width="600" height="400" loading="lazy"></p>
<p><strong>Step 4</strong>: The Windows Security app will open up. Under “Virus &amp; threat protection settings”, click on the link that says “Manage Settings”. 
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-4-2.png" alt="ss-4-2" width="600" height="400" loading="lazy"></p>
<p><strong>Step 5</strong>: Scroll down to “Exclusions” and select the “Add or remove exclusions” link.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-5-2.png" alt="ss-5-2" width="600" height="400" loading="lazy"></p>
<p><strong>Step 6</strong>: On the next page, click on “Add an exclusion”, then select “Folder”.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-6.png" alt="ss-6" width="600" height="400" loading="lazy"></p>
<p><strong>Step 7</strong>: Paste “<code>C:\Program Files\Windows Defender</code>” into the editor and click on “Select Folder”.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-7.png" alt="ss-7" width="600" height="400" loading="lazy"></p>
<p><strong>Step 8</strong>: Immediately after you click on “Select Folder”, a massive modal will appear – make sure you click “Yes”. </p>
<p>The folder selected will now be added to exclusions and will not be scanned.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-8.png" alt="ss-8" width="600" height="400" loading="lazy"></p>
<h2 id="heading-solution-2-disable-realtime-protection-and-reschedule-scans">Solution 2: Disable Realtime Protection and Reschedule Scans</h2>
<p><strong>Step 1</strong>: Press <code>WIN + R</code> (Windows key then the letter R) to open the Run Dialogue.</p>
<p><strong>Step 2</strong>: Type “taskschd.msc” and click “OK”. This will open up the Task Scheduler app.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-9.png" alt="ss-9" width="600" height="400" loading="lazy"></p>
<p><strong>Step 3</strong>: Expand the “Task Scheduler tab”, “Microsoft”, and “Windows”.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-10.png" alt="ss-10" width="600" height="400" loading="lazy"></p>
<p><strong>Step 4</strong>: Scroll down and select “Windows Defender”.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-11.png" alt="ss-11" width="600" height="400" loading="lazy"></p>
<p><strong>Step 5</strong>: Right-click on “Windows Defender Scheduled Scan” and select “Properties”.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-12.png" alt="ss-12" width="600" height="400" loading="lazy"></p>
<p><strong>Step 6</strong>: Uncheck “Run with highest privileges” in the general tab.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-13.png" alt="ss-13" width="600" height="400" loading="lazy"></p>
<p><strong>Step 7</strong>: Go to the Conditions tab and uncheck everything there.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-14.png" alt="ss-14" width="600" height="400" loading="lazy"></p>
<p><strong>Step 8</strong>: Switch to the Triggers tab and click “New”.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-15.png" alt="ss-15" width="600" height="400" loading="lazy"></p>
<p><strong>Step 9</strong>: Schedule the time you want Windows Defender to run scans. Choose the frequency, date, and time, then click “OK”. Click “OK” again.
<img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-16.png" alt="ss-16" width="600" height="400" loading="lazy"></p>
<p><strong>Step 10</strong>: Restart your computer. With this, the antimalware service executable should not eat up too many CPUs again.</p>
<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>The protection offered by the antimalware service executable is undeniably relevant. This protection prevents malware attacks so you can feel safe while using your Windows 10 computer.</p>
<p>If you try to make antimalware service executable consume less CPU with the 2 methods explained in this article and there seems to be no progress, you should try to disable your Windows Security program permanently. </p>
<p>But make sure you get another antivirus program so your computer won't be at the mercy of attacks.</p>
<p>Thank you for reading.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What is CPU? Meaning, Definition, and What CPU Stands For ]]>
                </title>
                <description>
                    <![CDATA[ Every single computing device has a CPU. You may have heard of this tech term before, but what is it exactly? What is a CPU and how does it work? In this beginner-friendly article you'll learn the basics on what a CPU actually is, and I'l give you an... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-cpu-meaning-definition-and-what-cpu-stands-for/</link>
                <guid isPermaLink="false">66b1e4e6ab763c1471e56794</guid>
                
                    <category>
                        <![CDATA[ Computer Science ]]>
                    </category>
                
                    <category>
                        <![CDATA[ cpu ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Tue, 16 Nov 2021 22:53:22 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/11/niek-doup-Xf071ws2Icg-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Every single computing device has a CPU.</p>
<p>You may have heard of this tech term before, but what is it exactly? What is a CPU and how does it work?</p>
<p>In this beginner-friendly article you'll learn the basics on what a CPU actually is, and I'l give you an overview of how it works.</p>
<h2 id="heading-what-is-a-cpu-and-where-do-you-find-it-in-a-computer">What is a CPU and where do you find it in a computer?</h2>
<p>CPU is short for Central Processing Unit. It is also known as a processor or microporcessor.</p>
<p>It's one of the most important pieces of hardware in any digital computing system – if not the most important.</p>
<p>Inside a CPU there are thousands of microscopic <em>transistors</em>, which are tiny switches that control the flow of electricity through the integrated circuits.</p>
<p>You'll find the CPU located on a computer's <em>motherboard</em>.</p>
<p>A computer's motherboard is the main circuit board inside a computer. Its job is to connect all hardware components together.</p>
<p>Often referred to as the brain and heart of all digital systems, a CPU is responsible for doing all the work. It performs every single action a computer does and executes programs.</p>
<h3 id="heading-what-are-computer-programs-and-where-are-they-stored">What are computer programs and where are they stored?</h3>
<p>There is a program for everything a CPU does.</p>
<p>You have a program that lets you use your web browser or a word processor. You have one that performs mathematical operations on a calculator or lets you type letters and characters on a keyboard. And there are programs that manage clicking and selecting elements with a computer mouse and pressing down on your laptop's touchpad. </p>
<p>Whatever it may be, there is a program for all computer activities.</p>
<p>Programs are sets of instructions that need to be executed in sequential, logical order and be followed precisely step-by-step.</p>
<p>They are written in a human-readable language – a programming language – by a programmer.</p>
<p>Computers don't understand programming languages directly, so they need to be translated to a form that is easier understood.</p>
<p>That form is called machine language or binary.</p>
<p>Binary is a <em>base two</em> numerical system. It's comprised of only two numbers: 0 and 1.</p>
<p>This reflects and ties in well with the only two possible states transistors  have to control the ebb and flow of electricity – they are either on (1) or off (0).</p>
<p>So, under the hood, programs are stored as sequences of bits. Bits are another name for binary digits (sequences of 1s and 0s).</p>
<p>Programs are stored permanently and long term in a storage device, whether it's a HDD (Hard Disk Drive) or SSD (Solid State Drive).</p>
<p>These are non-volatile types of memory, meaning they store data even when the power is off.</p>
<p>While a program is up and running and currently being used, though, all of its data is stored in the main, primary, memory or RAM (Random Access Memory).</p>
<p>This type of memory is volatile, and all data is lost when the power shuts off.</p>
<h2 id="heading-what-does-a-cpu-do">What does a CPU do?</h2>
<p>In a nutshell, a CPU is responsible for handling the processing of logical and mathematical operations and executing instructions that it is given.</p>
<p>It can execute millions of instructions per second – but can carry out only one instruction at a time.</p>
<p>It first receives some type of input, typically from an input device (such as a monitor display screen, a keyboard, a mouse, or a microphone) or from an application/system software program (like your web browser or operating system).</p>
<p>Then the CPU is in charge of four tasks:</p>
<p>1) <strong>Fetching</strong> instructions from memory, in order to know how to handle the input and know the corresponding instructions for that particular input data it received. Specifically, it looks for the address of the corresponding instruction and forwards the request to the RAM. The CPU and RAM constantly work together. This is also called <em>reading from</em> memory.
2) <strong>Decoding</strong> or translating the instructions into a form the CPU can understand, which is machine language (binary).
3) <strong>Executing</strong> and carrying out the given instructions.
4) <strong>Storing</strong> the result of the execution back to memory for later retrieval if and when requested. This is also called <em>writing to</em> memory.</p>
<p>Finally, there is an output of some kind, such as printing something to the screen.</p>
<p>The process described above is called the <strong>fetch-execute</strong> cycle, and happens millions of times per second.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/11/Screenshot-2021-10-25-at-5.30.18-PM.png" alt="Screenshot-2021-10-25-at-5.30.18-PM" width="600" height="400" loading="lazy"></p>
<h2 id="heading-the-main-parts-of-a-cpu">The main parts of a CPU</h2>
<p>Now you know the basic tasks a CPU performs for every operation happening on a computer, what are the parts of the CPU that help get that work done?</p>
<p>Below are some of the important components within it:</p>
<ul>
<li><strong>CU</strong> (short for Control Unit). It regulates the flow of input and output. It's the part that fetches and retrieves the instructions from main memory and later decodes them.</li>
<li><strong>ALU</strong> (short for Artithmetic Logic Unit). The part where all the processing happens. Here is where all mathematic calculations take place, such as addition, subtraction, multiplication, and division, as well as all the logical operations for decision making, such as comparing data.</li>
<li><strong>Registers</strong>. An extremely fast memory location. The data and instructions that are currenlty being processed during the fetch-execute cycle are stored there, for quick access by the processor.</li>
</ul>
<h2 id="heading-what-are-cpu-cores">What are CPU cores?</h2>
<p>Earlier you learned that a CPU can typically perform just one action at a time.</p>
<p>It executes one instruction at a time and it does this with with the help of physical cores. </p>
<p>Essentially, a core is a CPU itself, a separate device inside the main CPU chip. This means that it has the ability to do just one thing at a time.</p>
<p>However, modern computers have the ability to support more than one core inside the main chip. </p>
<p>The more cores a CPU has, the greater the computational power and the more tasks that can be running and completed simultaneously, making the CPU a serial multitasker.</p>
<p>For example, there are dual-core CPUs, meaning there are two CPUs on the same chip and can run two instructions at the same time.</p>
<p>Quad-core CPUs mean there are four CPUs on the same chip, hexa-core CPUs mean there are six cores, and so on.</p>
<h3 id="heading-what-is-hyperthreading">What is hyperthreading?</h3>
<p>Modern CPUs also support a technology called hyperthreading.</p>
<p>The way this works is that a single physical core appears as multiple physical cores, making the Operating System think there are more cores than there actually are. This in turn makes the computer think it has more power than it actually has.</p>
<p>So, in addition to the physical cores mentioned in the section above, there are also these virtual cores, or threads as they are also called.</p>
<p>They aren't actual physical cores, but they appear to be so.</p>
<p>The combination of both physical and virtual cores make the execution time of programs even faster and give CPU even more computational power.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Thanks for reading and making it to the end! Hopefully you now have a better understanding of what CPUs are, what they do, and why they're so important. </p>
<p>If you want to know more about computer basics, have a look <a target="_blank" href="https://www.freecodecamp.org/news/what-is-a-pc-computer-definition-and-computer-basics-for-beginners/">at this guide which goes over the basic parts of a computer</a>.</p>
<p>Happy learning!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ CPU Definition ]]>
                </title>
                <description>
                    <![CDATA[ A CPU, or central processing unit, is the brain of the computer. Like a human brain, the CPU controls all the functions of the computer. The CPU's job is to listen for input data from devices like a mouse or other software. Then it looks up instructi... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/cpu-definition/</link>
                <guid isPermaLink="false">66c347e912c88d894ffd1f60</guid>
                
                    <category>
                        <![CDATA[ Computers ]]>
                    </category>
                
                    <category>
                        <![CDATA[ cpu ]]>
                    </category>
                
                    <category>
                        <![CDATA[ hardware ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tech Terms ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 06 Apr 2021 06:35:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/60753bc2776bd507fe31ed0c.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>A CPU, or central processing unit, is the brain of the computer.</p>
<p>Like a human brain, the CPU controls all the functions of the computer.</p>
<p>The CPU's job is to listen for input data from devices like a mouse or other software. Then it looks up instructions for that data, and executes those instructions.</p>
<p>For example, if you type the character "a" on your computer, your CPU will look up how to handle your button press. Once it receives instructions back, it executes your button press, and sends out the output "a".</p>
<p>Modern CPUs can handle billions, or even trillions of these instructions per second.</p>
<p>CPUs are made up of tens of thousands of tiny rods called transistors. Each transistor has either an on (yes) or off (no) state, and form the basis of binary that all computers work with.</p>
<p>With enough transistors asking these simple yes/no questions, the CPU can perform complex tasks.</p>
<p>Modern CPUs are often made up of different cores that make it easier to multitask. Each core is just another CPU on the same chip. </p>
<p>For example, a dual-core CPU has two CPUs on the same chip. These days there are quad-core (4), hexa-core (6), or even hexadeca-core (16) CPUs.</p>
<p>Modern CPUs can also use a process called multi-threading or hyper-threading to be more efficient. This is the process of splitting a CPU up into virtual cores called threads.</p>
<p>For example, if you have a dual-core processor, you might see four cores if you check your computer's system monitor.</p>
<p>These physical cores and virtual cores / threads can make your computer much faster in some cases.</p>
<p>Games usually work better on a single, powerful core. But other programs like video editing software is much faster with more cores and threads.</p>
<p>Check out this video for a high-level overview of how the CPU handles all the inputs and outputs for your computer:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/image-48.png" alt="Image" width="600" height="400" loading="lazy"></p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/AkFi90lZmXA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-related-tech-terms">Related Tech Terms:</h2>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/hardware-definition/">Hardware Definition</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/operating-system-os-definition/">Operating System Definition</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/boolean-definition/">Boolean Definition</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Microprocessor's Romance With Negative Integers – The How and Why of CPU Arithmetic Design ]]>
                </title>
                <description>
                    <![CDATA[ By Vivek Agrawal One of the first things we learn about computers is that they only understand 0s and 1s, or bits.  We humans, on the other hand, communicate numbers via the decimal system. This system uses digits from 0 to 9 along with plus and minu... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/microprocessors-romance-with-integers/</link>
                <guid isPermaLink="false">66d461712472e5ed2fa07bc1</guid>
                
                    <category>
                        <![CDATA[ binary ]]>
                    </category>
                
                    <category>
                        <![CDATA[ cpu ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ systems ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 28 Jan 2021 18:30:45 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/01/cover2.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Vivek Agrawal</p>
<p>One of the first things we learn about computers is that they only understand <strong>0s and 1s</strong>, or <strong>bits</strong>. </p>
<p>We humans, on the other hand, communicate numbers via the decimal system. This system uses digits from 0 to 9 along with plus and minus signs (+ and -) to denote positive or negative numbers. </p>
<p>Since computers can use only two digits – 0 and 1 – engineers and mathematicians back in the day designed clever techniques for representing negative numbers and for doing arithmetic with them. Let's explore the beauty of those techniques.</p>
<h2 id="heading-first-some-background-on-how-computers-work">First, some background on how computers work</h2>
<p>Software, images, text, videos, numbers and everything in between are 0s and 1s at the lowest level in our computer.</p>
<p>For images, text, videos and numbers, we have encoding schemes that decide how these stuff will get to 0s and 1s. For example, ASCII and Unicode for text.</p>
<p>The software programs we code get to 0s and 1s via compilers and assemblers. Those set of 0s and 1s known as machine code (or machine instruction) are first stored in our computer's main memory (RAM) before the processor can execute them.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/fetch-decode-exec.png" alt="A diagram showing fetch decode execute cycle" width="600" height="400" loading="lazy">
_The fetch decode execute cycle architected by Sir <a target="_blank" href="https://en.wikipedia.org/wiki/John_von_Neumann">John von Neumann</a>. Every digital computer follows this cycle to run machine code._</p>
<p>The processor starts the execution cycle by <strong>fetching</strong> the instructions from the main memory, then the control unit of the processor <strong>decodes</strong> those instructions into two parts – operation code (opcode) and operands. </p>
<p>The opcode decides the further action that needs to be performed like ADD (addition), JMP (jump), INC (increment) and so on. The operands are the values (or memory locations) on which that operation will be performed. </p>
<p>The decoded instructions are sent to the Arithmetic and Logic Unit (ALU) for <strong>execution</strong>. In the ALU, the instruction is executed based on the opcode on the operands and the result is stored back in the memory. </p>
<p>For example, the assembly code <code>ADD eax, 42</code> is first turned into machine code (0s and 1s) by the assembler. Then it is stored into the main memory before the fetch-decode-execute cycle can begin. </p>
<p>When the fetching of the machine code for <code>ADD eax, 42</code> from the memory finishes, the instruction is decoded. The decoded output says that the opcode is <code>ADD</code> and the operands are <code>eax</code> and <code>42</code>.  </p>
<p><code>eax</code> is a register – a memory location inbuilt into the processor that can be accessed instantaneously by the processor. The <code>eax</code> register is called an accumulator in most processors. </p>
<p>The <code>ADD eax, 42</code> assembly code is designed to add 42 to the current value of the <code>eax</code> register (accumulator) and stores that sum in <code>eax</code>. It is <code>eax = eax + 42</code>. </p>
<p>Suppose that currently <code>eax</code> is 20. This means that the value of <code>eax</code> after executing <code>ADD eax, 42</code> will be 20 + 42 = 62.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/Edvac-1.jpg" alt="Two men operating EDVAC computer" width="600" height="400" loading="lazy">
<em>EDVAC was one of the earliest electronic binary computer built for the U.S. Army's Ballistics Research Laboratory. (<a target="_blank" href="https://en.wikipedia.org/wiki/EDVAC#/media/File:Edvac.jpg">Image source</a>, Public Domain).</em></p>
<p>The design of early computers such as EDVAC started with the desire to make tedious mathematical calculations easier and faster. </p>
<p>The whole responsibility of making computers compute lay on the shoulders of adders – circuits that add two numbers. This is because sophisticated operations like subtraction, multiplication, and division utilize adders in their circuits.  </p>
<p>Ultimately computers are just a fast arithmetic machines with logic capabilities. Understanding the challenges and the beauty of binary arithmetic design (of positive and especially negative integers) is <strong>one of the most fundamental concepts in a computer processor</strong>. </p>
<p>Let's first see how decimal numbers are represented in binary and how to add two binary values. Then we will start exploring the beauty.      </p>
<h2 id="heading-how-the-binary-system-works">How the binary system works</h2>
<p>If I tell you to read out <code>872500</code>, you will likely say <strong>872.5K</strong>. Let's take a look at how our minds do this.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/decimal_sys_img-2.png" alt="The procedure we humans use to read decimal numbers" width="600" height="400" loading="lazy"></p>
<p>We assign the one's place to the first digit from the right, then the ten's place to the second from the right, the hundredths to the third, and so on, growing each time by power of 10. </p>
<p>These powers of 10 in each place are the weights of the places. The weight of the hundredth place is one hundred. We multiply the digits in each place by their place's weight and sum them all up to get a complete number.</p>
<p>In the above diagram, you can see that the growth of each place's weight is in the powers of 10, starting from <code>10^0</code> and going through <code>10^5</code>. That's why decimals are called a base ten system.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/binary_sys_img-2.png" alt="The procedure via which computers read binary codes" width="600" height="400" loading="lazy"></p>
<p>In binary, each place's weight grows by a power of 2. This means that the place's weight starts from <code>2^0</code> and ends at <code>2^something</code>. That's the only difference.</p>
<p><code>00110101</code> in decimal translates to 53. Computers interpret binary in the same way as we humans interpret decimals, that is multiplying each place's digit by its weight and summing them up.</p>
<h3 id="heading-how-to-add-1s-and-0s">How to add 1s and 0s</h3>
<p>Addition works in binary pretty much the same way as it's done in decimals. Let's see that through an example. We'll add two binary numbers: <code>1101</code> (13) and <code>1100</code> (12).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/1-1.png" alt="Step one of addition of 1101 and 1100 that is one plus zero" width="600" height="400" loading="lazy"></p>
<p>As we do in the decimal system, we start from the one's place (<code>2^0</code>). Adding 1 and 0 gives us 1. So we put a 1 there. Stay with me and you'll get the whole picture.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/2-1.png" alt="Step two of addition of 1101 and 1100 that is zero plus zero" width="600" height="400" loading="lazy"></p>
<p>0 plus 0 is 0. Moving on.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/3-2.png" alt="Step three of addition of 1101 and 1100 that is one plus one" width="600" height="400" loading="lazy"></p>
<p>1 plus 1 is 2. And 2 in binary is represented as <code>10</code>. We carry 1 to the next place and keep 0 as a result of the current place we are in. Isn't this the same as exceeding 9 in a place in decimal addition? </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/4-1.png" alt="Step four of addition of 1101 and 1100 that is one plus one plus one of carry row" width="600" height="400" loading="lazy"></p>
<p>We have two 1s there and one 1 that was carried forward from the previous place, so there are a total of three 1s. Their sum will be 3, and in binary 3 is <code>11</code> so we write <code>11</code>. The final result comes out to be <code>11001</code> or 25 in decimal form, which is indeed 13 + 12.</p>
<p>The above computation assumes that we have five bits available to store the result. If a 4-bit computer does this addition, then it will only have four bits available to store the result. </p>
<p>That fifth bit will be called an <strong>overflow</strong> in 4-bit computers. In integer arithmetic, the overflow bit is ignored or discarded. So we would have got <code>1001</code> (9) as our result if we were using a 4-bit computer.</p>
<h2 id="heading-the-beauty-of-binary-arithmetic-design">The beauty of binary arithmetic design</h2>
<p>Two important terms we need to understand before we move forward are <strong>least significant bit</strong> and <strong>most significant bit</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/lsb_msb-1.png" alt="The least significant and the most significant bit in a byte word" width="600" height="400" loading="lazy"></p>
<p>The bit on the <strong>rightmost is the least significant bit</strong> because it has the smallest place weight (<code>2^0</code>). And the bit on the <strong>leftmost is the most significant bit</strong> as it has the highest place weight (<code>2^7</code>).</p>
<p>If the world only had positive numbers, then this would have been the end of this article (because we have already learned how to represent decimals in binary and how to add them in binary). </p>
<p>Thankfully, we have negative numbers, too. </p>
<p>The beauty of the CPU's arithmetic design rests in negativeness. </p>
<p>So how do computers represent negative numbers, and how does arithmetic on negative numbers work? Let's see an encoding approach to this problem.</p>
<p>Please note that in the below sections we will be working with a 4-bit computer to understand the concepts, meaning the fifth bit will be treated as an overflow. The same principles apply to all the CPU architectures like 16-bit, 32-bit or 64-bit to do arithmetic.</p>
<h3 id="heading-the-sign-magnitude-encoding-approach">The sign magnitude encoding approach</h3>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/sign-bit01-1.png" alt="The leftmost bit in a four bit binary is the sign bit and the remaining three represents magnitude" width="600" height="400" loading="lazy"></p>
<p><code>1101</code> in decimal form would be -5 in this encoding scheme. The leftmost or the most significant bit is the sign bit. It tells the processor about the sign of the number – that is, whether the number is positive or negative. </p>
<p><code>0</code> in the sign bit represents a positive value and <code>1</code> represents a negative value. The remaining bits tells us the actual magnitude.</p>
<p>In <code>1101</code>, the sign bit is <code>1</code>, so the number is negative. <code>101</code> equals 5 in decimal. So <code>1101</code> will compute to -5 in decimal.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/4bit-1.png" alt="All numbers that are possible with four bits using sign bit encoding scheme" width="600" height="400" loading="lazy">
<em>All possible numbers that can be represented by four bits with sign bit encoding scheme</em></p>
<p>In the above diagram you can see all the integers that can be represented by four bits using this encoding approach. All looks good up to this point. </p>
<p>But if we look closely, we can see a very serious design issue in this encoding scheme. Let's face that issue.</p>
<p>Let's add a positive and a negative number. For example we'll add +4 and -1. Our answer should be <code>(+4) + (-1) = (+3)</code> that is <code>0011</code>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/signbitproblem.png" alt="Adding +4 and -1 in binary resulting in -5 when using sign bit encoding scheme" width="600" height="400" loading="lazy"></p>
<p>See, the result is <code>1101</code> (-5). The actual answer should be <code>0011</code> (+3). If we were to implement this approach on a processor then we would need to add logic to deal with this issue, and engineers hate additional complexity in their logic. </p>
<p>As we add more circuits, the power consumption increases and performance suffers. </p>
<p>This might sound like a trivial issue for modern transistor-based computers. </p>
<p>But think of early computers like EDVAC which was run on thousands of vacuum tubes consuming power in kilowatts operated by hundreds of people a day. And the government spent millions to build them. </p>
<p>In those days putting additional circuits and vacuum tubes meant thousands of dollars and serious maintenance trouble.   </p>
<p>So engineers had to think of a smarter encoding design. </p>
<p>Now, the time has come to reveal the beauty that will tackle this problem and make our system simpler, more performant, and less power hungry.</p>
<h3 id="heading-a-beautiful-encoding-system-enters-and-the-cpu-shines">A beautiful encoding system enters and the CPU shines ❤️</h3>
<p>In this encoding scheme, like in the previous one, the leftmost bit acts as a sign bit – but with some art involved to represent negative numbers.</p>
<p>The positive numbers are represented in the exact same way as the previous encoding scheme: a leading <code>0</code> followed by remaining bits for the magnitude. For example, in this encoding scheme too, 6 will be represented as <code>0110</code>. </p>
<p>To represent a negative number, a two step math process is run in its positive counterpart. Meaning to represent -6 we will do a two step math process on +6 to get to -6 in binary. </p>
<p>Let's see how -6 will encode to binary:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/invert-1.png" alt="An illustration of bits getting inverted" width="600" height="400" loading="lazy"></p>
<p>In the previous sign magnitude approach, to calculate the negative of +6, we would have simply changed the sign bit from <code>0</code> to <code>1</code>. <code>0110</code> (+6) would become <code>1110</code> (-6).</p>
<p>In this new encoding scheme, we first invert the bits. Changing zeros to ones and ones to zeros. <code>0110</code> (+6) becomes <code>1001</code>. Inverting the bits is called "one's complement", so here we have calculated one's complement of <code>0110</code> resulting in <code>1001</code>. Then...</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/-6-1.png" alt="Adding binary one to 1001, resulting in 1001" width="600" height="400" loading="lazy"></p>
<p>We add <code>0001</code> (+1) to the one's complement we got from step one (<code>1001</code>). The result <strong><code>1010</code> will be the binary representation of -6. This encoding scheme is called two's complement.</strong> So keep in mind that calculating two's complement of a positive integer gives us its negative counterpart.  </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/twocomplementshort-4.png" alt="Steps of calculation of the two's complement of 0110" width="600" height="400" loading="lazy"></p>
<p>Inverting bits gives us the one's complement. Adding one to the one's complement gives us the two's complement of the original bits we started with. Simple, right?</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/2complement.png" alt="All possible numbers that can be represented by four bits with two's complement encoding scheme" width="600" height="400" loading="lazy">
<em>All possible numbers that can be represented by four bits with two's complement encoding scheme</em></p>
<p>Now, let's see why this encoding scheme is so beautiful. We'll add <code>0100</code> (+4) and <code>1111</code> (-1).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/2complesolution.png" alt="Addition of 0100 and 1111 when using two's complement encoding scheme" width="600" height="400" loading="lazy"></p>
<p>See, we get the accurate result with the two's complement encoding scheme. Now we can add integers without worrying about their signs. </p>
<p>We've learned how a negative integer can be represented in 0s and 1s via two's complement encoding. Now suppose we execute <code>ADD eax, -3</code> and the current value in the eax register is -1. So the value in eax after the execution of <code>ADD eax, -3</code> will be -4 (which is <code>1100</code> in two's complement encoding). </p>
<p>When the operating system retrieves <code>1100</code> from eax to present the result to the user, how does the operating system decode <code>1100</code> to decimal? Or suppose if we as a programmer come across <code>1100</code>, how can we figure out what number <code>1100</code> represents? </p>
<p>Of course we cannot keep on calculating two's complement of each positive integer to see when we hit <code>1100</code>. That will be too slow.   </p>
<p>Programmers and the OS use a beautiful property of two's complement to decode the binary into decimal. </p>
<p>When we calculate two's complement of a positive number, we get its negative counterpart. Well, <strong>the reverse is also true</strong> – which means calculating two's complement of a negative number will give us its positive counterpart. We will see the why of this in a minute. </p>
<p>First, let's understand how the OS or a programmer will decode <code>1100</code> to decimal.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/2complementexample-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>On retrieving <code>1100</code> from the eax register, the OS sees <code>1</code> as sign bit that signals that the integer is negative. Two's complement of <code>1100</code> is calculated that gives the positive counterpart of <code>1100</code> which comes out as <code>0100</code> (+4). The OS then prepends a negative sign on the positive counterpart and returns the final answer as -4. Re-read this paragraph once again and you'll get a better understanding.</p>
<p>Then the CPU smiles and says goodbye to the beauty for today ;) </p>
<p>The CPU has gone to its house to meet its mother. Now we have plenty of time to discuss the inner workings of the art of two's complement.</p>
<h2 id="heading-why-and-how-does-twos-complement-encoding-work">Why and how does two's complement encoding work?</h2>
<p>If I tell you to find the negative of a number, say +42, what's the simplest way to find the negative of +42? </p>
<p>Arguably, the simplest way is to subtract the number from 0, right? <code>0 - (+42) = -42</code>. If we repeat this, we get back to the positive value, <code>0 - (-42) = +42</code>. This is all the math that two's complement is built upon.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/zerominusnum-1.png" alt="Subtracting 0101 from 10000 resulting in 1011" width="600" height="400" loading="lazy"></p>
<p>We are doing <code>10000</code> (0 in decimal since the leftmost 1 is an overflow) minus <code>0101</code> (+5). We get <code>1011</code> that is -5 in decimal in two's complement encoding. Ignore how subtraction is done. That's not important. Understanding the intuition behind two's complement is important.</p>
<p><code>10000</code> can be written as <code>1111 + 0001</code> (try adding these two, you will get <code>10000</code>). So actually we are doing:</p>
<pre><code>        <span class="hljs-number">10000</span>       -   <span class="hljs-number">0101</span>
=&gt;  (<span class="hljs-number">1111</span> + <span class="hljs-number">0001</span>)   -   <span class="hljs-number">0101</span>
</code></pre><p>Rearranging the above equation we can write it as:</p>
<pre><code>    (<span class="hljs-number">1111</span> + <span class="hljs-number">0001</span>)  -  <span class="hljs-number">0101</span>
=&gt;  (<span class="hljs-number">1111</span> - <span class="hljs-number">0101</span>)  +  <span class="hljs-number">0001</span>

Step <span class="hljs-number">1</span>: subtract <span class="hljs-number">0101</span> <span class="hljs-keyword">from</span> <span class="hljs-number">1111</span>

        <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>
       <span class="hljs-number">-0</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span>
       ---------
        <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>

       see, subtracting <span class="hljs-number">0101</span> <span class="hljs-keyword">from</span> <span class="hljs-number">1111</span> is equivalent 
       to inverting the bits <span class="hljs-keyword">of</span> <span class="hljs-number">0101</span>, <span class="hljs-keyword">as</span> we got <span class="hljs-number">1010</span> <span class="hljs-keyword">as</span> a result. 



Step <span class="hljs-number">2</span>: add <span class="hljs-number">0001</span> to the above result  

        <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>  ---&gt; result <span class="hljs-keyword">of</span> step <span class="hljs-number">1</span>
       +<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span>
       ---------
        <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>      

       we get <span class="hljs-number">1011</span> that is <span class="hljs-number">-5</span> <span class="hljs-keyword">in</span> two<span class="hljs-string">'s complement encoding.</span>
</code></pre><p>Did you see that the two's complement system fundamentally does 0 minus the number? Inverting the bits and adding one is a fast and clever way to subtract the number from 0. </p>
<p>This is the reason we get the positive of a negative number and negative of a positive number when we calculate its two's complement – because we are actually subtracting the number from 0 (<code>0 - number</code>). </p>
<p>Computers in the 1900s used to have just the addition arithmetic logic because the two's complement encoding scheme is so beautiful that subtraction can easily be performed. </p>
<p>For example, to subtract 12 from 100, the CPU computes two's complement of +12 that produces -12 then we add -12 to 100 giving us the required output.  </p>
<p>Why don't we directly subtract from 0 to find the negative of a number or vice versa in binary? </p>
<p>Because subtraction is a slow and complicated process (thanks to borrowing) so our computer will need an expensive subtraction circuit if we go that way. Imagine subtracting from 0 every time we want to represent a negative integer. It'll be a nightmare for us and for our computers as well!</p>
<p>The two's complement encoding is a more performant solution, leads to a simple circuit design, and saves a lot of money. This is because we don't need an expensive circuit for subtraction and there's no additional logic to deal with the arithmetic of + and - integers. Just plain addition and we get to do both – add and subtract. </p>
<p>So let's appreciate our computer designers for this beautiful encoding scheme – <strong>the two's complement ❤️.</strong>  </p>
<h2 id="heading-final-words">Final words</h2>
<p>I promised myself that I would never charge for any learning material I produce. Whatever I do for education, whether it be a simple article or a course or an ebook, will always be 100% free and open. </p>
<p>I post useful resources and share meaningful thoughts on <a target="_blank" href="https://twitter.com/vkwebdev">my Twitter account</a>. You can follow me there and send me a DM if you learned something new from this article. It'll make my day :)</p>
<p>Every developer, every author, and every human being learns from someone. I believe the people and resources we learn from should be cited and spread. This encourages those good ones to do more for all of us. So here are my good ones.</p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=zxb8DvLUqcM">Animesh of mycodeschool</a> taught me many programming concepts better than anyone else including the concepts I wrote about in this article.</p>
<p><a target="_blank" href="https://jaenis.ch/">André Jaenisch</a>, my mentor and friend, without his reviewing efforts and constant support I would not have written this article.</p>
<p>Happy learning!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How does a CPU work? ]]>
                </title>
                <description>
                    <![CDATA[ By Milap Neupane CPU, also known as the microprocessor is the heart and/or brain of a computer. Lets Deep dive into the core of the computer to help us write computer programs efficiently. "A tool is usually more simple than a machine; it is general... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-does-a-cpu-work/</link>
                <guid isPermaLink="false">66d4604051f567b42d9f8495</guid>
                
                    <category>
                        <![CDATA[ Computer Science ]]>
                    </category>
                
                    <category>
                        <![CDATA[ cpu ]]>
                    </category>
                
                    <category>
                        <![CDATA[ programing ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 18 Jun 2019 22:00:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/06/1_n3hgXdDt8zb5pvjmBi570g.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Milap Neupane</p>
<p>CPU, also known as the microprocessor is the heart and/or brain of a computer. Lets Deep dive into the core of the computer to help us write computer programs efficiently.</p>
<blockquote>
<p><em>"A tool is usually more simple than a machine; it is generally used with the hand, whilst a machine is frequently moved by animal or steam power."</em>  </p>
<p><em>– Charles Babbage</em></p>
</blockquote>
<p><em>A</em> <strong><em>computer</em></strong> is a <strong>machine</strong> powered mostly by electricity but its flexibility and programability has helped achieve the simplicity of a tool.</p>
<p><strong>CPU</strong> is the heart and/or the brain of a computer. It executes the instructions that are provided to it. Its main job is to perform arithmetic and logical operations and orchestrate the instructions together. Before diving into the main parts let’s start by looking what are the main components of a CPU and what there roles are:</p>
<h3 id="heading-two-main-components-of-a-processor">Two main components of a processor</h3>
<ul>
<li><p><strong><em>Control unit — CU</em></strong></p>
</li>
<li><p><strong><em>Arithmetic and logical unit — ALU</em></strong></p>
</li>
</ul>
<h4 id="heading-control-unit-cu">Control Unit — CU</h4>
<p>Control unit CU is the part of CPU that helps orchestrate the execution of instructions. It tells what to do. According to the instruction, it helps activate the wires connecting CPU to different other parts of computer including the <strong>ALU</strong>. Control unit is the first component of CPU to receive the instruction for processing.</p>
<p>There are two types of control unit:</p>
<ul>
<li><p>hardwired <strong>control units</strong>.</p>
</li>
<li><p>microprogrammable (microprogrammed) <strong>control units</strong>.</p>
</li>
</ul>
<p><strong>Hardwired</strong> control units are the hardware and needs the change in hardware to add modify it’s working where as <strong>microprogrammable</strong> control unit can be programmed to change its behavior. Hardwired CU are faster in processing instruction whereas microprogrammable as more flexible.</p>
<h4 id="heading-arithmetic-and-logical-unit-alu">Arithmetic and logical unit — ALU</h4>
<p>Arithmetic and logical unit ALU as name suggest does all the arithmetic and logical computations. ALU performs the operations like addition, subtraction. ALU consists of logic circuitry or logic gates which performs these operations.</p>
<p>Most logic gates take in two input and produces one output</p>
<p>Bellow is an example of half adder circuit which takes in two inputs and outputs the result. Here A and B are the input, S is the output and C is the carry.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*u-VunK6bUafXlhubpGlNkA.png" alt="Image" width="180" height="100" loading="lazy"></p>
<p><em>Source:</em> <a target="_blank" href="https://en.wikipedia.org/wiki/Adder_\(electronics\)"><em>https://en.wikipedia.org/wiki/Adder_(electronics)</em></a></p>
<h3 id="heading-storage-registers-and-memory">Storage — Registers and Memory</h3>
<p>Main job of CPU is to execute the instructions provided to it. To process these instructions most of the time, it needs data. Some data are intermediate data, some of them are inputs and other is the output. These data along with the instructions are stored in the following storage:</p>
<h4 id="heading-registers">Registers</h4>
<p>Register is a small set of place where the data can be stored. A register is a combination of <strong>latches</strong>. <strong>Latches</strong> also known as <strong>flip-flops</strong> are combinations of <strong>logic gates</strong> which stores 1 bit of information.</p>
<p>A latch has two input wire, write and input wire and one output wire. We can enable the write wire to make changes to the stored data. When the write wire is disabled the output always remains the same.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*5WDU45YAH5CnICZOOvn1Yw.gif" alt="Image" width="500" height="365" loading="lazy"></p>
<p><em>An SR latch, constructed from a pair of cross-coupled</em> <a target="_blank" href="https://en.wikipedia.org/wiki/NOR_gate"><em>gates</em></a></p>
<p>CPU has registers to store the data of output. Sending to main memory(RAM) would be slow as it is the intermediate data. This data is send to other register which is connected by a <strong>BUS</strong>. A register can store instruction, output data, storage address or any kind of data.</p>
<h4 id="heading-memory-ram">Memory (RAM)</h4>
<p>Ram is a collection of register arranged and compact together in an optimized way so that it can store a higher number of data. RAM(Random Access Memory) are volatile and it’s data get’s lost when we turn off the power. As RAM is a collection of register to read/write data a RAM takes input of 8bit address, data input for the actual data to be stored and finally read and write enabler which works as it is for the latches.</p>
<h3 id="heading-what-are-instructions">What are Instructions</h3>
<p>Instruction is the granular level computation a computer can perform. There are various types of instruction a CPU can process.</p>
<p>Instructions include:</p>
<ul>
<li><p>Arithmetic such as <strong>add</strong> and <strong>subtract</strong></p>
</li>
<li><p>Logic instructions such as <strong>and</strong>, <strong>or</strong>, and <strong>not</strong></p>
</li>
<li><p>Data instructions such as <strong>move</strong>, <strong>input</strong>, <strong>output</strong>, <strong>load</strong>, and <strong>store</strong></p>
</li>
<li><p>Control Flow instructions such as <strong>goto</strong>, <strong>if … goto</strong>, <strong>call</strong>, and <strong>return</strong></p>
</li>
<li><p>Notify CPU that the program has ended <strong>Halt</strong></p>
</li>
</ul>
<p>Instruction are provided to computer using assembly language or are generated by compiler or are interpreted in some high level languages.</p>
<p>These instruction are hardwired inside CPU. ALU contains the arithmetic and logical where as the control flow are managed by CU.</p>
<p>In one <strong>clock cycle</strong> computers can perform one instruction but modern computers can perform more than one.</p>
<p>A group of instructions a computer can perform is called an <strong>instruction set</strong>.</p>
<h3 id="heading-cpu-clock">CPU clock</h3>
<p><strong>Clock cycle</strong></p>
<p>The speed of a computer is determined by its clock cycle. It is the number of <strong>clock periods</strong> per second a computer works on. A single clock cycles are very small like around 250 * 10 *-12 sec. Higher the clock cycle faster the processor is.</p>
<p>CPU clock cycle is measure in gHz(<strong>Gigahertz</strong>). 1gHz is equal to 10 ⁹ Hz(<strong>hertz</strong>). A hertz means a second. So 1Gigahertz means 10 ⁹ cycles per second.</p>
<p>The faster the clock cycle, the more instructions the <strong>CPU</strong> can execute.Clock cycle = 1/clock rateCPU Time = number of clock cycle / clock rate</p>
<p>This means to improve CPU time we can increase clock rate or decrease number of clock cycle by optimizing the instruction we provide to CPU. Some processor provide the ability to increase the clock cycle but since it is physical changes there might be over heating and even smokes/fires.</p>
<h3 id="heading-how-does-an-instruction-get-executed">How does an instruction get executed</h3>
<p>Instructions are stored on the <strong>RAM</strong> in a sequential order. For a hypothetical CPU Instruction consists of <strong>OP</strong> code(operational code) and <strong>memory or register address</strong>.</p>
<p>There are two registers inside a Control Unit <strong>Instruction register(IR)</strong> which loads the OP code of the instruction and <strong>Instruction address register</strong> which loads the address of the current executing instruction. There are other registers inside a CPU which stores the value stored in the address of the last 4 bits of a instruction.</p>
<p>Let’s take an example of a set of instruction which adds two number. The following are the instructions along with there description:</p>
<p><strong>STEP 1 — LOAD_A 8:</strong></p>
<p>The instruction is saved in RAM initially as let’s say &lt;1100 1000&gt;. The first 4 bit is the op code. This determines the instruction. This instruction is <strong>fetched</strong> into the <strong>IR</strong> of the control unit. The instruction is <strong>decode</strong> to be load_A which means it needs to load the data in the address 1000 which is the last 4 bit of the instruction to register A.</p>
<p><strong>STEP 2 — LOAD_B 2</strong></p>
<p>Similar to above this loads the the data in memory address 2 (0010) to CPU register B.</p>
<p><strong>STEP 3</strong> <strong>— ADD B A</strong></p>
<p>Now the next instruction is to add these two numbers. Here the CU tells ALU to perform the add operation and save the result back to register A.</p>
<p><strong>STEP 4 — STORE_A 23</strong></p>
<p>This is a very simple set of instruction that helps add two numbers.</p>
<p>We have successfully added two numbers!</p>
<h4 id="heading-bus"><strong>BUS</strong></h4>
<p>All the data between CPU, register, memory and IO devise are transferred via bus. To load the data to memory that it has just added, the CPU puts the memory address to address bus and the result of the sum to data bus and enables the right signal in control bus. In this way the data is loaded to memory with the help of the bus.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*N5wkXycN_ceV9HByxQUzCA.png" alt="Image" width="640" height="469" loading="lazy"></p>
<p><em>Source:</em> <a target="_blank" href="https://en.wikipedia.org/wiki/Bus_\(computing\)"><em>https://en.wikipedia.org/wiki/Bus_(computing)</em></a></p>
<h4 id="heading-cache">Cache</h4>
<p>CPU also has mechanism to prefetch the instruction to its cached. As we know there are millions of instruction a processor can complete within a second. This means that there will be more time spent in fetching the instruction from RAM than executing them. So the CPU cache prefetches some of the instruction and also data so that the execution gets fast.</p>
<p>If the data in cache and operating memory is different the data is marked as a <strong>dirty bit</strong>.</p>
<h4 id="heading-instruction-pipelining"><strong>Instruction pipelining</strong></h4>
<p>Modern CPU uses <strong>Instruction pipelining</strong> for parallelization in instruction execution. Fetch, Decode, Execute. When one instruction is in decode phase the CPU can process another instruction for fetch phase.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*FJxls8ZBHc3l3tTKxrO6Sg.png" alt="Image" width="501" height="480" loading="lazy"></p>
<p><em>Source:</em> <a target="_blank" href="https://en.wikipedia.org/wiki/Instruction_pipelining"><em>https://en.wikipedia.org/wiki/Instruction_pipelining</em></a></p>
<p>This has one problem when one instruction is dependent on another. So processors execute the instruction that are not dependent and in different order.</p>
<h4 id="heading-multi-core-computer">Multi core computer</h4>
<p>It is basically the different CPU but has some shared resource like the cache.</p>
<h3 id="heading-performance">Performance</h3>
<p>Performance of CPU is determined by it’s execution time. Performance = 1/execution time</p>
<p>let’s say it takes 20ms for a program to execute. The performance of CPU is 1/20 = 0.05msRelative performance = execution time 1/ execution time 2</p>
<p>The factor that comes under consideration for a CPU performance is the instruction execution time and the CPU clock speed. So to increase the performance of a program we either need to to increase the clock speed or decrease the number of instruction in a program. The processor speed is limited and modern computer’s with multi core can support millions of instructions a second. But if the program we have written has a lot of instructions this will decrease the overall performance.</p>
<p><strong>Big O notation</strong> determines with the given input how the performance will be affected.</p>
<p>There are a lot of optimization done in CPU to make it faster and perform as much as it can. While writing any program we need to consider how reducing the number of instruction we provide to CPU will increase the performance of computer program.</p>
<hr>
<p>Also Posted on Milap Neupane Blog: <a target="_blank" href="https://milapneupane.com.np/2019/07/06/how-does-a-cpu-work/">How Does a CPU work</a></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
