Showing posts with label system programming. Show all posts
Showing posts with label system programming. Show all posts

Monday, February 10, 2014

"Hello World" Program!!! (Writing C code!! Day 7) your own printf function

So let us start with a basic printf which will print hello world to the screen.
Remember that you can't use header files which you normally use in C programming (for example stdio.h) as they are not yet supported by the OS of this new system. But there are some header files which come with every compiler and standard across all type of compilers. In this case stddef.h stdint.h are two examples.
There is particular location in memory from which the data which needs to be displayed on the screen is read.
The memory location of this video buffer is stored in "buffer" variable.
Initially the assembly code calls kernel_main and the C code starts calling
Then we initialize screen to look black which is done in the cleanTheScreen() call
After which we call printf() which prints hello world to the screen.
Here you will see a lot of new data types which may be scary who have are still learning C programming. These new data types are used so that we are sure about the amount of space that is going to be allocated for these variables.

Make sure you write your own code which uses concepts explained in this code.
Following are the color code for various colors you can use in place of black and white we are using here in the code. BLACK = 0,
BLUE = 1,
GREEN = 2,
CYAN = 3,
RED = 4,
MAGENTA = 5,
BROWN = 6,
LIGHT GREY = 7,
DARK GREY = 8,
LIGHT BLUE = 9,
LIGHT GREEN = 10,
LIGHT CYAN = 11,
LIGHT RED = 12,
LIGHT MAGENTA = 13,
LIGHT BROWN = 14,
WHITE = 15,

#include 
#include 
//lets store the screen size in a global variable
static const size_t SCREEN_WIDTH = 80;
static const size_t SCREEN_HEIGHT = 24;
uint8_t screen_color = 0;//0x0
uint8_t text_color = 15;//0xf
uint16_t* buffer = (uint16_t*) 0xB8000;//pointer which points to the display memory. Display reads from this memory

int cleanTheScreen()
{
    size_t row = 0;
    size_t column = 0;
    char text = ' ';
    uint16_t color = screen_color;
    size_t position ;
    while(row < SCREEN_HEIGHT)
    {
        while(column < SCREEN_WIDTH)
        {
             position  = row*SCREEN_WIDTH+column;
             buffer[position] = text|screen_color<<8;
             column++;
        }
        row++;
    }
}
int printf(char* data,)
{
    uint8_t row = 0;
    uint8_t column = 0;
    size_t row = 0;
    size_t column = 0;
    char text = ' ';
    uint16_t color = text_color;
    size_t position ;
    while(column < SCREEN_WIDTH)
    {
         position  = row*SCREEN_WIDTH+column;
         buffer[position] = text|screen_color<<8;
         column++;
    }
}
void kernel_main()
{

 cleanTheScreen();
 printf();
}

Sunday, February 9, 2014

"Hello World" Program!!! (Writing assembly code!! Day 5)

So from now on we will follow the steps mentioned in the previous post. Today we will write a bit of assembly code 

First few questions that came to my mind when I started writing the hello world program were
#)Why do we need assembly code!!??
#)Why cant we just start with C program?
Few reasons for these questions are:
1) You cannot enter protected mode with just C (I mentioned about protected mode in my previous posts).
2) Putting the multiboot header in the right section.
Multiboot header is something which tells the GRUB that  
3) setting up the stack etc. 
If you know other reasons please post them in the comment section.

Assembly code is here. You have to use the code from that page.  Save the code as "boot.s"
Then compile it using the following command. But this code wont compile It has errors. The code has error some where. Hence use this code instead.

#remember that every time you want to compile the code you develop you should set the environment variables in the terminal as given below

export PREFIX="$HOME/opt/cross"
export TARGET=i586-elf  
export TARGET=i586-elf 
export PATH="$PREFIX/bin:$PATH"

# The reason why you do this is because without these the computer will not  know which compiler to use and it will use your default compiler which is not meant to compile code for the new operating system you are developing.
#after these steps execute the following command after you cd to the folder which contains boot.s

i586-elf-as boot.s -o boot.o

For now and I think for a very long time you are free from this assembly language. You don't have to worry about this any more.




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.

Tuesday, December 31, 2013

