Build your own operating system [1]-boot program

Source: Internet
Author: User

I have been reading the Linux 0.11 kernel over the past few days. I am also reading the code of some small operating systems in China, such as learnos, pretty, and Iris .... To combine some of their advantages, first make a simple kernel.

I have always been thinking about operating systems. I used to stay on a superficial level and learned a lot of theoretical knowledge. I still don't know anything about the underlying layer. There will be a <operating system> course next semester, and there will be no detailed explanation in the course, and the labs arranged in the outline will not be too deep. However, I can finally start my own experiment with some time :-)

Today, we officially started hands-on practice and wrote an experimental pilot program.
The following is an example from <Xie Yunbo's Experiment Report>
[Quote]
First, I want to explain what the computer did after you press the power button.
When you press the power key, the wire associated with this key will send an electrical signal to the motherboard, the motherboard will send this electrical signal to the power supply system, the power supply system starts to work, power supply for the entire system, and sends an electrical signal to the BIOS to inform the BIOS that the power supply system is ready. Then the BIOS starts a program to perform host self-check. The main task of host self-check is to ensure that each part of the system receives power support, internal memory, other chips on the motherboard, keyboard, and mouse, the disk controller and some I/O ports are available normally. After that, the self-check program returns the control to the BIOS.
 
Then, the BIOS starts the operating system.
The BIOS accesses the first sector of the boot disk (0 track, 1 sector, a total of 512 bytes). The BIOS transfers the content in the first sector to the 0x7c00 address of the memory and starts executing it. This is the first step to start the system. From then on, the system will give control to the operating system, and the tasks left behind will be done by your program.
 
Now our task is to write such a program. The system calls it a boot program and uses it to guide or start our computer. It has the following two features:
1. The size can only be 512 bytes, not one byte or one byte. Because the BIOS can only read 512 bytes of data to the memory, most of the BIOS will not
2. It must end with "55 AA", that is, the last two bytes (511,512) must be them. This is a sign of the end of the boot area program, without which the BIOS will not treat it as a boot program. (I didn't execute the previous program because I didn't write "55 AA" here ")
Place this program in the zero track and one sector of the disk. In this way, the disk can be used to guide the system and use your own boot program!
 
Before starting to create a boot program, we will first introduce how to perform such development in a Windows environment.
 
First, you need an experimental environment. Of course, you can use real computers. If you have multiple computers, you don't have to worry about it.
Here we use a virtual machine to conduct an experiment. It is the same as using a real computer. If you don't believe it, you can experiment on it yourself.
I am using a MS Virtual PC, it is very simple to use, here not to mention, it can use a 1.44m size imgfile, as a simulated floppy disk, therefore, we just need to write our boot program to an imgfile, just like writing it on a disk, we can use it to guide the system.
After starting the VM, select the Floopy disk image option under the Floopy menu, and then select the generated imgfile.
 
The following describes how to create and generate an imgfile.
To write the boot program to this 1.44m file, I use the winhex tool, which is very convenient. You can directly write binary files by copying them, you can also create a file of the specified size.
You can use winhex to create an imgfile of 1.44m. Click the new button. The size is 1474560 in bytes.
Then, open the program we wrote with winhex and copy its content to the generated imgfile.
[/Quote]
I learned a lot about the startup process. His experiment environment is in windows. Next I will talk about how to set up the experiment environment in Linux.

1. Download and install a virtual machine. I use virtualbox.
For more information about how to use virtualbox, see the documentation. After our IMG has been created, we can use the floppy VM to start the boot guide.
3. Download NASM Assembly compilation and linker

After the experiment environment is set up, you can start the first experiment, the simplest guiding program.
The Code is as follows:

[Bits 16]; generate a 16-bit program
[Org 0x7c00]; run the command starting from 0x7c00.

XOR ax, ax
MoV ds, ax
MoV es, ax
Call showmessage
JMP $; $ represents the address of this statement, indicating that this statement carries out an infinite loop

Showmessage:
MoV ax, bootmsg
MoV bp, ax; es: BP = string address
MoV CX, 28; Cx = words
MoV ax, 0x1301; set the Display Mode
MoV BX, 0x000c; bH = page number, BL = color attribute
MoV DL, 0; start position
Int 0x10
RET

Bootmsg: DB "Hello, world! I'm l0y0l ^! "; Define the prompt message. l0y0l is the name of my operating system :-)
Times 510-($-$) db 0; fill 0 to meet the file size, make up 510 bytes
; $ Indicates the address of the current statement, and $ indicates the start address of the program.
Db 0x55, 0xaa; end flag of the Bootstrap program

1. Assume that the Bootstrap program is called "boottester. ASM" and the NASM boottester. ASM-O boottest command is used to generate a binary file.
2. Run the command dd If = boottest of = boottest. IMG to create an imgfile.
3. boot with the virtual machine's floppy boot Function
 

For the int 0x10 interrupt used in the code, see some books on assembly language programming, such as <ibm pc assembly language programming>.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.