China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 22:58
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » How It Works --- Master Boot Record (Master Boot Record) View 1,125 Replies 0
Original Poster Posted 2003-06-01 00:00 ·  中国 江西 吉安 电信
版主
★★★★
Credits 7,296
Posts 1,628
Joined 2002-10-16 12:00
23-year member
UID 10
Gender Male
Status Offline
How It Works --- Master Boot Record (Master Boot Record)
version 1a
Author: Hale Landis (landis@sugs.tware.com)
Translator: qnp(South China Kapok)
"How It Works" series
This document is one article in the "How It Works" series. At present this series includes the following documents:
* How It Works -- CHS Translation
* How It Works -- Master Boot Record
* How It Works -- DOS Floppy Boot Sector
* How It Works -- OS2 Boot Sector
* How It Works -- Partition Tables
This article introduces the disassembled program of the MBR. The MBR is the area placed at cylinder 0, head 0, sector 1 of the hard disk.
The MBR is created by the FDISK program. The FDISK program of every operating system creates a similar MBR
record. The MBR is the prerequisite for multiple partitions; each MBR includes four partition table entries. After your computer is powered on,
INT19 is called. Generally speaking, INT19 first reads the boot sector record from the first floppy drive.
If a boot sector is found on the floppy, then the data from that sector is read into memory at address 0000:7c00
and INT19 also jumps to this address. But if this sector is not found on the floppy, INT
19 then reads from the first hard disk. Likewise, if successful, it also loads the MBR into the same memory address
0000:7c00, and INT19 also jumps to this address. A small program contained in the MBR determines the active par
tition in the partition table. After such an active partition is found, the boot sector of that partition is also read into address 0000:7c00, and the MBR program
also transfers to this address. Every operating system has a fixed boot sector format. The pro
gram in the boot sector must be able to determine the location of the operating system startup program (or the kernel itself, or a boot manager), and then
load it into memory.
Also: when the Ctrl-Alt-Del keys are pressed, INT19 is also called. On most systems, pressing the Ctrl-Alt-Del three?
keys at the same time means warm-booting your computer, returning to the state before the INT19 call.
=====
Locations where data is stored:
The MBR program is placed at offset 0000.
The MBR messages are placed at offset 008b.
The partition table is placed at offset 00be.
The signature is placed at offset 00fe.
Summary of who does what:
If an active partition is found, the boot data of this partition is loaded into location 0000:7c00, and the MBR pro
gram also jumps to location 0000:7c00. This program finds the pointer to the partition table entry, and the entry describes how this partition is
to be booted. The boot program uses this data to determine the drive used for booting and the location of this drive partition on the hard disk.
If such an active partition entry is not found, ROM BASIC will transfer to the INT 18 call. All other er
rors cause the system to hang; see the HANG label.

Note (extremely important):
  1) The content of the first byte of the active partition entry is 80h. Before INT13 is called, this byte is lo
aded into the DL register. When INT13 is called, DL represents the BIOS device number. Because of this, the boot sector read by the MBR
program can only be read from a hard disk whose BIOS device number is 80. This is why it cannot boot from just any hard disk.
?
  2) The MBR program uses CHS based on INT 13h (ah=02h) interrupts to read the location of the active partition's boot sector.
The location of the active partition's boot sector is indicated in the partition table in CHS format. If this drive is smaller than
528M, this CHS must be (L-CHS, see the BIOS TYPES document). No address in LBA is of any use.
(Another reason why LBA cannot solve the >528M problem) Don't believe this part, hehe.


