Write Your Own Operating System Tutorial (2)

zhaozj2021-02-08  228

Lesson 2: Making Our First Bootable Disk

In this Learn, We Well Learn How to create a boot program on a floppy disk. We will start by modify 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 DIS WILL CAUSE for formatted. this will cause you to be unable to access any of the files on the disk. hoot program, we can change 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 modify the code. Type the commman

-A 3E

For.

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 Appers 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 have not 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). Asfin 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 can not 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.

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

New Post(0)