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();
}

No comments:

Post a Comment