printing text in protected mode (not displaying correctly)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

printing text in protected mode (not displaying correctly)

Post by mike20123 »

hi guys.
i am facing a problem that every time i print text on the screen it is not displayed correctly.
my kernel is in C and i am using GRUB Bootloader
my code:-
kernel.c

Code: Select all

 void outportb(unsigned port, unsigned val);
 unsigned inportb(unsigned port);
void k_clear_screen();
void update_cursor(int row, int col);
 char get_ascii( char sc);
void write_string(int colour, const char *string);
//----------------------------------------------------------------

 
void kmain()//**main function**
{   
 k_clear_screen();
   update_cursor(0,0);
 
 write_string(7,"Hello World");
      

}

 
 
void write_string(int colour, const char *string)
{
	volatile char *video=(volatile char*)0xB8000;
	while(*string!=0)
	{
		*video=*string;
		string++;
		video++;
		*video=colour;
		video++;
	}
}

//-------------------------------------------------------------------------------------
 char get_ascii( char sc){
 if(sc==1)
 return 27;//esc
else if(sc==2)
return '1';
else if(sc==3)
return '2';
else if(sc==4)
return '3';
else if(sc==5)
return '4';
else if(sc==6)
return '5';
else if(sc==7)
return '6';
else if(sc==8)
return '7';
else if(sc==9)
return '8';
else if(sc==10)
return '9';
else if(sc==11)
return '0';
//-----------------------------
return 0;
}
 
 void update_cursor(int row, int col)
 {
    unsigned short position=(row*80) + col;
 
    // cursor LOW port to vga INDEX register
    outportb(0x3D4, 0x0F);
    outportb(0x3D5, (unsigned char)(position&0xFF));
    // cursor HIGH port to vga INDEX register
    outportb(0x3D4, 0x0E);
    outportb(0x3D5, (unsigned char )((position>>8)&0xFF));
 }
void k_clear_screen()
{
    char* vidmem = (char*) 0xb8000;
    unsigned int i=0;
    while(i<(80*2*25))
    {
        vidmem[i] = ' ';
        ++i;
        vidmem[i] = 0x07;
        ++i;
    };
}; 

 
   unsigned inportb(unsigned port)
{
    unsigned ret_val;
    asm volatile ("inb %w1,%b0"    : "=a"(ret_val)    : "d"(port));
    return ret_val;
}

  void outportb(unsigned port, unsigned val)
{
    asm volatile ("outb %b0,%w1" : : "a"(val), "d"(port));
}
output:-
Image
i am using GCC Complier
Kernel format is PE
Attachments
screenshot.png
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: printing text in protected mode (not displaying correctl

Post by Nessphoro »

Does Hello World appear in the kernel?
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: printing text in protected mode (not displaying correctl

Post by mike20123 »

thanks for your reply.
actually i don't know :oops: :roll:
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: printing text in protected mode (not displaying correctl

Post by Nessphoro »

Well, uh, show me your linker script.
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: printing text in protected mode (not displaying correctl

Post by mike20123 »

okay
linker.ld

Code: Select all

ENTRY (_kmain)
SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

    .rodata ALIGN (0x1000) : {
        *(.rodata)
    }

    .data ALIGN (0x1000) : {
        *(.data)
    }

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: printing text in protected mode (not displaying correctl

Post by Nessphoro »

Interesting, rodata is in place

http://www.jamesmolloy.co.uk/tutorial_h ... creen.html

Try that code out.
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: printing text in protected mode (not displaying correctl

Post by mike20123 »

Thank you Guys,it works 8) :lol:
thank you Nessphoro
Post Reply