11

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

可以hlt关闭机器吗?

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

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

4

3 回答 3

10

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

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

于 2012-01-15T04:18:44.277 回答
1

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

于 2012-01-15T03:45:25.337 回答
1

通过使用这两行代码:

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

于 2016-04-07T15:11:57.350 回答