How does processors know the end of program?

血红的双手。 提交于 2021-02-07 19:54:53

问题


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?

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

Thanks.


回答1:


This depends on the operating system and the processor.

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.

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.

Further reading

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.




回答2:


With some parallels to previous responses, I'll describe more detailedly. Two main contexts shall be distinguished: 1) a code with supervisor rights directly on the running hardware (this includes OS) and 2) an application code.

For the second case - an application code - an application shall do a system call usually named "exit" which notifies supervisor (kernel) that the application is finished. Details of this call vary; e.g. MS-DOS AH=4Ch/int21h as described by rcgldr is such "exit"; in modern Linux it's exit_group syscall; and so on. When seeing only main() you might miss the fact that main() is called by a system-specific application wrapper, which do this exit() call. If application doesn't exit explicitly, it will execute until a fatal error occured. Getting this exit() request, supervisor destructs the process and switches to another one.

For the supervisor itself, if it stops execution, this is very platform specific. For x86, initially the main method for shutdown without poweroff was a cycle around HLT instruction (because the latter can spontaneously continue on interrupts). The same HLT was used until approx. Pentium4 and Athlon to stop processor until an interrupt when there is no work; since these processors, additional measures became needed to stop chipset-level activity, so the stopping method became more complex. Modern x86 platforms describe how to make temporary system stop according to description on ACPI internal language, which includes multiple system port programming. Similar instructions for stopping until external event coming are present for all real architectures. There can be multiple levels of stopping; e.g. some embedded ones have "minor stop" which only slows processor down and "major stop" which stops processor fully but then needs tens of microseconds for clock PLL settling out. Anyway, after such stop, supervisor tries to continue normal work, until reboot/poweroff/etc. is being reached.

Multi-processor (multi-core) configurations have their own specifics so initially only one core at one processor is started. Then, inter-processor interrupts (IPIs) or analogs are used to start or stop another cores and processors. Stopping OS work usually includes stopping of all processors and cores except a final one.




回答3:


To end your in 8086 or MIPS you can use system calls in 8086 : hltorint 20h in MIPS: the code for hlt is 10 you need to put it in $v0 or $2 (same register ) li $v0, 10 then call the system syscall



来源:https://stackoverflow.com/questions/25336465/how-does-processors-know-the-end-of-program

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