Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

How machine code works?

Options
  • 18-04-2009 8:12pm
    #1
    Closed Accounts Posts: 221 ✭✭


    One thing I've never seen explained - Say you use assembly commands such as add, mul, cmp, div which in turn get converted into the relevant machine code...How does the processor then actually know how to add/multiply/etc.. two numbers together? How does a piece of material with electriciy running through it know how to add? Anyone know?


Comments

  • Registered Users Posts: 6,440 ✭✭✭jhegarty


    On a very basic level : http://en.wikipedia.org/wiki/Logic_gate


  • Registered Users Posts: 21,611 ✭✭✭✭Sam Vimes


    This is a basic adder:

    attachment.php?attachmentid=77697&stc=1&d=1240090697

    The top logic gate is and AND gate and it gives a 1 out if both of the inputs are 1. The bottom one is an Exclusive OR gate (or XOR) and it gives a 1 out if either of the inputs are 1 but not both


    If the input on both lines is 0, both gates give out 0.

    If one input is 1 (we want 0+1=1), the XOR gate gives out 1 and the AND gate gives out 0, so the result is from top to bottom is 01 (ie 1)

    If both of the inputs are 1 (we want 1+1=2), the XOR gate gives out 0 and the AND gate gives out 1. So the output from top to bottom is 10, which is 2 in binary

    Now isn't that confusing :D


  • Registered Users Posts: 32,417 ✭✭✭✭watty


    Simple computers use logic gates. And can only count to 1
    you can do ALL arithmetic, even to 80 bits floating point by only being able to count to 1

    More complicated computers have a simpler computer to execute MicroCode to control the logic, tests, arthmetic and copies.

    ALL instructions either:
    Test, setting flags
    Move stuff
    do arithemtic or logical operation (which can set a flag).
    Branch to two different instructions based on flag

    I wrote an assembler 16bit/32bit fixed point library once for a VERY simple CPU
    Its Only two arithmetic operations were complement and Add (8bits).
    Conditional was to either execute next instruction or skip it based on flag.

    Ultimately all more powerful computer chips internally work in a related way and could be built out of clockwork or relays.
    Even a Xeon or AMD64. It would be a very large amount of relays and using state of the art relays about 20 instructions per second. The early valve based computers could go 1000s of times faster. Computers have been built from relays.

    A simple full 8 bit adder needs about 200 transistors to create all the gates, or about 16 to 24 relays... depending on the type.
    A full Arithemetic / Logic unit (not an FPU) can be done in about 5,000 transistors.
    While a Pentium might have 1 Million transistors to 100Million depending on generation, a State of the Art RISC CPU might only use 125,000. This is why x86 is not used in smart phones and ARM is. The ARM can use 1/1000th of the power and leave 19/20ths of the chip empty for other non-CPU functions.

    Computers are really quite simple and stupid. There is just a lot of duplicated simplicity and high speed repetition of the stupidity.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Yeah basically a fuckton of logic gates. Just think of bitwise operations, its basically that in hardware form. Quite amazing really that you can do so much with computers just by doing a few tiny operations really, really fast.


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    I would suggest reading about Alan Turing and his "universal machine". Most books on Turing can be very heavy but in his book "The Emperor's new mind", Roger Penrose gave a great explanation (i.e. short & simply :)) of how, using Turing's ideas, a "load of 1s & 0s" can actually result in adding & multiplying or anything else. It's not directly transferable to the processors we use today, perhaps, but the concept is the same.


  • Advertisement
  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    A bit more in-depth, but not as hard to read as you might think : Microprocessor Architecture


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,353 Mod ✭✭✭✭Capt'n Midnight


    If it helps you can build a computer that only needs one instruction. OISC . The only instruction is Subtract and Branch if Negative. So you can simplify the hardware and everything after that is just using extra hardware to speed it up.
    Its operands are three addresses, X, Y, and Z. This instruction subtracts the number at address Y from the number at address X, stores the result at address X, and if the result is negative, branches to address Z

    Computers don't even have to do calculations
    http://www.skynet.ie/~darkstar/history/
    The second, third and fourth computers built by Stiblity and Williams were used by the army for ballistics. The fifth model could be split in two to work on two separate problems. It had 9000 relays and weighed 10 tons. It performed arithmetic by looking up tables. (CADET: Can't Add, Doesn't Even Try). However, it could handle floating-point numbers.

    Given the enough memory it should be possible to have a computer that works by just using the instruction and operands as pointers to the address that the result is stored in, no computation or bit shifting and almost no logic needed. If the OISC above was 8 bit then you would only need 16.7MB of ROM. Memory addressing can be done by simple AND / OR logic.




    At an electronic level you can think of most cpus as a bunch of field effect transistors. A static charge of electricity if present on the gate repels electrons so they can't flow through the body of the transistor. If there is no static charge the electrons can flow.
    The static charge is held by a capacitor so it "remembers" it's state. The capacitor can be charged or discharged because it's connected to another transistor.
    You can put transistors in series so you get NO current flow when there is a charge on the first capacitor OR the second one blocking either transistor.
    You can put them in parallel so you only get NO current flowing if the is a charge on the first AND second capacitor.


    My favorite hardware speedup is the carry look ahead adder


  • Closed Accounts Posts: 2 robwoods68


    Code
    The Hidden Language of Computer Hardware and Software
    Charles Petzold

    Rob


Advertisement