問題描述
如何在不使用 HLT 的情況下對程序進行 HLT (How to HLT the program without using HLT)
如何在沒有 HLT 的情況下停止執行程序寫在我讀過的代碼的末尾,HLT 可以在那裡發生中斷,但我不明白是怎麼回事
參考解法
方法 1:
The HLT
instruction does not actually terminate the program. As far as I can tell, its main use is putting the CPU in "idle" mode to reduce power consumption. But that only lasts until the next interrupt fires, then program execution will continue.
You should probably do a system call (a call into your OS), either to tell the OS to terminate your process, or to "yield" the processor (letting the OS HLT
it for you in an appropriate fashion).
How exactly system calls work and which one you need depends on the OS your program is running on. On DOS, there's e.g. INT 21h
(MOV AH, 4Ch; INT 21h
will terminate your program IIRC), for Linux, look up "syscalls").
If you want to truly halt program execution, i.e. intentionally hang the computer, you can either:
- enter into an infinite loop (
here: JMP here
), or - disable (maskable) interrupts using
CLI
, followed byHLT
.
The second option might be more power efficient, however both are equally non‑user friendly and probably somewhat pointless. :)
(Disclaimer: I haven't been doing system‑level programming in a while, the above information might be a little rough around the edges.)
(by Ali Hussein、stakx ‑ no longer contributing)