Gettings started with operating system development.

Welcome to the world of operating system development. In this tutorial you will learn how to make a simple operating system that will boot and print to the screen.

First we need to create a file that will setup the environment for higher level programming languages like C or C++.grub needs a multi boot header to use your kernel. This Multiboot header must contain 3 fields that are 4 byte aligned namely.
The Magic number is 0x1BADB002 and the flag is 0x00.

This file will be called boot.asm



;nasm directive - 32 bit
bits 32
section .text
        ;multiboot spec
        align 4
        dd 0x1BADB002            ;magic
        dd 0x00                  ;flags
        dd - (0x1BADB002 + 0x00) ;checksum. m+f+c should be zero

global start
extern kmain	        ;kmain is defined in the c file

  cli 			;block interrupts
  mov esp, stack_space	;set stack pointer
  call kmain
  hlt		 	;halt the CPU

section .bss
resb 8192		;8KB for stack



Now we need the main kernel code. In this code we will allow printing to the screen.

This file will be called kernel.c


void kmain(void)
	const char *str = "my first kernel!!!";
	char *vidptr = (char*)0xb8000; 	//video mem begins here.
	unsigned int i = 0;
	unsigned int j = 0;

	/* this loops clears the screen so we will not have lots of stuff that we don't want
	* there are 25 lines each of 80 columns; each element takes 2 bytes */
	while(j < 80 * 25 * 2) {
		vidptr[j] = ' '; 
		vidptr[j+1] = 0x07; 		
		j = j + 2;

	j = 0;

	/* this loop writes the string to video memory */
	while(str[j] != '\0') {
		/* the character's ascii */
		vidptr[i] = str[j];
		/* attribute-byte: give character black bg and light grey fg */
		vidptr[i+1] = 0x07;
		i = i + 2;

If you want different colors you can do that.

For example if we have the color code 0x07 this means the background is black and the foreground is light grey.

The 0 is the background color and the 7 is the foreground color.

So if you want a black and green terminal you will have 0x02 as the code.


Color table

0 - Black, 1 - Blue, 2 - Green, 3 - Cyan, 4 - Red, 5 - Magenta, 6 - Brown, 7 - Light Grey, 8 - Dark Grey, 9 - Light Blue, 10/a - Light Green, 11/b - Light Cyan, 12/c - Light Red, 13/d - Light Magenta, 14/e - Light Brown, 15/f – White.


Now we have some code we need to link these two files together. This file will be called link.ld.


*  link.ld
   . = 0x100000;
   .text : { *(.text) }
   .data : { *(.data) }
   .bss  : { *(.bss)  }

Now we have all this code how are we going to make a operating system with this. We will have to compile the this code. To compile the kernel.c file use.

gcc -m32 -c kernel.c -o kc.o

Now we need to compile boot.asm

nasm -f elf32 kernel.asm -o kasm.o

Now we need to link these files.

ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o

Now we have the kernel compiled now we have to make an iso so we can boot to are new operating system.

First create an iso directory which all the files will go in

We now need to create a directory called boot. Put the kernel file into this boot directory.

now create a grub directory in the boot directory.

Now create a file called grub.cfg and put it into the grub directory.

menuentry 'My os version 1'{
    multiboot /boot/kernel

Your file system should look like this.

iso --
  boot --
           grub --

Now we need to create a iso from this with

grub-mkrescue -o myos.iso iso

Now you are done.

Leave a Reply

Your email address will not be published. Required fields are marked *