About 16-bit OS attempt (1)

zhaozj2021-02-08  247

First of all, I have to declare that I am not an OS expert. About OS research is only for my interest. But I think it is just for myself, and I should write this time yourself in writing OS.

This OS I did for the first time is a 16-in-formal mode. Because it is simpler than the protection mode, it is easy to get started.

First, find a 1.44MB floppy disk. My OS is written to the floppy disk. Of course, you can also write on the hard disk, but you have to have two or more hard drives. Otherwise the hard disk data is destroyed, The computer can't start. Here I have to mention a stupid thing I have done. Because there is no floppy drive on my computer, I only have a USB mobile hard disk, so I took the USB disk instead of the floppy disk. Later I wrote the program In any case, there is a problem. After a step in detail, I found that the original USB disk could not be read with BIOS 13h. Because the USB disk is not a disk, I actually came to take it to read it. I think it is really ridiculous now. .

I first wrote Boot Loader, which is the first sector on the floppy disk. When the computer starts, it will automatically put this program to 0x0000: 0x7c00 (it seems to be like this) to execute. But only one sector, you don't You may put your entire OS in this sector. One sector is only 512 bytes. However, I can pass the data on this sector to run the data on the floppy disk. So the old outside it boot Loader (boot loader).

About this boot loader is very simple. Especially for the 16-bit real-mode I have to do, there is almost no requirement, how do you want to do it. But if you want to protect the protection mode? OS, you need to design a lot of trouble such as "A20 start". Now there is a introduction to OS everywritten now, but most of them stay in this Boot Loader explanation, and most of them are still explaining 16-bit real mode Boot loader. For example, go to www.google.com to search for "Write Your Own OWNG SYSTEM", you can find a lot of such articles. Of course, these major are English. See also good. Most of these websites have spend how to get started, and they will be very practical, and they are all good. For example, I know a website http://osdev.neopages.net/index.php, it is very good.. About OS introduction is not simply staying in Boot Loader. And there are all tools we need to write OS, and information collection.

My favorite teacher is the favorite speech, maybe I have learned his advantages, telling the above-mentioned paragraph, and I will start to really write my boot loader.

Boot loader seems to be written in compilers. The best assembly compiler is NASM. When I start writing Boot Loader, almost 100% said that I should use NASM to make a compiler. Perhaps because NASM is a public source Code, or maybe NASM supports a lot of format generated files, so this thing is coming to assemble the compiler recommended by the masters. This thing you can find in http://sourceforge.net/, even its source code You can also find it. However, http://osdev.neopages.net/index.php is also available. Now I have used NASM, I really feel that it is a good thing. And there is very good about it. It is very convenient to query.

Ok, this is my Boot Loader program.

; ------------------------------------------------- ----------------------; Hello World Operating System Boot Sector00 Program ;; Tangl_99 2003 ;; Disclaimer: i am not responsible for Any Results of the use of the contents T this file; --------------------------------------------- ------------------------- Bits 16 org 0x7c00; this is where bios loads the bootloader% define kernel_sectors 16; 8K size KERNEL

EXECUTION Begins Hereentry: JMP Short Begin; Jump over the dos boot record data

; ------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------- ---------------------------------- ; | Data section of boot.asm bootstrap file |; - -------------------------------------------------- --------- bsOEM DB 'DEVIATOR'; OEM StringbsSectSize DW 512; Bytes per sectorbsClustSize DB 1; Sectors per clusterbsRessect DW 1; # of reserved sectorsbsFatCnt DB 2; # of fat copiesbsRootSize DW 224; size of root directorybsTotalSect DW 2880; total # of sectors if <32 megbsMedia DB 0xF0; Media DescriptorbsFatSize DW 9; Size of each FATbsTrackSect DW 18; sectors per trackbsHeadCnt DW 2; number of read-write headsbsHidenSect DD 0; number of hidden s ectorsbsHugeSect DD 0; if bsTotalSect is 0 this value is; the number of sectorsbsBootDrv DB 0; holds drive that the bs came frombsReserv DB 0; not used for anythingbsBootSign DB 29h; boot signature 29hbsVolID DD 0; Disk volume ID also used for temp; Sector # / # sectors to loadbsvolabel DB 'deviatoros'; Volume Labelbsfstype DB 'FAT12'; File System Type; ------------------------ ---------------------------------------------

