Iceberg under the water surface

Source: Internet
Author: User

Iceberg under the water surface

This article is excerpted from the book Orange's implementation of an operating system.

Even a very small program may not be able to run correctly. You may not be surprised at this. Anyone may write less punctuation or be confused about a small logical problem. Fortunately, we can debug and debug to discover errors and improve the program. However, for special programs such as operating systems, we cannot use common debugging tools for debugging. However, even if we have a small boot sector, we are not sure how to write it once. What should we do if it cannot run correctly? What should you do if you don't see what we want on the screen or even restart the machine?
Every problem is a lock. You have to believe that there must be a key in the world that can be opened. You can also find the key.
There may be only 20 lines of code for a boot sector. If Copy & Paste is used, it will take 10 seconds. It will take 10 minutes to copy the Code even if you press the keyboard. However, when you encounter a problem, you may have to spend 10 minutes or even longer to solve it If you accidentally make a small mistake. The author calls the 20 lines of programs as the iceberg above the water surface, and calls the work you spent hours doing as the iceberg below the water surface.
The ancients cloud: "It is better to teach fish to fish ." This book will try to present the part of the iceberg to readers. These are some of my experiences after a painful exploration. These methods may not be the best, but they can at least provide you with a reference. Let's take the just-completed boot sector as an example. As you can imagine, we will certainly expand these 20 lines in the future and finally get 200 lines or more code, we have to find a way to make it easier to debug.
In fact, it is very easy, because bochs, the virtual machine we mentioned earlier, can be used as a debugger. Please allow me to sell another token for further analysis. However, debugging a boot sector, or even an operating system, is not very difficult with the help of tools. However, compared with debugging a common application, the details are inevitably different.
If you are using Windows, there is also an optional method to help debugging, the practice is also very simple, that is, to change the line "org 07c00h" to "org 0100h" can be compiled into one. the COM file is running in DOS. Let's try it. First, change 07c00h to 0100 H. Compile:
$ NASM boot. ASM-O boot.com
Now, a boot sector that is easy to execute and debug is ready. If you are programming on the DoS or Windows platform, debugging the. com file must be your work, for example, using turbo debugger.
After debugging, we need to put it on a floppy disk for testing. We need to change 0100h to 07c00h, Which is troublesome. Fortunately, the powerful NASM provides us with a precompiled macro, change the original "org 07c00h" line to many rows:
1; % DEFINE _ boot_debug _; comment out this line when creating the boot sector!
2. After this line of comment is removed, it can be used as a. com file for easy debugging:
3; NASM boot. ASM-O boot.com
4
5% ifdef _ boot_debug _
6 org 0100 h; debugging status, made into a. com file, debuggable
7% else
8 org 07c00h; BIOS will load the Boot Sector to 0: 7c00
9% endif
In this way, if we want to debug, we can make the first line effective. If we want to do a guide sector, just comment it out.
The pre-compiled command here is similar to the C language, so there is no need to explain it more.
So far, you have learned how to write a simple boot sector and how to debug it. This is like taking the Stone Age to the Iron Age. The wide road is shown in front of us. using tools, we are confident that we will continue to expand the sector and turn it into a part of a real operating system.
Review
Let's take a look at the code just now. You must have understood most of the Code. If you are new to NASM, you may not be so clear about all the details. However, after all, you have discovered that the first step in writing an operating system is so easy.
Yes, this is not a very difficult start. If you think so too, please bring times of confidence and have always wanted to explore the mysteries of the OS. Let's start with me!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.