11

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
manuhg
  • 360
  • 1
  • 4
  • 16

3 Answers3

10

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).

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • thank you. but i could not find how to set the global power states. can i get some help in that? – manuhg Jan 15 '12 at 09:37
  • 6
    For a "tiny OS" implementing an ACPI interface isn't an easy task. If you have a somewhat older computer, which BIOS still implements APM, you may be get around by shutting off via APM ( http://en.wikipedia.org/wiki/Advanced_Power_Management ) , this is the way windows 95 did it. Note that the OS before, like DOS, weren't able to shutdown the computer. – Gunther Piez Jan 15 '12 at 09:53
1

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.

hutheano
  • 172
  • 2
  • 10
  • 2
    Surely the infinite loop should include the `cli / hlt`, in case an NMI arrives. I mean, if you're going to have an infinite loop at all... But in any case, this doesn't answer the question. This just keeps the CPU in low-power idle, not off. – Peter Cordes Apr 07 '16 at 15:29
  • 4
    This doesn't shutdown the computer, it only stops it. – Ross Ridge Apr 07 '16 at 15:29
  • 3
    On top of the very valid and relevant (to the question of halt/shutdown) mentioned by Ross and Peter, I'll point out (in the context of bootloaders in general) that your code makes the false assumption that the BIOS cleared the direction flag before it reached your bootloader. You really should have called `CLD` since you eventually use `lodsb` . – Michael Petch Apr 07 '16 at 16:05
1

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.

alvin
  • 1,176
  • 8
  • 15
  • then please tell me how to call it. All i want to know is how to shutdown my OS and my computer – manuhg Jan 15 '12 at 03:47
  • 3
    @alvin: On x86, every interrupt does bring the processor out of the hlt state. It is not safer in any way to turn of the computer while the processor is in the hlt state, than while it is running, as all disk buffers etc. are not flushed. – Gunther Piez Jan 15 '12 at 09:49
  • @drhirsch, you are right, didnt think of the OS state. edited my answer. – alvin Jan 17 '12 at 16:31