Select Page
Poker Forum
Over 1,292,000 Posts!
Poker ForumFTR Community

Help me understand what goes on in a CPU.

Results 1 to 21 of 21

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #15
    Quote Originally Posted by MadMojoMonkey View Post
    Can anyone critique this?
    If you say:
    int x = 5;
    x = x + 3

    Then you are talking in a high level programming language.
    The CPU would not understand anything, it would need an interpreter* or a compiler.
    *(this may sound like an analogy but it is actually an application for an interpeted programming language)

    The interpreter would evaluate the expression using it's own code.
    A compiler would compile it to CPU usable machine code, (but the actual code would depend on so many things that it is not possible to provide a simple translation here without defining a bagfull of assumptions)

    To understand the CPU better try looking at the state of the art micro computing from 1975 with the Altair 8800
    http://www.youtube.com/watch?v=EV1ki6LiEmg

    So after watching the video you should now realise you need to write the machine code and load it in to the machine for the cpu to process.
    Therefore as the video showed how to turn the computer on and jump to location 20 we could enter our code at location 20.

    First we need the 8080 processor Op code to load the accumulator.
    There are several variations so we will choose the one to do it immediately with one byte (code 3E) which uses the next memory location for the immediate value.
    So you would use your switches to enter 3E at 20
    then 5 at 21 for your value of x=5.

    Now the CPU expects another Operation Code in the next location 22.
    So we need to choose an Add instruction (eg C6 add immediately with the next byte)
    (we could instead have chosen code 86 to add immediately with the next two bytes for bigger numbers, or 8E which would do the same but also uses a Carry, or 80 which would add the contents of the B register to the accumulator if we had loaded something in to the B register)
    Flick the switches to enter C6 to location 22, then enter 3 to location 23, which completes our x=x+3 calculation.

    The next opcode will be expected in location 24, so as our answer at the moment is just in the accumulator we could tell the cpu to store it in a memory location.
    This will be a 3 byte command, The opcode followed by Low Byte and High Byte of the memory location.

    When you have entered all that code, then you can put your programme counter back to zero and run the whole programme.
    (or you could put the programme counter to 20 and just run the commands we talked about here without processing that first Jump command).
    After running if you inspect the memory location you specified to store the accumulated value to, it should show the value 8 is now in that location.


    Code:
    Notice that the CPU itself only works with numbers.
    Programmers prefer to use mnemonics. Later they would use an Assembler to generate the machine code.
    
    Intel 8080
    MVI A,5
    ADI 3
    
    Zilog z80
    LD  A,5
    ADD A,3
    
    But coincidentally (although not a coincidence because Zilog like AMD later was aiming for some compatability) in both cases the memory would be stuffed with the same values.
    3E,05
    C6,03
    
    The MOS6502 however used completely different mnemonics and codes.
    A9,05  LDA #05
    69,03  ADC #03


    Quote Originally Posted by MadMojoMonkey View Post
    I.e. some processors use stacks and some don't.
    Am I wrong there?
    I don't know of any processor that doesn't use a stack.
    They usually have commands to PUSH and POP, (the 6502 calls it PHA and PLA).
    The processor also uses it with the Jump to Subroutine command, pushing the current address to the stack and pulling it back of the stack when it gets to the RTS command.
    Last edited by chemist; 10-10-2014 at 09:33 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •