引导加载程序堆栈设置

Bootloader stack set up

我目前正在尝试理解某段代码。我在这里找到:
http://mikeos.sourceforge.net/write-your-own-os.html

特别是开始标签下的前两行:

    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


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

    jmp $           ; Jump here - infinite loop!


    text_string db 'This is my cool new OS!', 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

教程说 "those lines aren't really of interest for us",但我真的很想知道。那么什么是“07C0h”呢?起初我以为它是 BIOS 加载引导加载程序的地址,但从我读到的地址是“7C00h”。 “(4096 + 512) / 每段 16 个字节”是什么意思?我也想知道"$$"是什么(我知道"$"是什么)

对,就是地址,用段表示。在实模式下 physical address = 16 * segment + offset 所以物理地址 7c00h 可以写成 7c0h:0 (这不是唯一的方法)。每个段落都是 16 个字节,将段更改 1 会将物理地址更改该数量。

要获得 4096 字节的堆栈,加载地址将递增 4096 字节加上引导扇区的大小(512 字节),然后将整个内容除以 16 以获得段值。

$$是一个特殊符号,表示当前节的开始。见 nasm manual.

PS:确实没有必要在运行时这样做,代码可以简单地使用 mov ax, 07C0h + 288 或类似的。