Bootloader堆栈设置

时间:2016-05-21 12:16:40

标签: assembly x86 stack bootloader real-mode

我目前正在尝试了解某段代码。我在这里找到了:
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

教程说“这些线条并不是我们真正感兴趣的”,但我真的很想知道。什么是“07C0h”?起初我以为它是BIOS加载引导加载程序的地址,但从我读到的地址是“7C00h”。什么是“每段(4096 + 512)/ 16字节”应该是什么意思?我也想知道“$$”是什么(我知道“$”是什么)。

1 个答案:

答案 0 :(得分:2)

是的,这是地址,表示为细分。在实模式physical address = 16 * segment + offset中,物理地址7c00h可以写为7c0h:0(这不是唯一的方式)。每个段落为16个字节,并且将段更改为1会将物理地址更改为该值。

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

$$是表示当前部分开头的特殊符号。请参阅nasm manual

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