====
This is a table of the entire MBR record (in hex and ascii format)
OFFSET 0 1 2 3 4 5 6 7 8 9 A B C D E F *0123456789ABCDEF*
000000 fa33c08e d0bc007c 8bf45007 501ffbfc *.3.....|..P.P...*
000010 bf0006b9 0001f2a5 ea1d0600 00bebe07 *................*
000020 b304803c 80740e80 3c00751c 83c610fe *...<.t..<.u.....*
000030 cb75efcd 188b148b 4c028bee 83c610fe *.u......L.......*
000040 cb741a80 3c0074f4 be8b06ac 3c00740b *.t..<.t.........x....*
0001d0 00000000 00000000 00000000 00000000 *................*
0001e0 00000000 00000000 00000000 00000000 *................*
0001f0 00000000 00000000 00000000 000055aa *..............U.*
====
This is the disassembled program of the MBR:
This sector is loaded into memory at location 0000:7c00, but it immediately relocates itself to location 0000:0060
?

BEGIN: # NOW AT 0000:7C00, RELOCATE
0000:7C00 FA CLI #disable interrupts
0000:7C01 33C0 XOR AX,AX #set the stack segment address to 0000
0000:7C03 8ED0 MOV SS,AX
0000:7C05 BC007C MOV SP,7C00 #set the stack pointer to 7c00
0000:7C08 8BF4 MOV SI,SP #SI = 7c00
0000:7C0A 50 PUSH AX
0000:7C0B 07 POP ES #ES = 0000:7c00
0000:7C0C 50 PUSH AX
0000:7C0D 1F POP DS #DS = 0000:7c00
0000:7C0E FB STI #enable interrupts
0000:7C0F FC CLD #clear direction
0000:7C10 BF0006 MOV DI,0600 #DI = 0600
0000:7C13 B90001 MOV CX,0100 #move 256 words (512 bytes)

0000:7C16 F2 REPNZ #move the MBR from 0000:7c00
0000:7C17 A5 MOVSW #to 0000:0600
0000:7C18 EA1D060000 JMP 0000:061D #jump to NEW_LOCATION
NEW_LOCATION:# NOW AT 0000:0600
0000:061D BEBE07 MOV SI,07BE #point to the first byte of the entry
0000:0620 B304 MOV BL,04 #there are 4 entries
SEARCH_LOOP1: # search for active partition
0000:0622 803C80 CMP BYTE PTR ,80 #is it an active partition?
0000:0625 740E JZ FOUND_ACTIVE #yes, stack pointer points to 7c00
0000:0627 803C00 CMP BYTE PTR ,00 #is it an inactive partition?
0000:062A 751C JNZ NOT_ACTIVE #jump to NOT_ACTIVE
0000:062C 83C610 ADD SI,+10 #increment table pointer by 16
0000:062F FECB DEC BL #decrease count
0000:0631 75EF JNZ SEARCH_LOOP1 #loop
0000:0633 CD18 INT 18 #jump to ROM BASIC
FOUND_ACTIVE: # active partition found
0000:0635 8B14 MOV DX, #set DH/DL for INT 13
0000:0637 8B4C02 MOV CX, #set CH/CL for INT 13
0000:063A 8BEE MOV BP,SI #save table pointer
SEARCH_LOOP2: # make sure it is one active partition
0000:063C 83C610 ADD SI,+10 #increment table pointer by 16
0000:063F FECB DEC BL #decrease count
0000:0641 741A JZ READ_BOOT #if end of table, jump
0000:0643 803C00 CMP BYTE PTR ,00 #is it an inactive partition
0000:0646 74F4 JZ SEARCH_LOOP2 #yes, loop
NOT_ACTIVE: # more than one active partition
0000:0648 BE8B06 MOV SI,068B #display "Invld prttn tbl"
DISPLAY_MSG: # display message loop
0000:064B AC LODSB #get a character from the message
0000:064C 3C00 CMP AL,00 #check for end of message
0000:064E 740B JZ HANG #yes
0000:0650 56 PUSH SI #save SI
0000:0651 BB0700 MOV BX,0007 #screen attribute
0000:0654 B40E MOV AH,0E #display one character
0000:0656 CD10 INT 10
0000:0658 5E POP SI #restore SI
0000:0659 EBF0 JMP DISPLAY_MSG #loop
HANG: #hang the system
0000:065B EBFE JMP HANG #stop here
READ_BOOT: #read the data of the active partition
0000:065D BF0500 MOV DI,0005 #number of INT 13 attempts
INT13RTRY: #INT 13 retry loop
0000:0660 BB007C MOV BX,7C00
0000:0663 B80102 MOV AX,0201 #read one sector
0000:0666 57 PUSH DI #save DI
0000:0667 CD13 INT 13 #read the sector into 0000:7c00
0000:0669 5F POP DI #restore DI
0000:066A 730C JNB INT13OK #no INT 13 error, jump
0000:066C 33C0 XOR AX,AX #call INT 13, hard disk reset
0000:066E CD13 INT 13
0000:0670 4F DEC DI #DI--
0000:0671 75ED JNZ INT13RTRY #if nonzero, retry
0000:0673 BEA306 MOV SI,06A3 #display "Errr ldng systm"
0000:0676 EBD3 JMP DISPLAY_MSG #jump to display loop
INT13OK: #INT 13 error
0000:0678 BEC206 MOV SI,06C2 #"missing op sys"
0000:067B BFFE7D MOV DI,7DFE #point to signature
0000:067E 813D55AA CMP WORD PTR ,AA55 #is the signature correct?
0000:0682 75C7 JNZ DISPLAY_MSG #not correct, jump to display message loop
0000:0684 8BF5 MOV SI,BP #set SI
0000:0686 EA007C0000 JMP 0000:7C00 #jump to boot sector, SI points to partition
table en
?

