引导程序之输出hello world字符串

2021-01-05  本文已影响0人  louyang
$ cat a.asm

org 0x7C00                         ;Since BIOS loads the first sector (512 Bytes) of disk into memory at address 0x07c00,
                                    ;we use this bin format specific directive, ORG, to let NASM know the program will 
                                    ;be loaded at 0x07c00.

    mov si, .hello_string           ;Store string pointer to register si 
    call print_string               ;Call print string procedure
    jmp $                           ;Infinite loop, hang it here.

print_char:                         ;Function to print character on screen
                                    ;Assume that ASCII value is in register AL
    mov ah, 0x0e                    ;0x0e is the code of TTY character output in BIOS int 10H 
    mov bh, 0x00                    ;Page no.
    mov bl, 0x07                    ;Foreground color (light gray), https://en.wikipedia.org/wiki/BIOS_color_attributes
    int 0x10                        ;Call video interrupt
    ret                             ;Return to calling procedure

print_string:                       ;Function to print string on screen
                                    ;Assume that string starting pointer is in register si
    .next_character:                ;Lable to fetch next character from string
        mov al, [si]                ;Get a byte from string and store in AL register
        inc si                      ;Increment SI pointer
        or al, al                   ;Check if value in AL is zero (end of string)
        jz .exit_function           ;If end then return
        call print_char             ;Else print the character which is in AL register
        jmp .next_character         ;Fetch next character from string
    .exit_function:                 ;End label
        ret                         ;Return from procedure

.hello_string db 'Hello World',0    ;HelloWorld string ending with 0

times 510-($-$$) db 0               ;Fill the rest of sector with 0
dw 0xaa55                           ;Add boot signature at the end of bootloader
$ nasm -f bin a.asm && qemu-system-x86_64 a
image.png

参考

https://stackoverflow.com/questions/2065370/how-to-load-second-stage-boot-loader-from-first-stage
https://www.viralpatel.net/taj/tutorial/hello_world_bootloader.php

上一篇 下一篇

猜你喜欢

热点阅读