Improve company productivity with a Business Account.Sign Up


dos boot loading MBR

Posted on 2004-04-20
Medium Priority
Last Modified: 2008-01-16
when i boot from a DOS bootable diskette, is it possible to load the MBR sector of the hard and run it, just as if the computer was booted from hard disk ?
i've found many code examples showing how to load the MBR in memory but i don't find a way to run this code and boot the system.

thank you for your help.
Question by:psadac
  • 6
  • 6
  • 5
  • +3
LVL 11

Assisted Solution

dimitry earned 250 total points
ID: 10870794
Take a look here:

So what is the problem ? You do not know how to compile the code of bootloader ?
You do not now how to insert bootloader into diskette ?
You do not know how to change diskette bootloader to boot from HD ?

What is your main task ? You can simply return from diskete bootloader and BIOS shpuld continue to boot from HD.

Assisted Solution

manish_regmi earned 250 total points
ID: 10876027
 If i understand your problem correctly, You want to load a bootsector from within dos and jump to it.
It can be done.
1. Read the sector of the disk. Bootsector is sector 0, U can use int 13.
2. Store the read buffer to the segment 07c0:0. (I am using this address because bios use this address to boot the computer. Just overwriting it. )
3. make a jump to that address.

you are done.

regards manish.

Expert Comment

ID: 10876225
here is an example how to do it, Some people might say it is risky. But i dont thinkm so. Loadlin does the same.

mov ah, 0x02 /* function 2 */
mov al, 0x1  /* no of sectors */
mov dl, 0x80 /* disk no. 0x80 = 1st hard disk, 0= first floppy */
lea bx, buff  /* this can be any buffer or any address */
int 0x13

/* now the boot sector is loaded at the specified address  */
/* make a jump */

regards manish

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

LVL 19

Assisted Solution

BrianGEFF719 earned 250 total points
ID: 10884994
to add on to manish's code...I've done this too, your best bet is NASM.

using int 13h, 02h, load it to memory address like 5000:5000 or something. Then just do a simple JMP 5000:5000.

Above manish posted the complete code to read the MBR.
Using his code you might want to do...

jmp [buff]

LVL 14

Author Comment

ID: 10898653
thank you for all your suggestions. I've tried with any kind of jump instruction and various tricks with ret, but i get always the same result : it begins to boot and it hangs up without launching the OS. here is the code :

Present segment Para 'code'
            assume CS:Present, DS:Present, ES:Present, SS:MyStack
main PROC far

      org 200h

      mov ax,07C0h
      mov ds, ax
      mov dx, 07C0h      
      mov es, dx
      mov bx, 00h
                mov dl, 80h
      mov dh,0h      
      xor ch, ch
      mov cl, 1h
      mov ax, 0201h
      int 13h
        mov ax,07C0h
        push ax            ; cs : 07C0h
        xor ax,ax
        push ax            ; ip : 0
main ENDP

Present ENDS

MyStack segment para STACK 'STACK'
      byte 200h dup (?)
MyStack ENDS

END main

is there something wrong ?

LVL 11

Expert Comment

ID: 10902028
Try 'retf' instead of 'ret'.

How are you putting it into floppy bootsector ?

Expert Comment

ID: 10910790
 Flags are not the problem.
 The main problem is that si must be pointing to the partition table. The table entry starts at offset 0xbe.

just add a line
mov si, 0xbe

regards manish

Expert Comment

ID: 10910886
no, no,
 sorry about by last post. it is also not the case.
 Si i points to partition table, in the code of MBR.

I have the disassembly if you would like to see.

try using a far jump
jmp far seg:off


LVL 14

Author Comment

ID: 10912796
let me clarify the process :
-i compile a dos executable file
-i boot from a dos bootable diskette (a win98 boot diskette)
-i run the executable previously compiled
-i've tried it on many computers, on a dual boot system with lilo (linux boot loader) it begins to launch lilo then it hangs.
-i've tried your suggestions (retf, far jump) but i get the same result.

so i thnk the jump is not the problem, there must be something else to do before the jump
LVL 11

Expert Comment

ID: 10913323
You want to have a bootloader but what are you doing is not the same at all.
You need to insert it into the bootsector of the floppy and you need to take ORG 200h out.
And you need to reboot your computer and give the BIOS opportunity to boot with the help of your bootloader.
And you can copy your 512 bytes bootloader to floppy with the help of diskeditor or any other utility that
allows you sector copy.
In Linux you can use 'dd':
% dd if=mybootldr.bin of=/dev/fd0 bs=512 count=512
LVL 14

Author Comment

ID: 10913361
dimitry, i already know you can use dd on linux to read or write boot sector, but that's not what i want.
basically i have a dos boot diskette, and on a given condition i want to boot from the hard drive. i thought it was possible to download the boot sector of the hard drive, make a jump to the boot code, but maybe it's not possible.
LVL 11

Expert Comment

ID: 10913599
No, my point is that DOS executable is not the same as Bootloader you are trying to run.
Run your code as Bootloader and not as DOS Executable.

Accepted Solution

Dancie earned 250 total points
ID: 10922773
Why not just execute INT 19h. This is the dos restart interrupt.

From Ralph Brown's interrupt list

This interrupt reboots the system without clearing memory or restoring
        interrupt vectors.  Because interrupt vectors are preserved, this
        interrupt usually causes a system hang if any TSRs have hooked
        vectors from 00h through 1Ch, particularly INT 08.
