How instructions are differentiated from data?

后端 未结 6 1377
长情又很酷
长情又很酷 2020-12-01 12:42

While reading ARM core document, I got this doubt. How does the CPU differentiate the read data from data bus, whether to execute it as an instruction or as a data that it c

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 13:30

    I think its down to where the data is stored in the program and OS support for informing the CPU whether it is code or data.

    All code is placed in different segment of the image (along with static data like constant character strings) compared to storage for variables. The OS (and memory management unit) need to know this because they can swap code out of memory by simply discarding it and reloading it from the original disk file (at least that's how Windows does it).

    So, I think the CPU 'knows' whether memory is data or code. No doubt the modern pipeling CPUs we have now also have instructions to read this memory differently to assist the CPU is processing it as fast as possible (eg code may not be cached, data will always be accessed randomly rather than in a stream)

    Its still possible to point your program counter at data, but the OS can tell the CPU to prevent this - see NX bit and Windows' "Data Execution Protection" settings (system control panel)

提交回复
热议问题