【Operating System】Write a micro operating system in half an hour - write a boot sector and import it into a floppy disk image

1. What is a boot sector

        When we use a floppy disk to start the operating system, the system first reads data from the first sector of the floppy disk, that is, the 0th side, the 0th sector of track 0, and each sector of the floppy disk is 512 The size of the byte, if the last two bytes are 0xaa55 (when the BIOS sees these two bytes, it is considered to be the end of the boot sector), it means that the machine instructions before the two bytes are start Sector instructions, starting with 0x00 and continuing to 0xaa55, the 512-byte sector is called the boot sector .

2. Programming of the boot sector

        The code (the program name is boot.S ) and comments are as follows:

.code16
.text
        mov     %cs,%ax
        mov     %ax,%ds  
        mov     %ax,%es  #将寄存器ds和es同时指向与cs相同的段,使得后续在进行数据操作时能定位到正确的位置
        call    DispStr  #调用DispStr函数
        jmp     .  #进入死循环
DispStr:  
        mov     $BootMessage,%ax
        mov     %ax,%bp
        mov     $16,%cx
        mov     $0x1301,%ax
        mov     $0x00c,%bx
        mov     $0,%dl
        int     $0x10
        ret
BootMessage:.ascii  "Hello, OS world!"
.org    510
.word   0xaa55

        The annotation of the DispStr function is as follows:

DispStr:  
        mov     $BootMessage,%ax #将字符串变量“BootMessage”的首地址传给寄存器ax
        mov     %ax,%bp #使用寄存器ES:BP保存字符串的地址
        mov     $16,%cx  #保存字符串的长度
        mov     $0x1301,%ax #寄存器ax高位为 0x13h,低位为0x01h
        mov     $0x00c,%bx #设置页号为0(高位0X0h),字符串颜色为红色高亮(低位0x0ch)
        mov     $0,%dl
        int     $0x10 #10h号中断
        ret

         The rest of the code is commented as follows:

BootMessage:.ascii  "Hello, OS world!"  #字符串函数
.org    510  #用0x00填满剩下的字节,一直到第510个字节
.word   0xaa55  #剩下的两个字节填0xaa55

3. Writing the connection script

        To compile a program into an executable file, we need to go through steps such as intermediate code generation and connection (you can refer to an article written before: [c language] compilation process from high-level language to executable EXE program ) , so, before compiling, we also need to write a connection script (named solrex_X86.ld), the code is as follows:

SECTIONS
{
. = 0x7c00;
.text :
{
  _ftext = .;
} = 0
}

        When the BIOS finds the boot partition (the first 512-byte sector ending with 0xaa55), it will transfer the contents of these 512 bytes to 0000:7c00 of the memory (you can refer to an article written before : [Operating System] What did the startup of the operating system do ), and then jump to 0000:7c00 to completely hand over the control to this boot code, so the function of the connection script is to connect the program The entry is set to the location of memory 0000:7c00, and if we want to place the code in another address space, we can directly modify the address of the connection script.

4. Makefile script writing

        In the next step, we write the Makefile script to compile the high-level language (.S) into an intermediate file (.o), and then link and compile the connection script (.ld) and the intermediate file (.o) into an executable file (.elf) , and finally write the executable file to the floppy disk image (.img), the code is as follows:

CC=gcc
LD=ld
LDFILE=solrex_X86.ld  #连接脚本
OBJCOPY=objcopy

all: boot.img

boot.o: boot.S  #生成中间文件
        $(CC) -c boot.S

boot.elf: boot.o  #连接脚本+中间文件=可执行文件
        $(LD) boot.o -o boot.elf -e c -T$(LDFILE)

boot.bin: boot.elf  #移除可执行文件中没有用到的块
        @$(OBJCOPY) -R .pdr -R .comment -R.note -S -O binary boot.elf boot.bin

boot.img: boot.bin   #生成软盘镜像
        @dd if=boot.bin of=boot.img bs=512 count=1
        @dd if=/dev/zero of=boot.img skip=1 seek=1 bs=512 count=2879

clean:
        @rm -rf boot.o boot.elf boot.bin boot.img

        Use the command:

make

        Compile and the output is as follows:

         Finally, a floppy disk image (boot.img) and some intermediate files are generated:

 5. Virtual machine VXBox starts mirroring

        We create a new virtual computer on the virtual machine, and then click the next step as recommended:

       After the creation is complete, we select the "Settings" item of the virtual computer and add a virtual floppy disk:

         Click "Add Virtual Floppy Drive", then click "Register" to register your previously compiled boot.img:

         After the registration is complete, select the corresponding floppy disk image to save and exit:

        In order to allow the system to import our floppy disk image first, we can delete the controller: IDE entirely, so that our image has the highest priority:

        Finally, we click "Start" directly, and we can see the red characters on the black background of the previously written strings! !

        Congratulations! You have taken your first steps in writing an operating system!

Supongo que te gusta

Origin blog.csdn.net/qq_41884002/article/details/130005878
Recomendado
Clasificación