Write Your Own Operating System Tutorial(1)

原创 2003年01月20日 09:43:00

Lesson 1: The Boot Sector

In this lesson we’ll learn about the contents of the boot sector so that we can learn to write our own boot program.

When the computer boots from a floppy, BIOS (Basic Input/Output System) reads the disk and loads the first sector into memory at address 0000:7C00.  This first sector is called the DOS Boot Record (DBR).  BIOS jumps to the address 0x7C00 and begins executing instructions there.  It is these instructions (the “boot loader”) that will load the operating system (OS) into memory and begin the OS’s boot process.

The first thing to do is to take a look inside the Boot Record.  The DOS utility DEBUG is a widely available tool that can be used to view the contents of memory and disks.  We’ll use DEBUG to look at a floppy disk’s Boot Record.

At a DOS (or Windows) command prompt type debug.  This will leave you with just a hyphen as a prompt.  If you enter letter ‘d’ as a command and press Enter, it will show you a portion of the contents of RAM.  Typing the question mark as a command will give you a list of all the available commands in DEBUG.  (Be very careful when using the DEBUG utility.  This utility can be used to overwrite data on any disk drive, possibly causing loss of data.)

Place a freshly formatted disk in the A: drive.  To load the Boot Record off your floppy disk, type the following command.

-l 0 0 0 1<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

(The first character is the letter ‘l’, not the number ‘1’.)  This command loads sectors off a disk into a portion of RAM.  The 4 numbers after the ‘l’ represent in order, the beginning address where you want the data loaded, the drive number (0 for first floppy driver), the first sector on the disk to load, and how many sectors to load.  Typing this command will load the first sector of the floppy into memory starting at address 0.

Now that we have the Boot Record loaded into memory, we want to view its contents.  Type the following command.

-d 0

What you see are 8 lines that represent the first 128 (0x80 in hex) bytes in the floppy’s Boot Record.  The results (for my floppy disk) are the following.

0AF6:0000  EB 3C 90 4D 53 44 4F 53-35 2E 30 00 02 01 01 00   .<.MSDOS5.0.....

0AF6:0010  02 E0 00 40 0B F0 09 00-12 00 02 00 00 00 00 00   ...@............

0AF6:0020  00 00 00 00 00 00 29 F6-63 30 88 4E 4F 20 4E 41   ......).c0.NO NA

0AF6:0030  4D 45 20 20 20 20 46 41-54 31 32 20 20 20 33 C9   ME    FAT12   3.

0AF6:0040  8E D1 BC F0 7B 8E D9 B8-00 20 8E C0 FC BD 00 7C   ....{.... .....|

0AF6:0050  38 4E 24 7D 24 8B C1 99-E8 3C 01 72 1C 83 EB 3A   8N$}$....<.r...:

0AF6:0060  66 A1 1C 7C 26 66 3B 07-26 8A 57 FC 75 06 80 CA   f..|&f;.&.W.u...

0AF6:0070  02 88 56 02 80 C3 10 73-EB 33 C9 8A 46 10 98 F7   ..V....s.3..F...


At first glance, this doesn’t tell me much.  I can see that it looks like this is a MS-DOS 5.0 disk with no name and a FAT12 file system.  The numbers in the far left column show the memory addresses in RAM.  The hexadecimal numbers in the middle show all the bytes in this portion of memory, and the column on the right shows the ASCII characters that the hex bytes represent (a period is shown if the byte does not translate to any visible character).  Some of the bytes you see in this portion of the Boot Record are parts of instructions in the boot loader, and some of them hold information about the disk such as the number of bytes per sector, the number of sectors per track, etc…

Now it’s time to take a glance at the code for the boot loader.  Type the following command.

-u 0

This performs an “unassemble” operation.  This shows us the same bytes as before (starting with address 0), but this time DEBUG shows us the Intel instructions that these bytes represent.  The results for my floppy are the following.

0AF6:0000 EB3C          JMP     003E

0AF6:0002 90            NOP

0AF6:0003 4D            DEC     BP

0AF6:0004 53            PUSH    BX

0AF6:0005 44            INC     SP

0AF6:0006 4F            DEC     DI

0AF6:0007 53            PUSH    BX

0AF6:0008 352E30        XOR     AX,302E

0AF6:000B 0002          ADD     [BP+SI],AL

0AF6:000D 0101          ADD     [BX+DI],AX

0AF6:000F 0002          ADD     [BP+SI],AL

0AF6:0011 E000          LOOPNZ  0013

0AF6:0013 40            INC     AX

0AF6:0014 0BF0          OR      SI,AX

0AF6:0016 0900          OR      [BX+SI],AX

0AF6:0018 1200          ADC     AL,[BX+SI]

0AF6:001A 0200          ADD     AL,[BX+SI]

0AF6:001C 0000          ADD     [BX+SI],AL

0AF6:001E 0000          ADD     [BX+SI],AL


The first instruction says to jump to address 0x3E.  The bytes after this are the data about the disk I mentioned before and do not really correspond to instructions, but DEBUG does its duty and tries to interpret them as such.

The first instruction jumps over this data to the boot program code that follows starting at address 0x3E.  Let’s look at the instructions there.  Type

-u 3E

Here you can see the beginning of the code that will load the DOS (or Windows) operating system.  This code (for MS-DOS) looks on the disk for the files IO.SYS and MSDOS.SYS.  These files contain the code for the operating system.  The boot loader code will load these files into memory and begin executing them.  If the files are not found on the disk, then the boot loader will display the famous error message.

Invalid system disk

Disk I/O error

Replace the disk, and then press any key


