Write Your Own Operating System Tutorial(2)

本文介绍如何在软盘上创建启动程序。先修改Microsoft DOS引导记录,在不改变引导扇区其他数据的前提下替换引导加载程序代码。通过DOS DEBUG程序加载软盘首扇区到内存,修改代码并写回磁盘。还介绍利用BIOS中断在屏幕输出字符,最后可继续学习用汇编器编写程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

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 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值