Write your own operating system: write the smallest operating system in the world

Write the smallest operating system in the world

Before writing the first operating system, we need to prepare hardware and software

Hardware preparation
· A computer (windows operating system)
· A blank floppy disk (or use CD-ROM instead)
Software preparation
· Assembler compiler NASM. You can download it at this link: download link of the assembly compiler .
· Floppy disk absolute sector read and write tool

Next, we start to write a minimal operating system, and you don’t believe it, this operating system only requires 20 lines of code.
code show as below:

       org 07c00h                            ;告诉编译器程序加载到7c00处
       mov ax, cs
       mov ds, ax
       mov es,ax
       call        DispStr                   ;调用显示字符串例程
       jmp $                                 ;无限循环
DispStr:
       mov ax, BootMessage
       mov bp,ax                             ;es:bp=串地址
       mov cx, 16                            ;cx=串长度
       mov ax, 01301h                        ;ah=13,al=01h
       mov bx, 000ch                         ;页号为0(bh=0)黑底红字(bl=0ch,高亮)
       mov dl, 0
       int 10h                               ;10h  号中断
       ret
BootMessage:      db  "Hello,OS world!"
times   510-($-$$)  db  0                    ;填充剩下的空间,使生成的二进制代码恰好为512字节
dw       0xaa55                              ;结束标志

Compile the above code with NASM and you will get:
nasm boot.asm -o boot.bin
So we get a 512B boot.bin, use the floppy disk absolute sector read and write tool to write this file to a blank floppy disk The first sector. Now the first "operating system" is complete, and this floppy disk becomes a boot disk.
Put the floppy disk into the floppy drive and restart the computer. Start booting from the floppy disk, and you will see the computer display your string in red: "Hello, OS world!"

But in fact, this is not a complete OS, just a simple boot sector, which is Boot Sector. Nevertheless, it at least runs directly on the bare metal and does not depend on any other software. It is not an operating system, but it already has a feature of the operating system.

Continue to update, next time the computer power-on inspection and code explanation

Guess you like

Origin blog.csdn.net/apple_52246384/article/details/110743554