Write Your Own Operating System Tutorial (3)

zhaozj2021-02-08  252

Lesson 3: NASM

In this lesson we will learn to use an assembler to write our programs. In previous lessons we have assembled them using DEBUG. After playing around with this for a while, you will quickly see that it would be a pain to use DEBUG to create a program of more than a handful of instructions (even harder to modify) We need a simpler way We will start by using the "Netwide Assembler" (NASM) Go to the official web page at http:... //www.octium. NET / NASM / AND Download a Copy of The Assembler.

Now we'll use this assembler to create the same "operating system" that we did at the end of Lesson 2. Download the boot program h.asm and take a look at it. The first instruction should be somewhat familiar by now. This is the instruction to jump over the Boot Record data. In this case, it's a jump to the label begin. After the jump instruction is 20 bytes of data. This is the data that I read off my floppy disk using the DEBUG program. These VALUES SHOULD WORKE. If You Want, Try Replacing The Data with The Data from Your Own Disk. Most of It Should Be The Same.

(NOTE: Keep in mind that numbers made up of more than 1 byte will look "byte swapped" when viewed in DEBUG because on the Intel architecture, the least significant byte is stored at the lowest memory address and vice versa The bytes will look. Backwards.)

The code starting at the label begin should look similar to the code we wrote for Lesson 2. It simply prints the letter 'H' to the screen and loops forever. At the bottom of this file you will see first a check to make sure the code all fits within 512 bytes (the size of one sector), then the line beginning with the word "times" adds zeros to the end of the file to pad the executable to 510 bytes. Finally, the two-byte signature 0x55, 0xAA Is added to the end of the file. assemble the file at the command prompt with the folloading command.nasmw h.asm -o h.bin

This assembles the assembly file to a pure binary executable h.bin. Check the size of the binary file. If we have done things right, it should be exactly 512 bytes. This is exactly the size needed to fit in the boot sector of the FLOPPY.

Now We need to copy this file ontto our floppy. With the floppy in the disk, run debug, and type the folloading commands

Debug

-n h.bin

-l 0

This loads our file into memory starting at address 0. Use the dump (d) and unassemble (u) commands if you wish to confirm that our file has assembled and been loaded correctly. You will be able to see the few instructions that we have written. Notice that the file has been correctly padded with zeros up until bytes 0x1FE and 0x1FF at the end of the sector. Also note that DEBUG fills the CX register with the number of bytes loaded from the file. (Display the contents of the registers With the r command.)

With the floppy disk in the drive, Write the file to the disk with the usual command.

-w 0 0 0 1

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

New Post(0)