Let's take Python as an example. If I am not mistaken, when you program in it, the computer first "translates" the code to C. Then again, from C to assembly. Assembly is written in machine code. (This is just a vague idea that I have about this so correct me if I am wrong) But what's machine code written in, or, more exactly, how does the processor process its instructions, how does it "find out" what to do?
If I am not mistaken, when you program in it, the computer first "translates" the code to C.
No it doesn't. C is nothing special except that it's the most widespread programming language used for system programming.
The Python interpreter translates the Python code into so called P-Code that's executed by a virtual machine. This virtual machine is the actual interpreter which reads P-Code and every blip of P-Code makes the interpreter execute a predefined codepath. This is not very unlike how native binary machine code controls a CPU. A more modern approach is to translate the P-Code into native machine code.
The CPython interpreter itself is written in C and has been compiled into a native binary. Basically a native binary is just a long series of numbers (opcodes) where each number designates a certain operation. Some opcodes tell the machine that a defined count of numbers following it are not opcodes but parameters.
The CPU itself contains a so called instruction decoder, which reads the native binary number by number and for each opcode it reads it gives power to the circuit of the CPU that implement this particular opcode. there are opcodes, that address memory, opcodes that load data from memory into registers and so on.
how does the processor process its instructions, how does it "find out" what to do?
For every opcode, which is just a binary pattern, there is its own circuit on the CPU. If the pattern of the opcode matches the "switch" that enables this opcode, its circuit processes it.
Here's a WikiBook about it: http://en.wikibooks.org/wiki/Microprocessor_Design
A few years ago some guy built a whole, working computer from simple function logic and memory ICs, i.e. no microcontroller or similar involved. The whole project called "Big Mess o' Wires" was more or less a CPU built from scratch. The only thing nerdier would have been building that thing from single transistors (which actually wasn't that much more difficult). He also provides a simulator which allows you to see how the CPU works internally, decoding each instruction and executing it: Big Mess o' Wires Simulator
EDIT: Ever since I originally wrote that answer, building a fully fledged CPU from modern, discrete components has been done: For your considereation a MOS6502 (the CPU that powered the Apple II, Commodore C64, NES, BBC Micro and many more) built from discetes: https://monster6502.com/
Machine-code does not "communicate with the processor".
Rather, the processor "knows how to evaluate" machine-code. In the [widespread] Von Neumann architecture this machine-code (program) can be thought of as an index-able array of where each cell contains a machine-code instruction (or data, but let's ignore that for now).
The CPU "looks" at the current instruction (often identified by the PC or Program Counter) and decides what to do (this can either be done directly with transistors/"bare-metal", or it be translated to even lower-level code): this is known as the fetch-decode-execute cycle.
When the instructions are executed side-effects occur such as setting a control flag, putting a value in a register, or jumping to a different index (changing the PC) in the program, etc. See this simple overview of a CPU which covers the above a little bit better.
It is the evaluation of each instruction -- as it is encountered -- and the interaction of side-effects that results in the operation of a traditional processor.
(Of course, modern CPUs are very complex and do lots of neat tricky things!)
That's called microcode. It's the code in the CPU that reads machine code instructions and translate that into low level data flow.
When the CPU for example encounters the add
instruction, the microcode describes how it should get the two values, feed them to the ALU to do the calculation, and where to put the result.
add reg, reg
like this answer describes. Later CPUs like maybe 486 and definitely Pentium decoded whole x86 instructions to one internal thing that goes through the pipeline. (For Pentium, dual-issue in-order, add ecx, edx
and add eax, edx
could both go through separate pipes in the same clock cycle.) But more complex instructions like add [ecx], eax
didn't get broken up, but couldn't pair and occupied the pipeline for more cycles). (Very complex instructions like lock cmpxchg
were still microcoded.) –
Then add [ecx], eax
into separate load / ALU / store-address / store-data uops (so it could schedule them separately in the out-of-order back-end, overlapping with work from other instructions). In x86 terminology, this is still not "microcoded"; the decoders produce those uops directly. A microcoded instruction is one where the uops are read from a ROM, when more than 4 uops are needed for one insn like rep movsb
(memcpy). What is a microcoded instruction?) –
Then Electricity. Circuits, memory, and logic gates.
Also, I believe Python is usually interpreted, not compiled through C → assembly → machine code.
Many people start off by explaining "When you press a key on the keyboard, then...".
Lets talk backwards. There is something called "Microcode" which breaks down your machine instructions into CPU specific code. Instructions are fetched from RAM (where a bunch of 0s and 1s in voltages are already stored as instructions for the cpu) in a process called a fetch-decode-execute cycle. This is also where cpu clock cycles come in.
Now you might ask hey, I have an IDE, in the form of an executable. My IDE can take care of the compilation and all the hard work. Then how does the IDE start up?
An operating system has something called a loader that feeds the executable instructions to RAM (setting up what you call "voltages" in RAM as instructions). This in turn gets read by the CPU and translated into microcode which then gets fed into other components.
An excellent book - https://www.amazon.com/dp/1558604286
When you press a key on the keyboard, then - https://www.youtube.com/watch?v=rNl9ol_rPMs
Novice guide to fetch-decode-execute - https://www.youtube.com/watch?v=-IyB8hBkA9w
© 2022 - 2024 — McMap. All rights reserved.