-1

I am trying to write the full OS independent program on C++. I want to load the program just before booting up the OS (i.e. Windows). I tried to replace the MBR with my simple “Hello World” program, but nothing happens and I lost Partition table of Virtual Hard disk. I think I should write the C++ compiler to the MBR to directly execute the program or source code.

Q. The Compiler is in EXE format so should require Windows or DOS. Does it can be written on MBR?

Q. The C++ program require DOS platform. Does it can be run without any OS?

I am trying to write a Boot loader and some same sort of programs.

Puppy
  • 144,682
  • 38
  • 256
  • 465
Farid-ur-Rahman
  • 1,809
  • 9
  • 31
  • 47
  • 5
    The MBR is only 512 bytes. How are you going to write the compiler to it? Further, the compiler needs OS facilities so that it can read and write files. In fact, you need the OS just to write "Hello World." Your idea is a non-starter. – Robert Harvey Aug 28 '11 at 23:31
  • You are very much off base... If you can't get a "hello world" program into the MBR, how will you install an interpreter? – Hasturkun Aug 28 '11 at 23:31
  • I suggest you look into some existing boot loaders, e.g. GRUB: http://www.gnu.org/software/grub/ - it is written in C partially. – osgx Aug 28 '11 at 23:33
  • I guess the [standard reference](http://wiki.osdev.org/C%2B%2B_Bare_Bones) can't hurt... – Kerrek SB Aug 29 '11 at 00:04
  • You can not use c/c++ because they require a memory-management of a os. Look for http://assembler86.de (warning: oldscool x86 required, 64-cpu not supported), you can easy write a "hello world" as an MBR (tooks 5 lines of code). Hexworkshop let you open a drive for place the MBR onto a usb-stick or "floppy". – Grim Jul 04 '14 at 19:09

1 Answers1

1

It is impossible to write such program in plain C/C++. There is no OS, and the compiler should work in "freestanding" mode. In this mode there is not "stdin.h" header, no files, no file access and no printf are available.

So, you program should call BIOS functions directly, e.g. using asm keyword.

Format of MBR binary is very close to COM file format of DOS. It is possible to convert short COM program into MBR boot code (by manipulating some parameters of compilation).

Also, you should know that MBR is one sector long and it contains a partition table. So, you have only ~450 bytes to store binary code.

osgx
  • 90,338
  • 53
  • 357
  • 513
  • Is it possible to save all header files to the disk and compiler upon request call them?. The Build program of C is in exe format. How do I convert it into COM. – Farid-ur-Rahman Aug 28 '11 at 23:33
  • 1
    Not, it is impossible. Headers defines functions from libc - standard c library. This library is HUGE, and it uses OS to work. If there is no OS, there is no libc. (The usual libc can't work; and no libc can be fit in 512 bytes or in tens kbytes if additional sectors before partition is used, like in grub for FS code) – osgx Aug 28 '11 at 23:35