Notes:      Usually, the BIOS will try to read sector 1, head 0, track 0 from drive
        A: to 0000h:7C00h.  If this fails, and a hard disk is installed, the
        BIOS will read sector 1, head 0, track 0 of the first hard disk.
        This sector should contain a master bootstrap loader and a partition
        table (see #00650).  After loading the master boot sector at
        0000h:7C00h, the master bootstrap loader is given control
        (see #00653).      It will scan the partition table for an active
        partition, and will then load the operating system's bootstrap
        loader (contained in the first sector of the active partition) and
        give it control.
      true IBM PCs and most clones issue an INT 18 if neither floppy nor hard
        disk have a valid boot sector
      to accomplish a warm boot equivalent to Ctrl-Alt-Del, store 1234h in
        0040h:0072h and jump to FFFFh:0000h.      For a cold boot equivalent to
        a reset, store 0000h at 0040h:0072h before jumping.
      VDISK.SYS hooks this interrupt to allow applications to find out how
        much extended memory has been used by VDISKs (see #00649).  DOS 3.3+
        PRINT hooks INT 19 but does not set up a correct VDISK header block
        at the beginning of its INT 19 handler segment, thus causing some
        programs to overwrite extended memory which is already in use.
      the default handler is at F000h:E6F2h for 100% compatible BIOSes
      MS-DOS 3.2+ hangs on booting (even from floppy) if the hard disk
        contains extended partitions which point at each other in a loop,
        since it will never find the end of the linked list of extended
      under Windows Real and Enhanced modes, calling INT 19 will hang the
        system in the same was as under bare DOS; under Windows Standard
        mode, INT 19 will successfully perform a cold reboot as it appears
        to have been redirected to a MOV AL,0FEh/OUT 64h,AL sequence
BUG:      when loading the remainder of the DOS system files fails, various
        versions of IBMBIO.COM/IO.SYS incorrectly restore INT 1E before
        calling INT 19, assuming that the boot sector had stored the
        contents of INT 1E at DS:SI instead of on the stack as it actually
SeeAlso: INT 14/AH=17h,INT 18"BOOT HOOK",INT 49"Tandy 2000",INT 5B"PC Cluster"
SeeAlso: MEM 0040h:0067h,MEM F000h:FFF0h,CMOS 0Fh

Expert Comment

ID: 10924813
 If that algorithm is not a constraint, you can use int 0x19.
Just a single instruction will do the job.
But i was wondering why the first method is not working.

LVL 14

Author Comment

ID: 10969393
sorry, but i can't use int 0x19 : i boot from a dos diskette and i want to run the hard disk boot loader, but i don't want to eject the diskette (it's an automated job). i've tried on many computers, it loads correctly the boot loader like lilo or grub, but it hangs as soon as i choose a system. i think the problem is related to the interrupt vector table, is there a way to reset the interrupt table vectors to their values after bios execution ?

Expert Comment

ID: 10975319
I have done this by first booting from the hard disk with no extra drivers.
By pressing F5. then run debug with these commands

C:\>debug <enter>
ndos.dat   <enter>     ;give a name to write to a file ie. dos.dat
rcs        <enter>       ;set cs register -- gives basis for a data segment
0          <enter>        ; to the first segment
rcx       <enter>        ;set cx to 400h to give the length of the file in bytes
400    <emter>       ;and saves all vectors
w0      <enter>         ;write form the 0 data position
q         <enter>         ;quit

Now you have a data file that can be loaded at 0:0 -- over the interrupt vector table.


Expert Comment

ID: 10993370
hi psadac,
 I checked the code, It also hangs in my computer. There is some problem,  but i am not figuring out where. hmm,
see this link if it can help,

LVL 14

Author Comment

ID: 10994992
ok, it's time to close, i don't have a solution to the problem but thank you for your help.
LVL 11

Expert Comment

ID: 10998198
Hi psadac,

Sorry for delay. I found several small points we didn't mention to you and the code below is booting from floppy from HD.
I used TASM, so my directives are different.
1) MBR is copied to [0:7C00h] and not to [7C0h:0]. It is same physical address, but not the same segment register value.
2) Your bootloader from floppy is copied to the same address, so you need to move it before MBR read.
3) The code below was loaded from floppy and boot MBR from HD.
4) First jump and last 55AA was added to tell to BIOS that floppy has valid boot sector.


  org 0h
  jmp loadmbr

; Self Copy to [0:7A00]
  xor  ax, ax
  mov  ds, ax
  mov  es, ax
  mov  si, 7C00h + offset startload
  mov  cx, offset endload - startload
  mov  di, 7A00h
  rep movsb
; Far jump to loader code in system RAM for execution
  push es
  push 7A00h
  xor ax, ax
  mov es, ax
  mov bx, 7C00h
  mov dl, 80h
  mov dh, 0h
  mov cx, 1h
  mov ax, 0201h
  int 13h

  push 0           ; cs : 0h
  push 7C00h       ; ip : 7C00h
  org 510
  DB  55h, 0AAh
END start
LVL 11

Expert Comment

ID: 11023731
Hi psadac,
Do you need additional help ?

Expert Comment

ID: 12615859
I wonder if someone finally could make and exe file, that succesfully loads MBR from hards disk, being on DOS without removing the flopply?

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Read this article to learn how to migrate data from SharePoint to SharePoint Online account. The entire process of SharePoint migration is described in detail.
Here is how to restore SQL Server database to the point in time.  Follow the step by step approach to restore your database at a specific point in time and also understand its alternate approach.
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…
Watch the software video of Kernel Import PST to Office 365 tools which can easily import PST and OST files to Office 365 for bulk mailboxes. The process of migration is simple and user can map source and destination mailboxes and easily import data…

595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question