At the end of the last article, I put a large section of my boot loader, it might confused. Here I wrote a small miniature version of 16 Boot Loader, let's take a look. Here I will explain again, this article is just a record of my own 16 OS, it is never a standard tutorial. But I will not talk to the boot loader like other articles. I will do it later. KERNEL and a lot of file systems
BITS 16org 0x7C00
Entry: MOV AX, 0 MOV DS, AX MOV SI, WelcomeMsg Call Putstrhang: JMP HAN
Welcomemsg DB 'Welcome To My Operating System', 0
Putstr: Lodsb or Al, Al Jz Putstrd Mov Ah, 0x0E MOV BX, 0x0007 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
The display is a true assembly code, which is displayed as blue is a macro or some control statement of NASM. The first is BITS 16, this note is to compile us into 16-bit code, org 0x7c00 Then tell the compiler Our code will start from the 0x7C00 address (Since the assembly compiler is used to use absolute addresses for the query of the quiet variable, so be sure to set the address starting with the start of your program. Such as DOS. The COM file is to set the org 0x100 is also a truth. Otherwise, your data address will have an error)
Entry: MOV AX, 0 MOV DS, AX MOV SI, Welcomemsg Call Putstr Program Entry starts to enter the execution. The following assembly code does not need me to explain it. First, pass the 0x0000 to the DS. Because our program is 0x0000 : 0x7C00 execution, the segment address should be 0x0000, otherwise we will not display it later. Then call a subroutine PutStr. Its function is to display a string. The parameter is placed in the Si register, I put WelcomemSg The address is transmitted to Si, then calls Call Putstr, you can display Welcome to my operating system. See here, many people will be excited. After all, I saw my "operating system" to print out for myself, really let People are happy! But there is no finished, this boot loader program does not even count the real boot loader program, how can it be said to be an operating system.
Putstr: Lodsb or Al, Al Jz PutStrd Mov AH, 0x0E MOV BX, 0x0007 INT 0x10 JMP PutstrputStrd: Retn This subroutine is not talking, it is very simple, that is, calling the BIOS 10h to display the interrupt of the character. Need to pay attention, don't Use 21h DOS interrupt to display characters! Because that is DOS, here is starting another "operating system", how to use DOS interrupt, is it?
Size Equ $ - ENTRY% IF SIZE 2> 512% Error "Code Is Too Large for Boot Sector"% Endif Times (512 - Size - 2) DB 0
This blue code is not our program, just to control our program size of 512 bytes. Because this program is placed in the first sector of the floppy, it is the first for the floppy disk. The sector is tailored, so the size must be 512 bytes, which is also the size of a sector.
DB 0x55, 0xAA
It is a bit strange here. Why do you want to add 0x55,0xaa in the program? This is also something specified in computer BIOS. The front is not said, BIOS will automatically read the first sector of the floppy and execute it? When BIOS read here When 0x55,0xaa, it knows that the information of the sector read is over, then automatically performs the code just read. So we have to add this 0x55, 0xaa. Ok, this miniature boot loader The introduction is completed. But this boot loader program function is not complete. Because Boot Loader Program is the most important feature to bring OS's kernel to run. We can't always put OS to boot loader program, boot loader program Maximum only 512, how to write?
The method that KERNEL outgoing should be very simple. Especially the floppy disk we choose as our OS storage medium, then read data is easy to add a pleasant thing. There is a 13h disk management interrupt program in BIOS. We offer a lot. Here I no longer introduce this interrupt, you can refer to the books of assembly language, and you should give a detailed introduction.
My OS is very small, and it is 16-bit real mode, so the kernel (kernel) is also very small. I stipulate 8K, just 16 sectors of the zone size. 1.44MB floppy disk 1 track (cylinder) a total of 18 fans District. My kernel plus my Boot Loader a total of 17 sectors of the sector. Good! Just enabled by a track on a disk.
Yes, I have forgot how to write our boot loader program to disk. Many methods, especially for floppy disk. You go to find a registered WinHex software, which supports reading and writing to disk sectors, And it is very convenient. Another way is to download a partcopy program in front of me, but I have never used this thing. Because my WinHex function is very comprehensive and use is very intuitive, with a clip The board can copy the data on the file to any sector on the disk. You can also write the disk for the DEBUG tool under the old DOS.
For example, your Boot Loader Program name is Boot.ASM. Then use NASM to compile
Nasmw boot.asm -o boot.bin
Debug-n boot.bin-l 0-w 0 0 0 1-Q
In your DOS, you can build Boot.asm and write to disk. Although it looks very convenient to use Debug, I think it is still WinHex software.
The front is not saying that this boot loader is not full? Now we add it. For the 16 real mode O very simple, we interrupt the data of 16 sectors behind the disk with the BIOS 13H. As for what location to read in memory is determined by you. Some people like to read 0x800: 0x0000, my OS is to read 0x500: 0x0000. Maybe you read kernel to other places will appear unpredictable errors So you have to read 0x500: 0x0000. My OS's kernel is running there, and it is running normal.
The Boot Loader in the previous article has been given to read the Kernel code. You can also take a look at << Try (1) >> about 16-bit OS.
The code given here is only called 13h read Kernel code.
MOV AX, 0x500; first pass the buffer segment address of the sector 01 to the AX MOV ES, AX; passed through the AX, re-burst segment address to ES MOV BX, 0; buffer offset address is 0 MOV DL, 0 The drive number to be read is 0h, 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 For 2 MOV Al, 16; the number of sectors to be read is 16, because my kernel has a total of 16 sectors of the large MOV AH, 2; the interrupt program for calling the disk is OK. After reading KERNEL to 0x500: 0x0000 Once, you should do kernel. This is easy to implement in assembly language. Is it not OK with a JMP instruction? But in addition to using JMP, pay attention to some details
MOV AX, 0x500; Jump command to 0x500: 0000, and turn the ES, DS to 0x500, but note that CSMOV ES, Axmov DS, AxJMP 0x0500: 0x0000 is Boot Loader before JMP instruction Go to Kernel code. It can be seen that in addition to JMP 0x0500: 0x0000, we also need to set the ES, DS and other registers. The green comment on the above records a small mistake I have made. At the time, I did except for Es. DS is set to 0x500, but also the CS is 0x500. CS is the address of my current code, when we use JMP 0x500: 0x0000, the CS will automatically turn 0x500. So we must not change before JMP Once the CS is changed, then the code executed by the computer will jump to the 0x500 segment address to execute. These words are spoken, don't know if I said clear. Our computer's CPU execution code is cs: IP The order is executed, each execution of a command, IP moves to the next. So once the CS or IP has changed, then our execution order has changed. So we generally can't touch CS and IP.
Ok, when your Boot Loader is successful, write it to disk, put it into the computer, restart your computer, and start with a floppy disk, you can see your OS's Boot Loader running.
If we have to restart your computer every time we can see our OS operation results, this will be very troublesome. Fortunately, foreigners write a lot of high simulators, you can simulate the computer to start our OS scenarios.
Bochs-2.0 is a good 80x86 simulator. Similarly, you can download this software at http://osdev.neopages.net/index.php. It has a lot of features, in addition to the simulated floppy start calculation, Simulate the hard disk and CD boot computer. I didn't know how to use it when I got it. It needs to set some parameters. I will now talk about it directly.
After first decompressing, enter Bochs-2.0, there is already a DLXLinux example. I will use it to modify it. Open Start.bat, join
SET BXSHARE = f: /bochs-2.0 ../ Bochs
I don't know how, its environment directory BXShare is not set, so we need to set up in Start.bat, my bochs-2.0 is installed in F: /Bochs-2.0, so set to set bxshare = f: / bochs -2.0
Then open BochStr.txt, find
# Choose the boot disk.boot: a Modify Boot to a:, so bochs-2.0 will simulate from A: start
# What disk images Will be used floppya: 1_44 = floppya.img, status = INSERTEDFLOPPYB: 1_44 = floppyb.img, status = inflicpya: 1_44 = floppya.img illustrates our A disk view file is Floppya.img
Here is a problem with how to create a FLOPPYA.IMG scene. The scene file is very simple. It is a complete 1.44MB file. You copy the data of each sector on the floppy disk to this file. Go it.
There is a program bxImage.exe in Boch to help you create a floppya.img scene file and copy your boot loader Boot.bin to floppya.img head 512 bytes. You can use WinHex to copy It is very convenient to copy directly with the clipboard. So I have always said that Winhex is really a good thing!
Ok, all work is done. Run start.bat, there is a choice, don't worry it, our parameter settings are set, "directly" simulation "is OK. Turn it to Boch pop-up window, that It is the picture that we boch simulates the OS.
So tired, talk so much. Even kernel has not started yet. In the next section, we will start entering the kernel part!