Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top