How does machine code interact with the processor?

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. The assembly is written in machine code. (This is just a vague idea that they correct me in this way if I am mistaken). But what kind of machine code is written, or more precisely , how does the processor process its instructions, how does it “know” what to do?

+4
source share
4 answers

If I'm not mistaken when you program in it, the computer first "translates" the code to C.

No no. C is nothing special, except that it is the most common programming language used for system programming.

The Python interpreter converts Python code into so-called P-code, which is executed by the virtual machine. This virtual machine is the actual interpreter that reads the P-code, and each display of the P-code forces the interpreter to execute a predefined encoding. This is not very similar to how native binary machine code controls the processor. A more modern approach is to convert the P-code into native machine code.

The CPython interpreter itself is written in C and compiled into its own binary file. Basically, the native binary code is just a long series of numbers (operation codes), where each number represents a specific operation. Some operation codes indicate to the machine that a certain number of numbers following it are not operation codes, but parameters.

The processor itself contains a so-called instruction decoder, which reads its own binary number by number and for each operation code that it reads, gives power to the CPU circuit that implements this particular operation code. there are operation codes, address memory, operation codes that load data from memory into registers, etc.

how the processor processes its instructions; how does it “know” what to do?

For each operation code, which is just a binary pattern, the CPU has its own scheme. If the operation code template matches the “switch” that activates this operation code, this circuit processes it.

Here's a wiki book: http://en.wikibooks.org/wiki/Microprocessor_Design

A few years ago, a guy built an entire working computer from a simple logic of functions and memory chips, i.e. no microcontroller or the like. The whole project, called "Big Mess o 'Wires" , was a more or less integrated processor from scratch. The only thing that would be absurd would be the construction of this thing from single transistors (which in fact was not much more complicated). It also provides a simulator that allows you to see how the processor works internally, decodes each command and executes it: Big Mess o 'Wires Simulator

+7
source

The machine code does not "communicate with the processor."

Rather, the processor “knows how to evaluate” machine code. In the architecture of the [widespread] Von Neumann, this machine code (program) can be thought of as an index array in which each cell contains machine code (or data, but this time ignored).

The CPU "looks" at the current instruction (often referred to as a PC or program counter ) and decides what to do (this can either be done directly with transistors / bare metal or transferred to an even lower code level): this is called fetch-decode-execute .

When instructions are executed, side effects occur, such as setting a control flag, placing a value in a register, or switching to another index (changing a PC) in a program, etc. See this simple processor review that best covers the above.

This is an assessment of each team - how it meets - and the interaction of side effects that lead to the operation of a traditional processor.

(Of course, modern processors are very complex and do a lot of neat things!)

+2
source

Electricity. Schemes, memory and logic blocks.

Also, I believe that Python is usually interpreted rather than compiled using C -> assembly -> machine code.

+1
source

This is called microcode . This is the code in the CPU that reads the machine code instructions and converts them into a low-level data stream.

When the processor, for example, encounters an add statement, the microcode describes how it should receive two values, submit them to the ALU to perform the calculation, and where to put the result.

+1
source

Source: https://habr.com/ru/post/1402016/


All Articles