【问题标题】:How to shutdown the machine? I'm building a tiny OS of my own [duplicate]如何关闭机器?我正在构建自己的微型操作系统 [重复]
【发布时间】:2012-02-10 15:32:39
【问题描述】:

汇编中的 hlt 指令可以在停止处理器时关闭计算机吗?如果可以使用我所说的来完成,这是正确的方法吗?

可以关闭机器吗?

start:
    xor ax, ax; ;clear ax
    mov bx, ax; ;clear bx
    cli ;stop all interrupts
    hlt ;halt the cpu

如果这不是方法,如果这不会关闭系统,请告诉我正确的方法。

【问题讨论】:

  • 什么平台?请标记您的问题...
  • 为什么最近发布代码的图片变得如此流行?这比复制和粘贴更容易吗?你肯定知道如何复制和粘贴...

标签: assembly operating-system x86 shutdown system-shutdown


【解决方案1】:

hlt 指令停止 x86,直到发生中断。除非所有中断都被禁用,否则处理器只会停止一毫秒左右。

要关闭现代计算机,请使用ACPI (Advanced Configuration and Power Interface)

【讨论】:

  • 谢谢。但我找不到如何设置全局电源状态。我能得到一些帮助吗?
  • 对于一个“微型操作系统”来说,实现 ACPI 接口并不是一件容易的事。如果您有一台较旧的计算机,其 BIOS 仍然实现 APM,您可能会通过 APM (en.wikipedia.org/wiki/Advanced_Power_Management) 关闭,这就是 windows 95 的方式。请注意,之前的操作系统(如 DOS)无法关闭计算机。
【解决方案2】:

halt 指令不会关闭电源。 它将处理器置于非执行状态。
通常,您可以在处理器复位后退出暂停状态。
在某些微控制器中,特定的中断也可以使处理器脱离暂停状态。 关机是主板/BIOS 特定的操作。

【讨论】:

  • 那么请告诉我怎么称呼它。我只想知道如何关闭我的操作系统和我的电脑
  • @alvin:在 x86 上,每个中断都会使处理器脱离 hlt 状态。在处理器处于 hlt 状态时关闭计算机并不比在运行时更安全,因为所有磁盘缓冲区等都不会被刷新。
  • @drhirsch,你是对的,没有想到操作系统状态。编辑了我的答案。
【解决方案3】:

通过使用这两行代码:

    cli                     ; stop all interrupts
    hlt                     ; halt the cpu

您可以停止 x86 电脑的可启动程序:

    BITS 16

start:
    mov ax, 07C0h           ; Set up 4K stack space after this bootloader
    add ax, 288             ; (4096 + 512) / 16 bytes per paragraph
    mov ss, ax
    mov sp, 4096

    mov ax, 07C0h           ; Set data segment to where we're loaded
    mov ds, ax


    cld                     ; clear direction flag
    mov si, text_string     ; Put string position into SI
    call print_string       ; Call our string-printing routine


    cli                     ; stop all interrupts
    hlt                     ; halt the cpu

    jmp $                   ; Jump here - infinite loop!


    text_string db 'Hello World!', 0


print_string:               ; Routine: output string in SI to screen
    mov ah, 0Eh             ; int 10h 'print char' function

.repeat:
    lodsb                   ; Get character from string
    cmp al, 0
        je .done            ; If char is zero, end of string
    int 10h                 ; Otherwise, print it
    jmp .repeat

.done:
    ret


    times 510-($-$$) db 0   ; Pad remainder of boot sector with 0s
    dw 0xAA55               ; The standard PC boot signature

将其保存为“prog.asm”,然后使用“nasm”创建引导扇区:

nasm -f bin -o boot.img prog.asm

现在你可以使用“qemu”来测试它了:

qemu-system-i386 -drive file=boot.img,index=0,media=disk,format=raw -boot c -net none

注意:删除上面提到的这两行会导致您的虚拟机使用最大的可用 CPU 周期。

编辑:添加了“cld”指令。正如 Michael 所提到的,有必要确保 text_string 从左到右加载。

【讨论】:

  • 当然无限循环应该包括cli / hlt,以防NMI到达。我的意思是,如果您要进行无限循环...但是无论如何,这并不能回答问题。这只是让 CPU 处于低功耗空闲状态,而不是关闭。
  • 这不会关闭计算机,只会停止它。
  • 除了罗斯和彼得提到的非常有效和相关的(关于停止/关闭的问题)之外,我会指出(在一般引导加载程序的上下文中)您的代码使错误假设 BIOS 在到达引导加载程序之前清除了方向标志。你真的应该打电话给CLD,因为你最终会使用lodsb
猜你喜欢
  • 2012-02-07
  • 2010-11-16
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多