How do the data, address and instruction differ in the processor / register / memory?

In a processor, how do data, address, and instructions differentiate? When the program is executed, how does the processor differentiate the instructions, when everything is at 0 s and 1 , and loads the data as data, addresses?

0
cpu-registers machine-instruction
source share
3 answers

The processor does only what you tell it. As you noted, the processor cannot determine the difference between โ€œdataโ€ and โ€œcodeโ€ in memory: all this is just a sequence of bytes. This is what you say what needs to be done with these bytes, which determine how it is processed.

When the program is compiled, the generated executable file contains information that says which parts are code and which are data. When the program is executed, the operating system loads the code and data into different parts of the memory, and then tells the processor to start executing the code at the program entry point. From there, the processor extracts the first command, executes it, and proceeds to the next command.

This is all very simplified, of course, but I think you understand the idea.

On older processors and older operating systems, nothing prevents you from telling the processor to start executing instructions in the middle of the data segment. Or, in essence, from changing โ€œdataโ€ in the middle of a code segment, thereby making self-modifying code. Newer processors and operating systems typically have some form of data prevention and locking to prevent code changes. Otherwise, self-modifying code can be a huge security risk.

Short answer: the processor treats the code as code because you are telling it. Otherwise, all are just bytes in memory.

+3
source share

Different registers help the processor distinguish between different segments of memory in a process running on a computer. When the program starts, the segment code register (cs) and the instruction pointer (ip / eip / rip) are configured to indicate where the code is located, while the data segment register (DS) and one of the general purpose registers (usually DX) are used to Indication of the data segment. In the best way, that is mainly Intel x86 architecture, but in general, most architectures have registers for demarcating the code area from the data area, as well as the stack segment. Through these registers, the processor "knows" or is able to distinguish code-code

+2
source share

When u starts the application, eip (program counter) is installed in the place where the text data of your program is, text data is your code (0..1). From here, he begins to execute instructions from the address that is placed on the eip. The instructions are defined on the ROM next to the processor (or cache), if for some reason the processor tries to execute something that is not an instruction that generates an exception (interrupt at this level). How does he know if this is an instruction or does not foresee that the foreach command that retrieves checks the similar and hash table to see if it is a valid instruction.

This is a very simple approach to the problem, because many things happen XDD.

0
source share

All Articles