Wednesday, January 8, 2014

"Hello World" Program!!! (Day 4)

This post is quite late as compared to what I had planned. Developing and writing a tutorial is quite time consuming. You'll realize soon that OS development is quite tough and is not a child's play. You have to handle lot of errors which may or may not be discussed in this tutorials. But the satisfaction that you get once your code runs successfully for the first time is immense.

Before you write OS code you should know that you don't have basic functions like "printf" and "scanf". You have to write your own "printf",  "scanf" and many more functions (which you'll find is very interesting and you will learn a lot in this process). Another interesting thing is your program will directly interact with the hardware and you will have full control over the hardware unlike the programs you wrote for your college assignments.

So lets get started with our "hello world" program.

In this tutorial we are going to use GRUB as the boot loader. You can write your own bootloader but it requires a lot of knowledge of assembly language program. As our aim here is to cover more important concepts of OS, hence we will skip this step. If you wish to develop your own bootloader then you can refer to these websites.
1) GRUB official website.
2) OSDEV

GRUB:
GRUB is an excellent bootloader. It greatly simplifies the loading of operating system. It also provides us with many features which can be used in initializing the operating system. We will discuss about these features when we write the C code (step 2).

Steps that we are going to follow!!!
1) Write assembly code
2)  Write C code.
3) Compile them separately using the compiler we created earlier (refer to previous post).
4) Connect these two executable to create one single OS image.
5) Load this OS image on your virtualbox.
6) If everything is fine then you'll see your OS running.

This is how it will look after following all these steps :P



I hope this motivates you to go ahead with the tutorial :P

Thursday, January 2, 2014

Building A cross compiler ( Day 3 )

What is a cross compiler?

A cross compiler is used to create executable for a platform which may be other than the platform on which the compiler is currently running.
To make thing clear:
If I have a machine A(64 bit machine) and I am using a compiler on it for developing OS which will run on machine B(32 bit machine) then the compiler that I am using has to be a cross compiler as it should create executable which will run on machine B.  

Why do you need a cross compiler?

If I use a compiler which runs on machine A(64 bit machine) then the executable produced by it may not run on machine B(32 bit  machine). Because instruction set for 64 bit machine can be differnt from a 32 bit machine. Hence we need a compiler that understands the instruction set which are compatible with machine B.

How do I build a cross compiler for the OS I am developing? 

Steps that must be followed:
1) open your terminal and execute the following commands.
cd ~
mkdir src
2) Download the following files to the "src" folder you just created in the previous step.

  • binutils
    • when you click on the link you will see a list of files. On that page you have to click on the link which is latest (date modified and version is mentioned there). Make sure you don't download a "patch" or ".sig" (Note that file size is in MB). 
    • Note extensions to the file names indicate different compression techniques. All of them when uncompressed give the same file. You can download any compression provided both have same version. The only difference will be how much you download.
  • gcc
    • Its a two step job. First you follow the link. Then in that you click on the link which has latest version. That will lead you to yet another page. There you will get the download link for gcc. Download only one of the file mentioned on that page. Note do not download ".sig" or "diff" files, these files don't have gcc.
  • gmp
  • mpfr
    • click on download and you will find the download link.
  • mpc
    • click on download and you will find the latest release. Download this latest release.
3) Extract all the files you downloaded in the step 2 to the same folder.
4) Execute the following commands:

cd ~
mkdir opt
cd opt
mkdir cross
export PREFIX="$HOME/opt/cross"
export TARGET=i586-elf
export PATH="$PREFIX/bin:$PATH"
cd $HOME/src
mkdir build-binutils
cd build-binutils
#note that "x.y.z" in the next command is its version number you have to replace it with that.
../binutils-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls
make
make install
cd $HOME/src
mv gmp-x.y.z gcc-x.y.z/gmp
mv mpfr-x.y.z gcc-x.y.z/mpfr
mv mpc-x.y.z gcc-x.y.z/mpc
mkdir build-gcc
cd build-gcc
../gcc-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
make all-gcc
make all-target-libgcc
make install-gcc
make install-target-libgcc
#there is high chance you may run into errors during this installation. Post your queries in the comment section or you can google them (obviously). Some times error can be solved by downloading a different file which has the same version but has a different compression (It happened in my case, dont know why!!!).
#If you don't run into any problem then CONGRATULATIONS your compiler is ready.
#in the next part of this tutorial we will write our first "hello world" OS.

#I recommend you folks to go through this page to understand the details. The explanation is good and most of the information in this tutorial is form that page.