Warm up!!! (Day 2)


So its day two of the tutorial. I hope that you have completed the installation I had mentioned in the previous tutorials. Today's tutorial would more of reading stuff that matters.

Things you should know about before moving ahead (You just have to understand the basic definitions):

BIOS
BIOS stands for Basic Input/Output System. The BIOS is the firmware in the ROM (read only memory)  of a PC. When the computer is turned on, the BIOS is the first program that runs. All other programs must be loaded into RAM first. The task of a BIOS is to test and initialize the hardware and then it loads the bootloader or the operating system. The BIOS does the following:
  • POST (Power On Self Test).
  • It provides you with a START UP menu where you can set certain  parameters and lets you adjust the real time clock. Most modern BIOS versions let you set the boot order. The boot order is the order in which the bios checks for the devices which may contain the bootloader.For example If the first device in the boot order is USB and if you have a bootable pen-drive in your USB slot of your computer then it boots using the USB. If there was no USB then it will check the next device which can be hard disk.
  • The boot sector loader loads the first 512-byte sector from the boot disk into RAM and jumps to it (i.e. executes the program that was just loaded by it in the RAM). 
  • The BIOS interrupts. These are simple device drivers that programs can use to access the screen, the keyboard and disks. Boot loaders rely on them, most operating systems do not (the Linux kernel does not use BIOS interrupts once it has been started). MSDOS does use BIOS interrupts.
As far as boot loading facilities are concerned, the PC BIOS is very primitive compared to that of other computer systems. The only thing it knows about disks is how to load the first 512-byte sector.
  • The first sector of a diskette can be loaded at address 0000:7C00. The last two bytes of the sector are checked for the values 0x55 and 0xAA. If these are OK, the BIOS jumps to the address 0000:7C00.
  • Booting from a hard disk is very similar to booting from a diskette. The first sector of a hard disk (often called the Master Boot Record) is loaded at 0000:7C00 and next the BIOS jumps to it. The MBR program must move itself to an address that is different from 0000:7C00 as it is supposed to load a different boot sector from a partition to address 0000:7C00 and jump to that.
  • Modern BIOS versions can treat a certain file on a CD-ROM as a diskette image. They pretend to boot from a diskette by loading the first 512 bytes of the file to 0000:7C00 and jumping to it. Every attempt to access the same diskette using the BIOS routines, will be redirected to the image file on CD-ROM. Some other ways to boot a CD-ROM may also be supported (with an emulated hard disk or with no disk emulation at all).
Bootloader
The bootloader  loads the operating system in the ram and gives control to the OS(jumps to OS execution).

Also read about the following:

Sunday, December 29, 2013

Basics (Day 1)

Its time I should write a tutorial on OS development as I faced a lot of problems in trying to find out how to start OSDEVING. The subject is quite interesting, It will improve your Knowledge of C and OSDEVING  exponentially (provided you play around with the code).

###################################   Note     ##################################
#The tutorial is going to be in C. So knowledge of C is necessary. Its not very difficult to learn C. Just refer to C concepts wherever and whenever you don't understand certain part of the code.

#You need to have basic knowledge of ubuntu i.e. how things work! how to use terminal! etc. 
Including this (If you get time):- 
-Quora (this i like the most)

#I am using virtualbox to test my OS and development is being done on UBUNTU 12.04 LTS.
#To install ubuntu on your system follow this.
#To install virtualbox after installing ubuntu, run this command in terminal 
sudo apt-get install virtualbox
#A good code editor for c development is codeblocks.
sudo apt-get install codeblocks

#Obviously this tutorial is going to be very long tutorial as OS cannot be developed overnight. Hence the tutorials is going to be in parts.
#With time you will have to install other softwares. All the necessary instructions will be provided here.
To understand osdeving just don't copy-paste the code but play with it.  
#############################################################################

Thanks to OSDEV. Its a very good place to learn operating system development. 
The initial steps are to be followed from OSDEV Bare Bones Tutorial (very nicely explained). Still I'll help with the initial tutorial too but in the next part of this tutorial.
Before moving ahead please complete all the installation that are specified in the "Note" section above (installing ubuntu and virtualbox are necessary, at least for the sake of this tutorial).