ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Write Your Own Operating System Tutorial(2) (转)

Write Your Own Operating System Tutorial(2) (转)

原创 Linux操作系统 作者:gugu99 时间:2007-12-02 14:24:33 0 删除 编辑
Write Your Own Operating System Tutorial(2) (转)[@more@]

Lesson 2: Making Our First Bootable Disk

In this lesson, we will learn how to create a boot program on a floppy disk.  We will start by modifying the microsoft Dos Boot Record.

For our purposes, we want to replace the boot loader code without changing the other data in the boot sector.  If we change the data to something invalid, then DOS and windows will not recognize the disk as being valid.  Windows will give an error saying the disk is not formatted.  This will cause you to be unable to access any of the files on the disk.  However, we can change the boot program code all we want and, as long as we don’t mess with the other data, DOS and Windows will be able to read and write the files on the disk just fine.

We will leave the first instruction (jmp 0x3E) alone, because we need to jump over the Boot Record data.  Thus we can begin modifying the code at 0x3E.  Run the DOS DEbug program and load the first sector of a formatted floppy disk into memory at address 0.  Then type the command

-u 3E

 XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

to view the instructions there.  Now, we will begin modifying the code.  Type the command

-a 3E

 

to begin assembling instructions.  The prompt changes from a hyphen to the address at the location that we gave.  Type the following instruction and press enter.

jmp 3E

 

The instruction is assembled to machine code and placed into memory, and the following prompt is the next available memory after the instruction you just entered.  Press Enter once more to exit the assembly mode.  The whole procedure on my computer looked like this.

-a 3E

0AFC:003E jmp 3E

0AFC:0040

-

 

The segment address (0x0AFC, in my case) can (an probably will be) different on your computer, or even between different sessions of DEBUG.  Now view the instruction you just entered by giving the unassembled command.

-u 3E

 

As you can see, the first instruction is now our jump instruction.  This will create an infinite loop.  If we quit DEBUG now, no changes will be saved, but we can now write our modified boot sector back to the disk (overwriting the previous one) by typing this command.

-w 0 0 0 1

 

This "write" command uses the same syntax as the "load" command.  This writes the data found at memory address 0 to disk 0, starting with sector 0 and writing 1 sector.  Be very careful when using the write command.  This command can be used to overwrite sectors on any drive, and cause loss of data.

You can now boot with this floppy.  When you boot, BIOS will load the first sector off the disk into memory and begin execution at the beginning of the sector.  This will be the jump to 0x3E instruction.  The instruction there is one to jump to 0x3E, so this will continue forever.  Try it.  Boot up a computer with this disk.  Nothing appears to happen.  The computer will just sit there and do nothing.  But your new “operating system” is running.

Okay, I know what you’re saying, you want to see some sign that the code you wrote is actually does running and you haven’t done something to mess up your computer.  In order to do this, we are going to make function calls to BIOS (at least at first).  As of the time of this writing, you can find a short list of BIOS function calls at http://users.win.be/W0005997/GI/biosref.html.  A longer list of software interrupts can be found at .NET/Interrup.txt">http://burak1.virtualave.net/Interrup.txt, but keep in mind that some of those interrupts are BIOS calls, while others are MS-DOS calls which cannot be used since, of course, MS-DOS is not running.  You would have to implement those functions yourself before using them.

We are going to use interrupt 0x10, function 0x0E to write a character to the screen.  The registers must be set as follows.

AH = 0x0E

AL = ASCII code of the character to be printed

BL = color/style of character

 

Now, repeat the instructions in this lesson, only instead of entering the jump instruction as we did before, this time enter the following instructions.

-a 3E

0AF6:003E mov ah, 0e

0AF6:0040 mov al, 48

0AF6:0042 mov bl, 07

0AF6:0044 int 10

0AF6:0046 jmp 46

0AF6:0048

-

 

First we set AH to zero, AL to 0x48 (ASCII for the letter ‘H’), BL to 7 (color code for white-on-black), and then we call interrupt 0x10, which handles the video controller.  The last instruction creates an infinite loop like before, so things stop there.  Save the modified boot sector to a disk (-w 0 0 0 1) and try booting with the disk.  This time you should see the character ‘H’ printed on the screen before the system hangs.

Play around with this for a while.  You can repeat the code for printing a character multiple times to print a phrase, or you can try out other software interrupts.  When you are done, continue on to the next lesson where we will learn to use a full-blown assembler to write our programs rather than DEBUG.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-987359/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3122
  • 访问量
    2244931