#here are the messages
0000:0680 ........ ........ ......49 6e76616c * Inval*
0000:0690 69642070 61727469 74696f6e 20746162 *id partition tab*
0000:06a0 6c650045 72726f72 206c6f61 64696e67 *le.Error loading*
0000:06b0 206f7065 72617469 6e672073 79737465 * operating syste*
0000:06c0 6d004d69 7373696e 67206f70 65726174 *m.Missing operat*
0000:06d0 696e6720 73797374 656d00.. ........ *ing system. *
#unused data
0000:06d0 ........ ........ ......00 00000000 * .....*
0000:06e0 00000000 00000000 00000000 00000000 *................*
0000:06f0 00000000 00000000 00000000 00000000 *................*
0000:0700 00000000 00000000 00000000 00000000 *................*
0000:0710 00000000 00000000 00000000 00000000 *................*
0000:0720 00000000 00000000 00000000 00000000 *................*
0000:0730 00000000 00000000 00000000 00000000 *................*
0000:0740 00000000 00000000 00000000 00000000 *................*
0000:0750 00000000 00000000 00000000 00000000 *................*
0000:0760 00000000 00000000 00000000 00000000 *................*
0000:0770 00000000 00000000 00000000 00000000 *................*
0000:0780 00000000 00000000 00000000 00000000 *................*
0000:0790 00000000 00000000 00000000 00000000 *................*
0000:07a0 00000000 00000000 00000000 00000000 *................*
0000:07b0 00000000 00000000 00000000 0000.... *............ *
#the partition table starts at 0000:07be. Each partition table entry is 16 bytes. This table defines a single primary par
tition and
is an active partition.
0000:07b0 ........ ........ ........ ....8001 * ....*
0000:07c0 0100060d fef83e00 00000678 0d000000 *......>....x....*
0000:07d0 00000000 00000000 00000000 00000000 *................*
0000:07e0 00000000 00000000 00000000 00000000 *................*
0000:07f0 00000000 00000000 00000000 0000.... *............ *
#the last two bytes must include the data 55aah.
0000:07f0 ........ ........ ........ ....55aa *..............U.*

(This article was copied using the S-Term article copy script)
==================================================
KO says:
I found the English version of this before and originally wanted to translate it, but unfortunately it was too long... I wanted to translate it by programming methods, but I don't know programming, :-( and if I used software to translate it intelligently, the software was never really that intelligent, and the proofreading work took almost as long as the translation work, maybe even longer...
Now I found a ready-made one, hehe...



ko20010214
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器
Forum Jump: