处理器如何知道程序结束? [英] How does processors know the end of program?

查看:80
本文介绍了处理器如何知道程序结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,处理器如何知道何时停止执行程序.或者更确切地说,何时停止获取,解码执行"循环.
我想过不同的方法,但不确定哪个是正确的,或者它们是否都是错误的.
1- 可能是汇编器在末尾自动添加了一条特殊指令,让处理器知道这是结束.
2- 当它到达无效内存时(但它是如何识别的).
3- 它循环并重新运行程序,但它又如何识别何时循环或由汇编程序自动添加.
4- 或者它根本不会停止,它将永远运行.(但这不是效率低下吗?

I was wondering, how does processors know when to stop executing a program. Or rather, when to stop the "fetch, decode execute" cycle.
I have thought of different ways but not sure which is the correct one or if they are all wrong.
1- Maybe there is a special instruction at the end automatically added by the assembler to let the processor know this is the end.
2- When it reach an invalid memory (But how does it recognize that).
3- It loops and re-run the program, but again how does it recognize when to loop or maybe it is added by the assembler automatically.
4- Or maybe it doesn't stop at all, it will keep running for ever. (But isn't that power inefficient?

请指教.
如果答案可能因处理器而异,让我们以 MIPS 和 x86 为例.

Please advice.
If the answer may vary from processor to processor, lets take MIPS and x86 as an example.

谢谢.

推荐答案

这取决于操作系统和处理器.

This depends on the operating system and the processor.

对于有halt指令的处理器(如X86),当所有进程和线程都处于等待状态(无事可做)时,操作系统可能会执行halt指令,该指令会暂停cpu并等待中断.如果下一个中断没有导致任何线程的状态切换到可运行状态,那么操作系统将返回到暂停指令.

For a processor with a halt instruction (like X86) when all processes and threads are in a wait state (nothing to do), the operating system may execute a halt instruction, which will halt the cpu and wait for an interrupt. If the next interrupt doesn't result in switching the state of any thread to runnable, then the operating system will go back to the halt instruction.

在没有暂停指令的 cpu 上,当所有线程都在等待时,操作系统进入一个空闲循环,该循环只分支到自己.

On a cpu without a halt instruction, when all threads are waiting, the operating system goes into an idle loop that just branches to itself.

回到最初的问题,运行程序的基本顺序是为程序进行内存空间的初始分配,加载程序,然后调用程序.程序最终应该从回调中返回到操作系统,操作系统将释放分配给运行程序的内存.操作系统也可能在初始分配阶段创建虚拟地址空间.

Getting back to the original question, the basic sequence for running a program is to do an initial allocation of memory space for a program, load a program, then call the program. The program should eventually return from the call back to the operating system, and the operating system will release the memory that was allocated to run the program. An operating system may also create a virtual address space during the initial allocation phase.

维基加载器 , wiki 程序执行.在MSDOS的情况下,程序使用INT软件序列返回MSDOS,例如设置AH = 04Ch然后执行INT 21H.对于其他操作系统,加载程序调用程序,程序完成后返回.

wiki loader , wiki program execution. In the case of MSDOS, a program used an INT software sequence to return back to MSDOS, such as setting AH = 04Ch and then doing an INT 21H. For other operating systems, the loader does a call to the program, which returns when it's complete.

这篇关于处理器如何知道程序结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