|
|
"Linux Gazette...making Linux just a little more fun!"
Writing Your Own Toy OS (PART II)By Krishnakumar R.
Part I was published in April. The next thing that any one should know after learning to make a boot sector and before switching to protected mode is, how to use the BIOS interrupts. BIOS interrupts are the low level routines provided by the BIOS to make the work of the Operating System creator easy. This part of the article would deal with BIOS interrupts. 1. Theory1.1 Why BIOS ?BIOS does the copying of the boot sector to the RAM and execution of code there. Besides this there are lot of things that the BIOS does. When an operating system boots up it does not have a video driver or a floppy driver or any other driver as such. To include any such driver in the boot sector is next to impossible. So some other way should be there. The BIOS comes to our help here. BIOS contains various routines we can use. For example there are ready made routines available for various purposes like, checking the equipments installed, controlling the printer, finding out memory size etc. These routines are what we call BIOS interrupts. 1.2 How do we invoke BIOS interrupts ?In ordinary programming languages we invoke a routine by making a call to the routine. For example in a C program, if there is a routine by name For example for printing something on the screen we call the C function like this :
Equivalent to this, when we use BIOS, we write :
1.3 Now, how do we pass the parameters ?Before calling the BIOS interrupt, we need to load certain values in prespecified format in the registers. Suppose we are using BIOS interrupt 13h, which is for transferring the data from the floppy to the memory. Before calling interrupt 13h we have to specify the segment address to which the data would be copied. Also we need to pass as parameters the drive number, track number, sector number, number of sectors to be transferred etc. This we do by loading the prespecified registers with the needed values. The idea will be clear after you read the explanation on the boot sector we are going to construct. One important thing is that the same interrupt can be used for a variety of purposes. The purpose for which a particular interrupt is used depends upon the function number selected. The choice of the function is made depending on the value present in the 2. What are we going to do ?This time our source code consists of two assembly language programs and one C program. First assembly file is the boot sector code. In the boot sector we have written the code to copy the second sector of the floppy to the memory segment 3. The boot sectorUsing interrupt 13h, the boot sector loads the second sector of the floppy into memory location 0x5000 (segment address 0x500).
Given below is the source code used for this purpose. Save the code to file
The first line is similar to a macro. The next two statements might be familiar to you by now. Then we load the value 0x500 into the Next we load drive number into Moving value 2 into register Now we call interrupt 13h and finally jump to 0th offset in the segment 4. The second sectorThe code in the second sector will as given below :
This code will be loaded to segment 0x500 and executed. The code here uses interrupt 10h to get the current cursor position and then to print a message.
The first three lines of code (starting from the 3rd line) are used to get the current cursor position. Here function number 3 of interrupt 5. The C programThe source code of the C program is given below. Save it into file
In PART I of this article I had given the description about making the boot floppy. Here there are slight differences. We first copy the file bsect, the executable code produced from bsect.s to the boot sector. Then we copy the sect2 the executable corresponding to sect2.s the second sector of the floppy. Also the changes to be made to make the floppy bootable have also been performed.
6. DownloadsYou can download the sources from Remove the txt extension of the files, and type
at the shell prompt or you can compile everything separately.
Type
and repeat the same for sect2.s giving sect2.
Compile write.c and execute it after putting the boot floppy in to drive by typing :
7. What Next?After booting with the floppy you can see the string being displayed. Thus
we will have used the BIOS interrupts. In the next part of this series I hope
to write about how we can switch the processor to protected mode. Till then,
bye !
|

Krishnakumar R.