It will help greatly to go into this guide with some understanding of C and Assembly.
Before you start you will need to install nasm, genisoimage and may want to install qemu or bochs in order to test your OS without rebooting your computer, bochs is preferred as it has more debugging capability than qemu
By the end of this part you should have a very basic OS that can boot. This part of the guide will not cover anything but making a basic OS that does absolutely nothing due to how long it already is. If people like this I will make a part 2 going over how to use C with the OS and write text to the screen.
Booting
The first thing to do is create the main assembly file, I will call it Boot.asm
global entry
MAGIC equ 0x1BADB002 ; These tell the bootloader that we are bootable
FLAGS equ 0
SUM equ -MAGIC
section .text:
align 4 ; Align everything after this to a 4 byte boundary, which the boot header needs to be aligned to
dd MAGIC ; dd means "define double word", a word is usually 2 bytes on most computers, so a dword is 4 bytes. You can think of a word as being a short in C and a dword being an int.
dd FLAGS
dd SUM
entry:
jmp entry ; For now we won't do anything but loop forever.
This should boot and do nothing.
But how do we compile it?
First we need a linker script since grub(the bootloader this tutorial will be using) is loaded below 1 MB so we should load above that!
Now I don't expect you to know how to do this, and neither do I, so I will copy this from littleosbook which is another good tutorial on writing an operating system from scratch.
ENTRY(entry) /* the name of the entry label */
SECTIONS {
. = 0x00100000; /* the code should be loaded at 1 MB */
.text ALIGN (0x1000) : /* align at 4 KB */
{
*(.text) /* all text sections from all files */
}
.rodata ALIGN (0x1000) : /* align at 4 KB */
{
*(.rodata*) /* all read-only data sections from all files */
}
.data ALIGN (0x1000) : /* align at 4 KB */
{
*(.data) /* all data sections from all files */
}
.bss ALIGN (0x1000) : /* align at 4 KB */
{
*(COMMON) /* all COMMON sections from all files */
*(.bss) /* all bss sections from all files */
}
}
nasm -f elf32 Boot.asm should create a file called Boot.o
Which we can then turn into an elf binary by linking it with ld
@StudentFires You could probably do it in C++ too but that will only add more ways for things to go wrong, using C++ in os dev is really only useful if you are addicted to classes
A beginners guide to OS Dev: Part 1
It will help greatly to go into this guide with some understanding of C and Assembly.
Before you start you will need to install nasm, genisoimage and may want to install qemu or bochs in order to test your OS without rebooting your computer, bochs is preferred as it has more debugging capability than qemu
By the end of this part you should have a very basic OS that can boot.
This part of the guide will not cover anything but making a basic OS that does absolutely nothing due to how long it already is.
If people like this I will make a part 2 going over how to use C with the OS and write text to the screen.
Booting
The first thing to do is create the main assembly file, I will call it
Boot.asm
This should boot and do nothing.
But how do we compile it?
First we need a linker script since grub(the bootloader this tutorial will be using) is loaded below 1 MB so we should load above that!
Now I don't expect you to know how to do this, and neither do I, so I will copy this from littleosbook which is another good tutorial on writing an operating system from scratch.
nasm -f elf32 Boot.asm
should create a file called Boot.oWhich we can then turn into an elf binary by linking it with ld
ld -T link.ld -melf_i386 Boot.o -o kernel
We then must download grub, which can also be found in littleosbook, http://littleosbook.github.com/files/stage2_eltorito
Now we create the folder structure our os iso will be built from
And our grub configuration to put in /boot/grub/menu.lst
Now to run it!
A bash script to automatically compile/link and run the OS
To use bochs you must make a configuration file named bochsrc
Since you are using an ELF executable, what platform does this work for? ARM? x86? x64? AMD?
@AmazingMech2418 no idea, I only have intel computers
@CSharpIsGud I think x86 then.
@CSharpIsGud Also, I don't see any C in this tutorial. Will that be in the second part?
@AmazingMech2418 yeah
@CSharpIsGud By "C," you're referring to C++, right... right!?
@StudentFires No. C.
@StudentFires You could probably do it in C++ too but that will only add more ways for things to go wrong, using C++ in os dev is really only useful if you are addicted to classes
@CSharpIsGud Isn't Java meant for those who are addicted to classes?
@StudentFires Pretty much, but it still can be useful for some things like Android development (Kotlin is used there too).
@StudentFires or C#, im not addicted to classes but I like having vaguely C like syntax without having to worry about memory