简体   繁体   English

冯·诺伊曼模型中的控制单元如何区分数据和指令?

[英]How does the Control Unit in Von Neuman Model distinguish between data and instructions?

My text book doesn't seem to answer this question, just that it has to 'decode' the instruction, so it doesn't answer how it knows it has an instruction in the first place. 我的教科书似乎没有回答这个问题,只是它必须“解码”指令,因此它没有回答它首先知道有一条指令的方式。

My research into this gives me possibly two answers: 我对此的研究可能给了我两个答案:

1) It can't because both data and instructions look the same in memory, so it has to use the Program Counter to load the next instruction which may or may not fetch the next data it needs through addressing. 1)不能,因为数据和指令在内存中看起来都一样,所以它必须使用程序计数器来加载下一条指令,该指令可能通过寻址也可能不获取其所需的下一条数据。

2) Something something opcodes something.... 2)某些内容对某些内容进行操作码...

I'm trying to figure out a clearly understandable way of explaining this so I know I can understand it. 我正在尝试找出一种清晰易懂的方式来解释这一点,以便使我知道。

The processor fetches and decodes whatever memory is pointed at by the Program Counter. 处理器获取并解码程序计数器指向的任何内存。

The decoder can throw an "invalid instruction" exception if it does not understand the instruction. 如果解码器不理解该指令,则会抛出“无效指令”异常。 It will then jump to the exception handler (ie, the OS), which will try to make sense of the invalid instruction. 然后它将跳转到异常处理程序(即OS),它将尝试使无效指令有意义。 Often, it may just be an unsupported instruction that can be handled in software (eg, floating point divide), and then execution can resume from where it had left off. 通常,它可能只是不受支持的指令,可以在软件中进行处理(例如,浮点除法),然后可以从中断处继续执行。

However, if it's a true invalid instruction, then the program will error out with some sort of exception/crash ( "Illegal Instruction" on x86 I believe). 但是,如果这是一条真正无效的指令,则该程序将因某种异常/崩溃而出错(我相信x86上的“非法指令” )。

One technique to help separate code from data is to put them on different "pages" in virtual memory. 帮助将代码与数据分离的一种技术是将它们放在虚拟内存中的不同“页面”上。 The OS can then mark the page that the code sits on as "read-only". 然后,操作系统可以将代码所在的页面标记为“只读”。 In this manner, a program trying to overwrite code will throw an exception. 这样,试图覆盖代码的程序将引发异常。 Some systems allow even more protection, such as "Fault on Execute" - throw an exception if you try to execute code on this page (More info here: http://www.tldp.org/LDP/tlk/mm/memory.html ). 某些系统甚至可以提供更多保护,例如“执行时出错”-如果您尝试在此页上执行代码,则会引发异常(更多信息,请访问此处: http : //www.tldp.org/LDP/tlk/mm/memory。 html )。

Things can get a bit weird though in that some code actually does need to be "modifiable" by the program (aka, "self modifying code"). 事情可能有些怪异,尽管实际上某些代码确实需要由程序“修改”(即“自修改代码”)。 This is made difficult because you typically have separate instruction caches and data caches. 由于您通常具有单独的指令高速缓存和数据高速缓存,因此这变得很困难。 Typically, the instruction cache is oblivious to the world around it - it will have no idea that somebody has written new data to the addresses it contains. 通常,指令高速缓存不了解它周围的世界-它不知道有人将新数据写入了它所包含的地址。 For some platforms, it is the job of the programmer to flush the i-cache after modifying the instruction data, so that the i-cache can refetch the updated instructions. 对于某些平台,程序员的工作是在修改指令数据后刷新 i缓存,以便i缓存可以重新获取更新的指令。

In the end though, it's the job of the programmer/compiler to make sure that the PC never jumps to any address that isn't code. 最后,确保PC永远不会跳转到非代码地址是程序员/编译器的工作。

(Of course, malicious users will of course try to get the PC to jump to their bad code, but I'll save stack buffer overflow attacks for another post.) (当然,恶意用户当然会尝试使PC跳到他们的错误代码,但我将为另一个帖子保留堆栈缓冲区溢出攻击。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 冯·诺伊曼建筑,这是否仍然适用 - Von Neuman Architecture, is this still applicable 为什么编译器将数据放在PE和ELF文件的.text(code)部分,CPU如何区分数据和代码? - Why do Compilers put data inside .text(code) section of the PE and ELF files and how does the CPU distinguish between data and code? 进程之间的上下文切换是否会使MMU(内存控制单元)无效? - Does context switch between processes invalidate the MMU(memory control unit)? 指令如何与数据区分开来? - How instructions are differentiated from data? CPU如何重新排序指令 - How does CPU reorder instructions 如何区分CPU和memory瓶颈? - How to distinguish between a CPU and a memory bottleneck? CPU如何分辨指令顺序? - How does CPU tell the sequence of instructions? 当代处理器与纯冯·诺依曼架构有何不同? - How do contemporary processors differ from a pure von Neumann architecture? 机器指令如何知道某些数据的长度和大小? - How do machine instructions know the length and size of certain data? 考虑到指令的长度不同,CPU如何知道下一条指令应读取多少字节? - How does the CPU know how many bytes it should read for the next instruction, considering instructions have different lenghts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM