Build your own operating system [1]-boot Program

Source: Internet
Author: User

These days have been looking at the Linux 0.11 kernel, but also looking at some of the small operating system code, Learnos,pretty,iris .... To assemble some of their advantages, make a simple kernel first.

Learning operating system is that I have been thinking, before all just stay in a relatively superficial level, learned some relatively large theoretical knowledge, for the bottom of a little thing or ignorant. The next semester has the "operating system" this course, know that the class will not have any too detailed explanation, the outline of the experiment will not be too in-depth. But I can finally have a little time for my own experiments.:-)

Today officially began hands-on practice, wrote an experimental nature of the boot program.
The following is quoted from the experimental report of Shei
[Quote]
First, I want to explain what the computer does when you press the power button.
When you press the power button, the wire connected with this key will send an electrical signal to the motherboard, the motherboard will pass this electrical signal to the power system, power supply system to start work for the entire power supply, and send an electrical signal to the BIOS, notify the BIOS power supply system is ready to complete. The BIOS then starts a program, host self-test, host self-test is to ensure that each part of the system has been the power support, internal memory, the motherboard of other chips, keyboard, mouse, disk controller and some I/O ports are normally available, since then, the Self-Test program will control the power back to the BIOS.

Next, the BIOS starts to start the operating system.
The BIOS accesses the first sector of the boot disk (0 tracks, 1 sectors, 512 bytes in total), and the BIOS 0x7c00 the contents of this first sector into the memory address and starts executing it. This is the first to start the system, from then on, the system will control the operating system, leaving the matter is your program to complete.

Our task now is to write a program that the system will call a boot program and use it to boot or start our computer. It has the following two features:
1. Size can only be 512 bytes, not more than one byte, and not less than one byte. Because the BIOS can only read 512 bytes of data into memory, many parts of the BIOS will not be picked up
2. It must end with "AA", i.e. the last two bytes (511,512) must be them. This is the flag for the boot zone program to end, without which the BIOS will not treat it as a bootstrapper. (My previous program didn't execute because I didn't write "AA" here)
Put this program on the 0-track, 1-section of the disk so that the disk can be used to boot the system and use your own bootstrapper.

Before you start making the bootstrapper, let's begin by describing how you can develop this in a Windows environment.

First of all, need an experimental environment, you can certainly use the real computer, if you have more than one computer, and do not feel trouble.
Here we use the virtual machine to experiment, it is the same as using a real computer, do not believe that you can experiment yourself later.
I use MS Virtual PC, very simple to use, this is not much to say, it can be a 1.44M-size img file, as an analog floppy disk, so we just have to write our bootstrapper to an IMG file, as if written on a disk, you can use it to boot the system.
After starting the virtual machine, under the Floopy menu, select the Floopy Disk image item, and then select the img file that we generated, it is OK.

Let's talk about the creation of an IMG file generation method
To write the bootstrapper into this 1.44M file, I use the Winhex tool, which is handy for writing binary files directly from a copy, and can also create files of a specified size.
1.44M img file can be created with Winhex, click the New button on the line, size input 1474560, unit byte.
When we do, we'll open the program we wrote with Winhex and copy the contents to the generated IMG file.
[/quote]
Talk about the initiation process very carefully, let me learn a lot of knowledge. His experimental environment is under windows, I said under the Linux experimental environment to build.

1 Download and install a virtual machine, I use the VirtualBox
For detailed use of 2 VirtualBox, please refer to the documentation it brings. After our IMG production is ready, we can start booting with the floppy of the virtual machine.
3 download NASM compilation and linker

The experimental environment can be built after the first experiment, one of the simplest of the boot program.
The code is as follows:

[BITS 16]; To generate a 16-bit program
[ORG 0X7C00]; Start execution from offset position for 0X7C00

XOR ax, Ax
MOV ds, ax
MOV es, ax
Call ShowMessage
JMP $; $ represents the address of this statement, representing an infinite loop in this statement

ShowMessage:
mov ax, bootmsg
mov bp, ax; es:bp= Word string Address
mov cx, 28; cx= words
mov ax, 0x1301; Set display mode
mov bx, 0x000c; bh= pages, bl= color properties
mov dl, 0; Starting position
int 0x10
Ret

Bootmsg:db "Hello, world! I ' am l0y0l ^^!  "         ; Define the hint information, l0y0l is the name of my operating system:-)
Times 510-($-$$) DB 0; Fill 0 to fit the file size, enough 510 bytes
; $ represents the address of the current statement, $$ represents the starting address of the program
DB 0x55, 0XAA; Bootstrapper End Flag

1 Assuming that this bootstrapper is called "boottester.asm", use the command nasm boottester.asm-o boottest to generate the binary file
2 using the command DD If=boottest of=boottest.img to make an img file
3 boot with virtual machine's floppy disk boot function


The code uses that int 0x10 interrupt please refer to some of the 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.