Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How does a processor work

  1. Apr 18, 2007 #1
    i want to know how it works, on a really basic scale (like what components do what). The farthest i've read about is on the assembly language level. I want to know how the hardware works.

    any ideas?
  2. jcsd
  3. Apr 19, 2007 #2
    Indeed, this interests me too. I can't connect the concept of how a transistor behaves with the ability to perform binary arithmetic. I've seen circuit diagrams of logic gates, but I haven't followed them through to see what's actually going on.
  4. Apr 19, 2007 #3


    User Avatar
    Science Advisor

    Basically you start off with some logic gates, like the AND, NOT and OR, for example.
    Each gate receives one or more binary inputs (each either 0 or 1). The AND outputs a 1 if both its inputs are a 1 and a 0 otherwise. The OR outputs 1 if either input is a 1 and 0 otherwise. The NOT outputs a 1 if the input is 0, and 0 if it is 1.
    With these gates you build components for adding numbers. Addition of binary numbers is the same as addition of regular (base 10) numbers, for example 6+2 is, using 16-bit numbers:
    Code (Text):

    We can implement addition using the basic logic gates, to form an 8-bit adder, which adds two 8-bit numbers. The adder starts at the rightmost bits of both inputs and moves left, computing the sum and any carryovers for the next column, just like we do manually.
    Once we have 8-bit adders, or X-bit adders, we can stack them, or cascade them for summing numbers of 16, 32, 64 bits, etc.
    With addition implemented you can now think about implementing multiplication, and then division, there are some popular & clever algorithms for doing multiplication and division using basic gates.
    Some other operations you might want to do are shifts, i.e. shift all bits right or left:
    shift 001 left = 010
    So you develop a collection of operations all acting on 1 or more inputs:
    Logical AND
    Logical NOT
    Logical OR
    Logical XOR
    Shift Left
    Shift Right

    Then once you have these operations in place you want to use them. So you want to bring inputs in and perform operations on them. The CPU has a number of registers which store inputs and outputs. For bringing in input from main memory and storing it in the registers, so that you perform operations on them, you add an operation from moving bits from main memory into the registers, that's another operation.
    Now that we have so many operations, we want to tell the CPU what operation to do. So, we encode the operations in some bit pattern, for example:
    0001 Perform NOT
    0010 Perform AND
    0011 Perform OR
    0100 Perform Addition
    0101 Perform Subtraction
    0110 Perform Multiplication
    0111 Perform Division
    1000 Move bits from memory into registers

    Now, suppose you want the CPU to perform addition of 6 and 2, you pass it in the operation code (0100), the number 6 (0110) and the number 2 (0010), so something like:
    0100 0110 0010
    Now we can start talking about a sequence of operations:
    0100 0110 0010
    01010 010 0010
    0100 0110 0110
    0010 1000 0010
    1000 1010 0010
    This Machine Language is hard to work with so we develop assembly languages, which compiles to Machine Language, so for adding two numbers maybe now we can do:
    ADD $R1 2
    Which adds 2 to the value in register 1 perhaps. This way it's easier to develop computer programs. One other operation we might now be interested in developing is the Jump operation. Which jumps to the Nth operation and continues from there. Maybe the Machine Code for jumping to line 4 is:
    1001 0010 0000
    and the assembly command is
    JMP 4
    JMP #MyLabel

    With jumps we can start developing conditionals, i.e. Jump to this line if A > B (that reminds me, implement CPU support for >, <, ==)
    We can also start thinking about loops, i.e. if you reach this line and Y is 0, jump to the line X lines back and repeat.
    We reach a point where we think we can do better than assembly, so we develop higher level languages like C, C++ which maybe compile to assembly and then to machine code, or to machine code directly.
    Then we use those languages to develop frameworks, such as the Java virtual machine, or the .NET framework, and now the underlying CPU implementation isn't relevant anymore as far as your programming goes.

    CPUs also have to implmement caches, because bringing in data from main memory is not as fast as we'd like. These caches cache the data we bring in from memory. There's also internal stuff like the instruction pointer, which points to the next operation to perform. There's also "interrupts" which trigger code routines to run (this way when you plugin a USB device, an interrupt is generated, the CPU runs some code, which the Operating System can recognize and handle for instance).

    These are some of the main ideas, but CPU makers are looking for performance, so they implement different kinds of strategies and approaches.
  5. Apr 19, 2007 #4


    User Avatar

    Staff: Mentor

  6. Jun 5, 2008 #5
    Great post -Job-! I've been a programmer for a long time but I've had a new desire to get lower and lower into the technology. Thanks for the heads up. Google thinks your explanation is worthy as well.
  7. Jun 5, 2008 #6


    User Avatar
    Science Advisor
    Homework Helper

    There is an excellent book called 'code' by Charles Petzold that explains how computers/binary/etc work for non technical audience.
    It's a little basic if you already know assembler but is very well written.
  8. Sep 9, 2010 #7
    ok guys my brother in law and i have been having this discusion about how this all works .

    i have tried to explain to him how the programs are compiled into binary machine code .
    and the machine code is read in the form of electrical current .

    but in all my research and reading one thing keeps coming to mind,so please correct me if im wrong.

    the way i understand it is this: the processor is not smart,it only gets instructions on what to do from programs through your ram/harddrive in the form of electrical current .

    and depending on the rout of the current the cpu will open or close its switches ,thus either rerouting impulse or stopping the flow of impulse.

    so is the processor "dumb"? does it really act as a wet brick untill it is told what to do in the form of electrical impulse through the binary code?

    the way i understand it is the ram reads data from the harddrive ,loads the data ,then outputs the data through a series of electrical "neuro pathways" causing switches or relays to be triggered thus outputing code in the form of current to be redirected to other parts of the system.

    please correct me or fill in the gaps for me.
  9. Sep 9, 2010 #8


    User Avatar
    Homework Helper

    One thing not mentioned yet is how computers convert instruction opcodes into actual operations. The simplest form of these is probably is to used the opcod to index into a table of "bit per function" values, which was common on 2901 bit sliced based mini-computers. These machines had a limited number of registers (just 4), and limited number of instructions, so an 80 bit value would be large enough to represent all 80 possible instruction combinations possible. The opcode would index into the "bit per function" table, and which bit that was set would enable the circuitry to perform that specific instruction.

    A step up from this would be to index into the start of a series of bit per function values, so that a series of operations could be performed, there would need to be a bit value to indicate the end of a series for a particular instruction.

    IBM implemented a similar concept with it's "micro-code" handling of 360 machine language opcodes. Depending on the machine the "micro-code" handled all the "native" instructions implemented on the machine, and exception handling and conventional machine language routines were used to handle "native" instructions not implemented on a particular machine.
    Last edited: Sep 9, 2010
  10. Sep 9, 2010 #9
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook