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

狂风中的少年 提交于 2019-12-19 04:44:15

问题


In Processor, how are Data, Address and instructions are differentiated? When a program is executed, how is processor differentiate instructions when everything is in 0s and 1s and register`s load both data, addresses?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/4443324/how-is-data-address-and-instruction-differentiated-in-processor-register-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!