Stack segment in the MikeOS bootloader

前端 未结 1 1305
夕颜
夕颜 2021-02-14 23:51

I don\'t understand this piece of code:

mov ax, 07C0h   ; Set up 4K of stack space above buffer
add ax, 544     ; 8k buffer = 512 paragraphs + 32 paragraphs (lo         


        
1条回答
  •  深忆病人
    2021-02-15 00:18

    I think the comment on the last line sums it up:

    buffer:             ; Disk buffer begins (8k after this, stack starts)
    

    The memory layout looks like this:

    +-------------------+ <-- 07C0:0000, where the BIOS loads the boot sector
    | 512 bytes of code |
    +-------------------+
    | 8KB set aside for |
    |   a disk buffer   |
    +-------------------+ <-- SS:0000
    |   4KB of stack    |
    +-------------------+ <-- SS:1000 = SS:SP
    

    The comment about paragraphs is slightly obtuse; I find it easier to think in bytes, where 16 bytes makes one paragraph.

    The reason for these magic numbers:

    • Start at segment 07C0, where the BIOS loads the code
    • Skip past 512 bytes, to account for the code itself (512 bytes = 32 paragraphs)
    • Skip past 8KB, to set aside space for the disk buffer (8,192 bytes = 512 paragraphs)
    • Put SS at the start of a 4KB block. 512+8192 = 8,704 bytes = 544 paragraphs
    • Put SP at the end of that block. Put it at the end because the stack needs to grow upwards in memory.

    Note that the number 4096 = 4KB appears as normal in the code, because the SP register needs a value in bytes. All the other values are in paragraphs because they relate to SS, which is a segment register.

    0 讨论(0)
提交回复
热议问题