5

Suppose, I have a simple Hello world program which is built in C. Now How can I boot it during start my PC?

My wish is to make simple hello world Operating System. I've quite knowledge regarding C and C++, how can I boot it? Please let me know. Do I need to learn Assembly language for it?

If yes, then inside Assembly what do I need require to understand first? And where should I go for Assembly (NASM, MASM etc..)?

Help would be appreciated!

rene
  • 41,474
  • 78
  • 114
  • 152
Raj kumar
  • 117
  • 2
  • 7
  • I think "how do I write my own operative system and also a boot loader for it" is too broad a topic for SO. – Lundin Mar 11 '15 at 14:15
  • @Lundin I've a little doubt ..please clear to me, What programming language will be used here to boot program? `C`, C++ or Assembler with NASM or MASM? – Raj kumar Mar 11 '15 at 14:17
  • 1
    @Rajkumar sorry to burst your bubble here, but the very fact that you ask "how to do it" means that you don't have a deep enough understanding of computer systems to be able to do it. Usually you would use a OS to run your process after booting. Writing your own boot loader is a totally different matter – Pandrei Mar 11 '15 at 14:25
  • 2
    @Pandrei a "hello world" bootloader is no more than a few lines of assembly. The question is actually pretty straightforward. – Alfred Bratterud Mar 11 '15 at 14:50
  • @AlfredBratterud: A few lines of assembly, an explanation of BIOS interrupts / video memory, an explanation of the on-disk MBR, an explanation of how to get your code in there without wiping out your partition table, and an explanation of how to get back into your OS, and it still wouldn't get the OP anywhere. Linking to OSDev and closing the question was basically the only sane thing to do. ;) (Besides, I suspect the OP wrote his "Hello world" using ``...) – DevSolar Mar 11 '15 at 14:53
  • @DevSolar I get your point, but well - it's about 35 lines of assembler code. – Alfred Bratterud Mar 11 '15 at 15:02
  • You might want to Google "writing a bootloader in C". But one promising hit is [Writing a Bootloader in Assembly and C - Part 1](http://www.codeproject.com/Articles/664165/Writing-a-boot-loader-in-Assembly-and-C-Part). – lurker Mar 11 '15 at 16:45
  • https://en.wikipedia.org/wiki/IncludeOS – CW Holeman II Jan 18 '20 at 18:52
  • @AlfredBratterud I'm linking your talk from CppCon 2017 for everyone to enjoy ["Deconstructing the OS: The devil's In the side effects"](https://youtu.be/h7D88U-5pKc?t=85) – hdorio Oct 22 '20 at 16:06

1 Answers1

9

You could do this by writing a simple bootloader. This OSdev article shows you one way to go about it.

Regarding languages: Any compiled language (C/C++) gets compiled into machine code, which again is 1-1 mappable to assembly instructions. So, in principle you could write most of the bootloader in C/C++.

Printing: The challenge with "booting your own code" is of course that you won't have any drivers or any standard library (so, no printf-function or cout). In x86, however, certain parts of low memory (starting at 0xa0000) is directly mapped to video memory when you boot, meaning that the bytes you write to this part of memory will atuomagically appear on the screen, as text.

Choice of assembler: This is really only a matter of taste. For a simple assembly-language bootloader, you'll want to avoid any particular formatting of the resulting binary. nasm -f bin myfile.asm -o myBootsector will just assemble the code into a raw binary.

This post has more details.

Community
  • 1
  • 1
Alfred Bratterud
  • 523
  • 1
  • 4
  • 12
  • Please clear this doubt to me, What programming language will be used here to boot? `C`, `C++` or `Assembler with MASM or NASM`? Please let me know? – Raj kumar Mar 11 '15 at 14:13
  • When you boot, the CPU sees machine code - just like compiled C/C++. – Alfred Bratterud Mar 11 '15 at 14:20
  • So, Can i go for C/C++ instead learning `Assembler`? – Raj kumar Mar 11 '15 at 15:03
  • 3
    @Rajkumar: Please *do* follow the first link in the answer. [OSDev](http://wiki.osdev.org/Main_Page) is a website *dedicated* to those questions you are asking. – DevSolar Mar 11 '15 at 15:05
  • 1
    You will need to attach a boot-signature at the end of the boot sector, and to write your own "print"-function. It should be possible with pure C, but I would recommend learning the little assembler it takes. If you're doing any thing more than "Hello world" in your kernel, you'll need it. – Alfred Bratterud Mar 11 '15 at 15:08
  • @AlfredBratterud `I would recommend learning the little assembler it takes.` So, Can i write my assembly with `C` ? – Raj kumar Mar 11 '15 at 15:24
  • @Rajkumar bite the bullet and learn some assembly. It's not that hard. If you want to get into this kind of programming, you need it for the lowest level stuff. I'm not sure I know what you mean by, *can I write my assembly in C*. If you could, then it would be C, not assembly. You could write certain things in C then generate the assembly, but then you're faced with hand-editing the assembly to handle the parts that C doesn't know about, or to optimize or whatever. – lurker Mar 11 '15 at 16:48
  • @Rajkumar adding the boot signature `0xAA55`, requires modifying the last two bytes, in a 512 byte binary. So you A) have to get the binary to that exact size, and B) set the last two bytes. With `nasm`, this is easily done with: `times 510-($-$$) db 0 ` followed by `dw 0xAA55`. With C/C++ - I don't know (you could of course write a little tool for it.) – Alfred Bratterud Mar 11 '15 at 16:55