This message can be seen if you look towards the end of the DOS Boot Record.  You can see this on my floppy below.

-d 180

0AFC:0180  18 01 27 0D 0A 49 6E 76-61 6C 69 64 20 73 79 73   ..'..Invalid sys

0AFC:0190  74 65 6D 20 64 69 73 6B-FF 0D 0A 44 69 73 6B 20   tem disk...Disk

0AFC:01A0  49 2F 4F 20 65 72 72 6F-72 FF 0D 0A 52 65 70 6C   I/O error...Repl

0AFC:01B0  61 63 65 20 74 68 65 20-64 69 73 6B 2C 20 61 6E   ace the disk, an

0AFC:01C0  64 20 74 68 65 6E 20 70-72 65 73 73 20 61 6E 79   d then press any

0AFC:01D0  20 6B 65 79 0D 0A 00 00-49 4F 20 20 20 20 20 20    key....IO     

0AFC:01E0  53 59 53 4D 53 44 4F 53-20 20 20 53 59 53 7F 01   SYSMSDOS   SYS..

0AFC:01F0  00 41 BB 00 07 60 66 6A-00 E9 3B FF 00 00 55 AA   .A...`fj..;...U.


This shows the very end of the Boot Record.  The Boot Record is exactly one sector (512 bytes) on the disk.  If it is loaded into memory starting with address 0, then the last byte will be in address 0x1FF.  If you look at the last two bytes of the Boot Record (0x1FE and 0x1FF), you will notice that they are 0x55 and 0xAA.  The last two bytes of the Boot Record must be set to these values or else BIOS will not load the sector and begin executing it.

So, to recap, the DOS Boot Record starts with an instruction to jump over the data that follows that instruction.  These 60 bytes of data starts at address 0x02 and ends on 0x3D, with the boot code resuming at 0x3E and going all the way to 0x1FD, which is followed by the two bytes, 0x55 and 0xAA.  In the next lesson we will use this knowledge to start making our own book program.

Build Your Own Angularjs 读书笔记(AngularJS牛逼的地方在于它内嵌了一个表达式到Function对象的编译器。。。当然还有DI框架)

Build Your Own Angularjs 读书笔记 目录  [隐藏]  1 项目配置2 作用域3 表达式与过滤器4 模块与依赖注入5 辅助函数6 指令 项目配置[编辑] npm ...
  • cteng
  • cteng
  • 2017年05月31日 19:41
  • 1333

Operating Systems: Three Easy Pieces(操作系统:三个简单方面)4.4进程状态

4.4进程状态 至此我们已经对进程的概念、大致的进程创建方式有一定的了解(尽管我们还是要继续完善这个概念),现在让我们来谈谈在特定时间里面进程可能呈现的不同状态。在早期计算机系统中就出现了进程状态的概...
  • u010970209
  • u010970209
  • 2016年03月18日 09:32
  • 418

操作系统概念 第九版 Operating System Concepts, 9th Edition 中文译稿(不定时更新)

前文: 本译稿不定时更新,翻译自Operating System Concepts, 9th Edition. 欢迎您的阅读,欢迎随时交流,仅供学习,请勿用作商业用途. 自行翻译的,水平不够存在错漏在...
  • advanced_delighted
  • advanced_delighted
  • 2016年03月06日 15:20
  • 3514

ROS(Robot Operating System)操作系统探索总结(一)简介

一、历史   随着机器人领域的快速发展和复杂化,代码的复用性和模块化的需求原来越强烈,而已有的开源机器人系统又不能很好的适应需求。2010年Willow Garage公司发布了开源机器人操作系统ROS...
  • yake827
  • yake827
  • 2015年01月06日 20:09
  • 2784

快速了解 Robot Operating System(ROS) 机器人操作系统

http://www.ros.org/关于ROS About ROS http://www.ros.org/about-ros/机器人操作系统(ROS)是用于编写机器人软件的灵活框架。目的在简化多种机...
  • ZhangRelay
  • ZhangRelay
  • 2016年12月19日 17:57
  • 4367

操作系统(Operating System)2

Ch2 Operating System 计算机硬件和软件结构如图。操作系统链接上层应用和下层硬件指令集体系结构。 简单的说,操作系统提供了以下服务: 程序开发 程序运行 I/O设备访问 ...
  • leo_queef
  • leo_queef
  • 2017年05月19日 22:09
  • 170

操作系统三个简单的部分(Operating Systems in Three Easy Pieces闲来无聊,翻译的)1,2(操作系统的介绍部分)

那啥书名具体不知道怎么说才好,反正看英文知道是那么个意思,翻译成中文就是别扭大家就简单接受一下吧,多多包涵(六级擦线而过,灰溜溜~~) Operating Systems in Three Easy...
  • u010970209
  • u010970209
  • 2015年03月30日 14:36
  • 2187

oracle 11gR2 单机使用asm存储 改主机名 重启系统 GI故障处理

一. 问题描述: 因为克隆了11g r2改了hostname 结果重启服务器后就css和oha都起不来了 相关文档: Reconfiguring & Recreating The11gR2,1...
  • 2016年09月18日 17:18
  • 1223


  • ZhangRelay
  • ZhangRelay
  • 2017年04月04日 23:46
  • 1463

py2exe安装及使用; 复制cmd中的内容小记

安装包网上给的资源很多都不能用,自己整理了一份完整的py2exe, 网址如下: http://download.csdn.net/detail/baoyan2015/9729323 选择适合自己...
  • baoyan2015
  • baoyan2015
  • 2017年01月05日 11:01
  • 312
您举报文章:Write Your Own Operating System Tutorial(1)