Writing the operating system yourself (2)

xiaoxiao2021-03-06  42

Author: Trevor article taken from: open systems world - SEOUL November 1, 2002

The previous issue, I tell how to write some code on the boot sector of the floppy disk, then start from the floppy disk. Making a start-up sector, we should know how to use the BIOS interrupt before switching to the protection mode. The BIOS interrupt is some low-level programs provided by the BIOS that makes the operating system's creation easier. In this article, we will learn to deal with BIOS interrupts.

Why use BIOS

BIOS will copy the start sector to the RAM and perform these code. In addition, BIOS will do a lot of other things. When an operating system is started, there is no graphics driver, floppy disk drive, etc. in the system. Therefore, it is impossible to include any driver in the start sector, and we have to take other ways. At this time, BIOS can help us. The BIOS contains various programs that can be used, including detecting installed devices, control printers, computing memory sizes, etc. for various purposes. These programs are what the BIOS is interrupted.

How to call BIOS interrupt

In a general programming language, the call is a very easy thing. For example, in the C language, if there is a program called Display, it comes with two parameters, where parameter noofchar represents the number of characters displayed, the parameter Attr represents the properties of the display character. So to call it, just give the name of the program. For interrupt calls, we use int instruction in assembly language.

For example, when some things are displayed in the C language, the instructions used are as follows:

Display (NOFCHAR, ATTR);

When using the BIOS, the instructions to achieve the same function are as follows:

INT 0x10

How to transfer parameters

Before calling the BIOS interrupt, we need to send some specific values ​​in the register. Suppose it is to use the BIOS interrupt 13h, the interrupt function is to transfer the data from the floppy disk to the memory. Before calling the interrupt, you must first specify the segment address of the copy data, specify the driver number, the track number, the sector number, and the number of sectors to be transmitted. Then, send the corresponding register to the corresponding register. Before making the following steps, the reader must have a more clear understanding of this.

In addition, a more important fact is that the same interrupt can often achieve a variety of different functions. The exact functionality implemented by the interrupt depends on the function number selected, and the function number is generally in the AH register. For example, the interrupt 13h can be used to read the disk, write disk, etc.

What we have to do

This time our source code consists of two assembly language programs and a C program. The first assembler is the code that boots the sector. In the boot sector, the code we write is to copy the second sector in the floppy disk to the 0x500 of the memory segment (the address is 0x5000, that is, the offset address is 0). At this time we need to use the BIOS interrupt 13h. At this time, the code that starts the sector will transfer the control to 0x500. In the second assembly file, the code will use the BIOS interrupt 10h to display an information on the screen. The function of the C procedure is to copy the executable file 1 to the start sector, copy the executable file 2 to the second sector of the floppy disk.

Start sector code

Using interrupt 13h, the start sector puts the contents of the floppy disk in the second sector of the floppy disk to the memory of the 0x5000 (segment address is 0x500). The following code is a code for implementing this, saving it to the file sbect.s.

LOC1 = 0x500

Entry Start

Start:

Mov AX, # loc1

Mov ES, AX

Mov bx, # 0

Mov DL, # 0

Mov Dh, # 0

Mov ch, # 0

Mov Cl, # 2

Mov al, # 1

Mov Ah, # 2

INT 0X13JMPI 0, # loc1

The first line above is similar to a macro. The next two rows are loaded to the ES register to the ES register, which is where the second sector code on the floppy disk will be copied (the first sector is a start sector). At this time, the offset within the segment is set to 0.

Next, the drive letter is sent to the DL register, where the head number is sent to the DL register, and the track number is sent to the CH register, and the sector number is sent to the CL register, and the sector number is sent into the Al register. The function we want to implement is to send the sector 2, the magnetic track number 0, the content of the drive number 0 is 0x500. All of these parameters correspond to the 1.44MB floppy disk.

The 2 is sent to the AH register, which is selected from the corresponding functionality provided by the interrupt 13h, that is, the function of transferring data from the floppy drive drive.

Finally, the interrupt 13h is called, and it is transferred to a segment address 0x500 of the offset zero.

The second sector code

The code in the second sector is as follows (saving these code into the file sBect2.s):

Entry Start

Start:

Mov Ah, # 0x03

XOR BH, BH

INT 0x10

Mov CX, # 26

Mov bx, # 0x0007

Mov bp, # mymsg

Mov AX, # 0x1301

INT 0x10

LOOP1: JMP loop1

Mymsg:

.bete 13, 10

.ascii "Operating System Is Loading ..."

The above code will be loaded to the segment address of 0x500 and is executed. In this code, the interrupt 10h is used to obtain the current cursor position and then display information.

From the third line to the 5th line to obtain the current cursor position, the interrupt 10h is selected is functional 3. Then, the contents of the BH register are cleared and the string is sent to the CH register. In BX, we sent the page and displayed properties. Here, we want to show white characters on a black background. Then, in the address to display the character, the information consists of two bytes, and its value is 13, which respectively corresponds to the ASCI II value of the Enter and the LF (Renewal), respectively. Next is a string consisting of 29 characters; the functionality implemented below is the output string and move the cursor; finally call the interrupt, then enter the loop.

C program code

The source code of the C program is stored as a Write.c file as shown below.

#include

/ * Unistd.h Needs this * /

#include

/ * Contains Read / Write * /

#include

int main ()

{

Char boot_buf [512];

INT FLOPPY_DESC, FILE_DESC;

FILE_DESC = Open ("./ bsect", o_rdonly);

Read (File_Desc, Boot_buf, 510);

Close (file_desc);

Boot_buf [510] = 0x55;

Boot_buf [511] = 0xAA;

FLOPPY_DESC = Open ("/ dev / fd0", o_rdwr);

Lseek (FLOPPY_DESC, 0, Seek_set);

Write (FLOPPY_DESC, BOOT_BUF, 512);

FILE_DESC = Open ("./ SECT2", O_RDONLY);

READ (File_Desc, Boot_BUF, 512);

Close (file_desc);

Lseek (Floppy_Desc, 512, seek_set);

Write (FLOPPY_DESC, BOOT_BUF, 512);

Close (FLOPPY_DESC);

}

In the previous phase, I have introduced how to operate the floppy disk that can start. Now this process is slightly different, first copy the executable BSECT compiled by bsect.s to the boot sector of the floppy disk. Then copy the executable file SECT2 generated by SECT2.s to the second sector of the floppy disk. Place the above files under the same directory, then compile them, and the method is as follows:

As86 bsect.s -o bsect.o

LD86 -D bsect.o -o bsect

Repeat the above operations for the SECT2.s file to draw the executable file SECT2. Compile Write.c, execute the write file after inserting the floppy disk, the command is as follows:

CC Write.c -o Write

./write

The next step we have to do

After starting from the floppy disk, you can see the displayed string. This is done using the BIOS interrupt. The next issue to do is to achieve the conversion of the real mode to the protection mode in this operating system.

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

New Post(0)