如何关闭机器?我正在构建自己的小型操作系统

发布于 2024-12-27 02:35:26 字数 251 浏览 5 评论 0原文

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

能不能关掉机器?

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

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

Can the hlt instruction in assembly shutdown a computer it as it halts the processor? if it can be done using what i have told, is it the right way?

Can hlt shutdown the machine?

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

If this is not the way to be done of if this will not shutdown the system please tell me the right way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末蓝 2025-01-03 02:35:26

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

要关闭现代计算机的电源,请使用ACPI(高级配置和电源接口)

The hlt instruction stops the x86 until an interrupt occurs. Unless all interrupts are disabled, that will stop the processor for only a millisecond or so.

To power down a modern computer, use the ACPI (Advanced Configuration and Power Interface).

虐人心 2025-01-03 02:35:26

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

halt instruction does not turn off the power.
it puts the processor into a non-executing state.
usually, you can come out of the halt state upon processor reset.
in some microcontrollers, specific interrupts can also bring the processor out of the halt state.
power off is a motherboard/bios specific operation.

感受沵的脚步 2025-01-03 02:35:26

通过使用这两行代码:

    cli                     ; stop all interrupts
    hlt                     ; halt the cpu

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

    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 是从左到右加载的。

By using these two lines of code:

    cli                     ; stop all interrupts
    hlt                     ; halt the cpu

you could halt a bootable program for x86 pc:

    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

Save it as "prog.asm", then use "nasm" to create boot sector:

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

Now you could use "qemu" to test it:

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

Note: Removing those two lines mentioned above, causes your virtual machine to use maximum cpu cycles available.

Edit: Added "cld" instruction. As mentioned by Michael, it was necessary to make sure text_string is loaded from left-to-right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文