Author: Trevor article taken from: open systems world - SEOUL October 10, 2002
The Free Software Community is a place full of freedom and dreams, which created another miracle in more than 10 years. However, these miracles of these miracles are not just Stallman, nor is it Linus Torvalds, but is active in the world's countless developers.
In the use of a variety of powerful free software, I will always be full of respect for their developers, and I hope that I can become one of them in one day. Many people who are full of love for the free community, although I want to strive to strive in it, but I don't know what to do. So, please start with us to write a simple operating system!
What we have to do
Some people may worry that they have neither learned the computer principles, and they have not studied operating system principles. I don't understand the language of assembly. Do you know how to use the C language? Can you write an operating system? The answer is no problem. I will take you step by step to complete your operating system. Of course, if you learn the above content, it is ok.
The first thing to confirm the processor (that is, the CPU) is controlled. For PCs, when startup, the CPU is in real mode status, which is equivalent to just an Intel 8086 processor. That is, even if you have a Pentium processor now, its function can only be 8086 level. From this point of view, you can use some software to convert the processor to the famous protection mode. Only in this way can we make full use of the powerful function of the processor.
Writing the operating system begins to control the BIOS and remove the program stored in the ROM. The BIOS is used to perform POST (Power On Self Test, self-test). Self-test is to check the integrity of the computer (such as whether the peripheter is working properly, the keyboard is connected, etc.). After all of this, you will hear the PC speaker to make a crisp sound. If everything is normal, the BIOS selects a boot device and read the first sector of the device (ie, the boot sector), and then the control process will be transferred to the specified location. The boot device may be a floppy disk, an optical disk, a hard disk, or other selected device. Here we put the floppy disk as a startup device. If we have written some code in the boot sector of the floppy disk, it is executed. Therefore, our purpose is clear, just write some programs to the boot sector of the floppy disk.
First use the 8086 assembly to write a small program, then copy it to the boot sector of the floppy disk. In order to achieve a copy, you have to write a C program. Finally, start the computer using a floppy disk.
Tools needed
● AS86: This is a assembler that converts the code to the target file.
● LD86: This is a connector, the target code generated by the AS86 is converted into a real machine language. The machine language is the form of 8086 can interpret.
● GCC: The famous C programmer. Because we need to write a C program to transfer your OS to the floppy disk.
● A empty floppy disk: It is used to store the written operating system and the startup device.
● A computer with Linux: This machine can be very old, 386, 486 can.
There will be AS86 and LD86 in most standard Linux distributions. These two tools are included in the Red Hat 7.3 I am using, and in the default, it is already installed in the machine. If you don't use these two tools, you can download (http://www.cix.co.uk/~mayday/), both of which are included in a package called bin86. In addition, the relevant documents can also be available online (www.linux.org/docs/ldp/howto/assembly-howto/as86.html).
start working
Use a editor you like to enter the following:
Entry StartStart: MOV AX, # 0XB800 MOV ES, AX SEG ES MOV [0], # 0x41 SEG ES MOV [1], # 0x1floop1: JMP loop1 This is an assembler that AS86 can read. The first sentence indicates the entry point of the program and declares that the entire process begins at START. The second line indicates the location of the Start, indicating that the entire program begins to start from START. The 0xB800 is the start address of the memory. # Indicates that it is an immediate number. Execute a statement:
Mov AX, # oxb800
The value of the AX register changes to 0xB800, which is the address of the memory. This value is moved to the ES register, and ES is an additional segment register. Remember that 8086 has a segmentation architecture. Its segment registers are code segments, data segments, stack segments, and additional sections, corresponding register names, CS, DS, SS, and ES, respectively. In fact, we sent the memory address into additional sections, so anything sent to the additional segment will be sent to the memory.
To display characters on the screen, you need to write two bytes to your memory. The previous one is the ASCII value of the characters to display, and the second byte indicates the properties of the character. Attributes include the foreground color, background color and whether the background color is blinking, and so on. SEG ES indicates that the next command to be executed is to the ES segment. So, we send value 0x41 (a character represented in ASCII is a) to the first byte of the memory. Next, you want to send the properties of the character to the next byte. The 0x1f is entered here, which is a character that shows a white in a blue background. Therefore, if this program can be executed, you can get a white A on the blue bottom on the screen. Then it is a loop. Because after the task is executed, the program is either end, or use a loop to make it run forever. Name this file boot.s, then store.
The concept of memory here is not very clear, it is necessary to further explain it. Assume that the screen consists of 80 columns × 25 lines, then the first line takes 160 bytes, one byte is used to represent characters, and one byte is used to represent the properties of the character. If you want to display a character in the third line, you want to skip the No. 0 and 1 bytes of the memory (they are used to display the first column), the second and 3 bytes (they are used to display the second Column), then put the ASCII code value of the display character into the 4th byte, write the properties of the characters to the 5th byte.
Write the program to the start sector
The following is written in a C program and write my operating system into the first sector of the floppy disk. The program content is as follows:
#include
First, open the boot file in read-only mode, then copy the file descriptor to the file_desc variable when the file is opened. Read 510 characters from the file, or read until the end of the file. In this case, since the file is small, it is read to the end of the file. Then close the file. The last 4 lines of code open the floppy disk drive device (generally / dev / fd0). Use LSeek to find the file start and write 512 bytes from the buffer.
In the Help page of Read, Write, Open, and LSeek, you can see all related parameters and how the usage is used. There are two lines in the program.
Boot_buf [510] = 0x55; boot_buf [511] = 0xAA;
This information is used for BIOS, and if it identifies that the device is a bootable device, the value of 510 and 511 should be 0x55 and 0xAA. The program will read the file boot into buffer named boot_buf. It requires to change the 510 and 511 bytes, and then write boot_buf to the floppy disk. If the code is executed, the first 512 bytes on the floppy contain the startup code. Finally, save the file as Write.c.
Compile operation
Use the following command to turn the file to an executable:
AS86 boot.s -o boot.old86 -d boot.o -o bootcc write.c -o write
First compile the boot.s file into the target file Boot.o, then connect the file into the final boot file. Finally, the C program compiles the executable WRITE file.
Insert a blank floppy disk, run the following procedure:
./write
Restart the computer, perform the BIOS interface setting, and set the floppy disk as the first start-up device. Then insert the floppy disk, the computer starts from the floppy disk.
After the startup is complete, you can see a letter A (Blue Bit) on the screen, the start speed is very fast, almost instantly completed. This means that the system has already started from the floppy disk we make, and executes programs just written to start sectors. Now it is in a state in an infinite loop. So, if you want to enter Linux, you must take your floppy disk and restart the machine.
At this point, this operating system is completed, although it does not implement any function, but it can already start the machine.
The next issue I will add some code in this launch sector program, so that it can do some more complicated things (such as using BIOS interrupts, protect mode switches, etc.).