; --------------------------------------------; Boot Program Code Begins Here; --------------------------------------------; boot code begins at 0x0040begin: xor ax, ax; zero out ax mov ds, ax; set data segment to base of RAM mov si, welcomeMsg; load address of our welcome message call putstr; print the welcome message mov si, newline call putstr mov si, Loadmsg; Load Address of Loading Message Call Putstr; Print The Loading Message Mov Si, Newline Call Putstr xor AX, AX INT 13H JC Fail

; ------------------------------------------------- ; Read the data of the next sector 01 to 0x500: 0000, then execute; ------------------------------ ----------------- Readsector01: MOV AX, 0x500; first pass the buffer segment of the data stored by the sector 01 to the AX MOV ES, AX; through AX, reboot The address is transmitted to the ES MOV BX, 0; the buffer offset address is 0 MOV DL, 0; the driver number to be read is 0 h, which is a floppy drive MOV DH, 0; the magnetic head number to be read is 0 MOV CH, 0; The track number to be read is 0 MOV CL, 2; the sector number to be read is 2 MOV Al, kernel_sectors; the number to read is kernel_sectors Mov AH, 2; call the interrupt program for the disk INT 13H CMP AH , 0; reading success, AH 0 means reading success jz gotosector01; if successful, go to Gotosector01Fail: Mov Si, ReaderrorMSG; pass the address of ReaderrorMSG to Si, ready to print the Call Putstr; Print read disk errors information MOV Si, Newline Call Putstrhang: MOV SI, LOADSECTOROKMSG But note that CS MOV ES, AX MOV DS, AX JMP 0x0500: 0x0000 before JMP instruction; ------------------------------------------------------------------------------------------------------- ----------------; Data for Our Program; -------------------- ------------------- WelcomeMsg DB 'Welcome To Tangl Operating System', 0LoadMsg DB 'Operating System Boot Program Is loading ... ', 0Loadsectorokmsg DB' Loaing Next Sector OK ', 0ReaderrorMsg DB' Error: Can Not Sector 01! ', 0Newline DB 13, 10, 0; ---------- -----------------------------------; Print a null-terminated string on the screen; ---------------------------------------- Putstr: Lodsb; Al = [DS: Si] OR Al, Al; Set Zero Flag IF Al = 0 JZ PutStrd; Jump To PutStrd IF Zero Flag IS Set MOV AH, 0x0E; Video Function 0EH (Print Char) MOV BX, 0x0007; Color Int 0x10 JMP PutstrputStrd: Retn; -------------------------------------------- Size Equ $ - Entry % IF Size 2> 512% Error "Code Is Too Large for Boot Sector"% Endif Times (512 - size - 2) DB 0

DB 0x55, 0xAA; 2 byte boot signature

Such a long one, you don't feel a lot of things. Simply, I only have a useful thing. It is to read the data of 16 sectors behind the floppy disk, and go to execute. The code starts with a block " Guide Record "Data Definition is to record some of the sectors of this disk, how many heads, how many bytes, each sector, etc. In fact, 1.44MB disk is unchanged, as long as it is 1.44 MB's floppy disk, these data is the same, there is no need to write it. But if I don't write them, then when I am inserted into the floppy drive, Windows or DOS will say that it is not formatted, but I want to re-re- Format, then I wrote that the Boot Loader program I have written above. So I still pay attention to Windows / DOS, write this information as, according to their criteria.

The latter code is mainly to show some prompt information, then a interrupt program called BIOS 13H read disk. By it, read the data of the 16 sectors, read 0x500: 0x0000, finally jump to 0x500: 0x0000 Execute those code. The 16 sectors' code is the code of the Kernel kernel I really OS.

转载请注明原文地址:https://www.9cbs.com/read-3164.html

New Post(0)