开发者

How is data, address and Instruction differentiated in Processor/Register/memory?

开发者 https://www.devze.com 2023-01-31 03:09 出处:网络
In Processor, how are Data, Address and instructions are differentiated? When a program is executed, how is processor differentiate instructi开发者_运维知识库ons when everything is in 0s and 1s and re

In Processor, how are Data, Address and instructions are differentiated? When a program is executed, how is processor differentiate instructi开发者_运维知识库ons when everything is in 0s and 1s and registers load both data, addresses?


The processor only does what you tell it to do. As you noted, the processor can't tell the difference between "data" and "code" in memory: it's all just a sequence of bytes. It's what you tell it to do with those bytes that defines how it's treated.

When a program is compiled, the generated executable file has information in it that says which parts are code and which parts are data. When a program is executed, the operating system loads code and data into different parts of memory and then tells the processor to start executing code at the program's entry point. From there, the processor fetches the first instruction, executes it, and moves on to the next instruction.

That's all very simplified, of course, but I think you get the idea.

In older processors and older operating systems, nothing stops you from telling the processor to start executing instructions that are in the middle of a data segment. Or, in fact, from modifying "data" in the middle of a code segment--thereby making self-modifying code. Newer processors and operating systems usually have some form of data execution prevention and locks to prevent modification of code. Otherwise, self-modifying code can become a huge security risk.

The short answer: the processor treats code as code because you tell it to. Otherwise, everything's just bytes in memory.


The various registers help the processor in distinguishing between the various segments of memory in a process executing on a computer. When a program start the Code Segment register (cs) and Instruction Pointer (ip/eip/rip) are set up to point to where the code is, whereas the Data segment register (DS) and one of the general purpose registers (typically DX) are used to point into data segment. Well that's mostly Intel x86 architecture, but in general, most architectures have registers to demarcate the code region from the data region, as also the stack segment. Through these registers, the cpu "knows" or is able to distinguish between code address


When u start an application the eip (program counter) is set to where the text data of your program is, the text data is your code (0..1). From here it starts to execute instructions from the address that is putted on eip. The instructions are defined on a ROM near the cpu (or cache) if by any chance the cpu tries to execute something that is not an instruction it throws an exception (interrupt on this level). How he knows if it is instruction or not imagine that foreach instruction that is fetch he checks the rom like and hashtable to see if it is a valid instruction.

This is a very simple aproach of the problem because many things happen XDD.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号