简体   繁体   中英

Assembly x86 Bootloader

So I found the following tutorial for building a simple bootloader: http://mikeos.sourceforge.net/write-your-own-os.html#firstos

Here is the start of his example and the area that I'm having trouble understanding:

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

I understand that Mike here is trying to build a stack for his bootloader. The start of the program is at 07C0h in memory. What I don't understand is the following line 'add ax, 288 ; (4096 + 512) / 16 bytes per paragraph'. Why is he taking the total amount of the stack and boot sector, then dividing it by the 16-bit registers for the start of the stack segment? Shouldn't the stack segment start at 20h, right after the bootsector? Lastly, shouldn't the stack pointer then be set at the end of (512 + 4096)? Thanks

You are right. With the current implementation, the code will leave a gap of 4k. The way to resolve this is what you said, which is to set ss to end of bootsector / 16 and to load sp with 4k (not 4k + 512). Hence the correct implementation would be.

mov ax, 07c0h
add ax, 32     ; (512 / 16) i.e end of bootsector
mov ss, ax
mov sp, 4096

Aside, from the question, i would also like to mention that this type of bootloaders won't work on real machines anymore and you should focus on standards like UEFI to build a bootloader. It also takes care of other tasks such as switching the cpu modes.

If you wish to see a working UEFI based bootloader with a minimalistic kernel refer to my github repo https://github.com/RohitRTdev/RTOS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM