some thing about printing string

Programming, for all ages and all languages.
Post Reply
sebastian93921
Posts: 15
Joined: Thu Jan 12, 2012 7:44 pm

some thing about printing string

Post by sebastian93921 »

I'm the beginner or OSdev
but i having a problem in kernel in C language.

Code: Select all

int k_main(void)
{
	kclrscr();
	kprint("Protected Mode process ...\n");
	kprint("First prototype - Sebastian Ko \n\nDate: 12/01/2012\n");
	kprintc("\nNO \\t or value type \n",GRAY_TXT);
	
	kprint("Register checking ... ");
	if(reg() == 0)kprintc("OK\n",GRAY_TXT);
	else kprintc("Fail\n",GRAY_TXT);
	
        return 0;
}
while my kernel is type as above, it will show this output
1.PNG
but while i add the same code with copying kprint("First prototype - Sebastian Ko \n\nDate: 12/01/2012\n"); twice next to the same sentence.
the output will becomes this
2.PNG
Can any people help me ?? :oops:

the Method of kprint and kprintc

Code: Select all

unsigned int line = 0;							//Line in screen (Text)
unsigned int text_pos = 0;						//Text position


//Easiest print method
void kprint(char *message){kprintc(message,0x0B);};


void kprintc(char *message,double text_color)	//Print with change default color
{
	char *vidmem 	= (char *) 0xb8000;			//define video memory
	unsigned int i	= 0;						//define memory position

	i=((line*80*2)+(text_pos*2));				//Memory MUST be line * colume * TxtSpace (160 Means Over Screen of line,line++)	
	
	while(*message!=0)
	{
		if(line > CONSOLE_MAX_LINE)line = 0;
		
		//\n \r \t is 1 char
		if(*message == '\n') 					//Check for a new line
		{	
			text_pos = 0;						//reset text position
			line++;
			i=(line*80*2);
			*message++;
		}else if(*message == '\r'){				//Return to head from line
			text_pos = 0;						//reset text position
			i = (line*80*2);
			*message++;
		}else{									//print Message (Char)
			vidmem[i]=*message;
			*message++;
			i++;
			vidmem[i]=text_color;
			i++;
			text_pos++;
		}
	}
};
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: some thing about printing string

Post by bluemoon »

Code: Select all

*message++;
Are you sure?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: some thing about printing string

Post by bluemoon »

Yes it works and harmless, but for one engaging OSDev one should not mix up message++ with *message++. For the size of kernel codebase, this habit may lead to unmaintainable code. #-o
sebastian93921
Posts: 15
Joined: Thu Jan 12, 2012 7:44 pm

Re: some thing about printing string

Post by sebastian93921 »

THX for reply :D
i fix the *message++ with no pointer on it now :D
it works!!

and double color I changed to "unsigned int color"
would it be correct?

and why I use message++ but don't use *(message++) since message is a array ?? :(

THX :)
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: some thing about printing string

Post by bluemoon »

I kindly advice to read the C++ reference for the difference between message++ and *message++.
sebastian93921
Posts: 15
Joined: Thu Jan 12, 2012 7:44 pm

Re: some thing about printing string

Post by sebastian93921 »

berkus wrote:
sebastian93921 wrote:and why I use message++ but don't use *(message++) since message is a array ?? :(
message is not an array. It's a pointer. And since you need to learn up what a pointer is, I call for missing Required_Knowledge. It would be beneficial for you to program some applications in C first. You're bound to fail with kernel development.
i know this is a pointer, but the pointer can use message++, such as the message[i++] :(

now i have the problem with changed the code of kprint() with your advice.
but when i executing the kernel by these code.

Code: Select all

int k_main(void)
{
	kclrscr();
	kprint("Anti-Web OS - Protected Mode process ...\n");
	kprint("First prototype - Sebastian Ko \n");
	kprint("First prototype - Sebastian Ko \n");
	kprint("First prototype - Sebastian Ko \n");
	kprint("First prototype - Sebastian Ko \n");
	kprint("First prototype - Sebastian Ko \n");
	kprint("First prototype - Sebastian Ko \n");
	kprint("\nDate : ");kprint(COM_DATE);kprint("\n");
	kprintc("\nNO \\t or value type \n",GRAY_TXT);
	
	kprint("Register checking ... ");
	if(reg() == 0)kprintc("OK\n",GRAY_TXT);
	else kprintc("Fail\n",GRAY_TXT);
	
	//for(;;)kgetChar();
	
}
it will having the same problem, like this
1.PNG
is it the problem of the stack pointer of ESP?
how can it fix?

THX :)
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: some thing about printing string

Post by VolTeK »

I have never used qemu, but i believe it has a debugger.

Problems like those can be solved by you debugging. In fact it will solve ther other 40% (or more) problems you will have in future. Look into it
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: some thing about printing string

Post by gravaera »

Man, these printf things are so hard to get right. Printf too ADVANCED D:

EDIT: I have a solution, we should dumb things down a bit, like make a multiple-choice version of printf where you choose from a set of predefined messages and distribute it as a library.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
sebastian93921
Posts: 15
Joined: Thu Jan 12, 2012 7:44 pm

Re: some thing about printing string

Post by sebastian93921 »

gravaera wrote:Man, these printf things are so hard to get right. Printf too ADVANCED D:

EDIT: I have a solution, we should dumb things down a bit, like make a multiple-choice version of printf where you choose from a set of predefined messages and distribute it as a library.
Can you explain more ?? :D
THX
sebastian93921
Posts: 15
Joined: Thu Jan 12, 2012 7:44 pm

Re: some thing about printing string

Post by sebastian93921 »

i know the problems now, when i set the floppy drive(load disk?) in bootloader, i just set 2 of sector need to be read in disk :(
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: some thing about printing string

Post by VolTeK »

Did you write your bootloader.
Post Reply