Page 1 of 1

Can only print so many lines

Posted: Mon Jan 07, 2008 12:16 am
by lurner
Hello, this is my first of many posts here while I learn how to develop this toy OS I am working on. So, hello to everyone in advance.

My question is this: I have a boot loader, that switches to Pmode and then loads my kernel. Now I am mixing my OS with both ASM and C code, where C code is my kernel language. I am working ona Win32 platform using DJGPP to compile my C code and NASM for my ASM code.

OK, so I am just trying to print some verbiage to the screen, (Welcome Screen), but after I get to > 8 lines, it starts printing out garbage, if I take it a line or two further than that it crashes the system.

Here is my Kernel code (Main):

Code: Select all

const char *m_menu;
const char *m_decp_1;
const char *m_decp_2;
const char *m_decp_3;
const char *m_decp_4;
const char *m_decp_5;
const char *m_decp_6;

void main()
{
  cls();
  ccls(0x70);
  print(0x70, m_menu,53/2,1);	//<-- prints just fine 
  print(0x70, m_decp_1,13/2,3);	//<-- prints just fine
  print(0x70, m_decp_2,13/2,4);	//<-- prints just fine
  print(0x70, m_decp_3,13/2,5);	//<-- prints just fine
  print(0x70, m_decp_4,13/2,7);	//<-- prints just fine
  print(0x70, m_decp_5,13/2,8);	//<-- prints just fine
  print(0x70, m_decp_6,13/2,9);	//<-- prints garbage at this point

  for(;;);	//infinite loop
}

const char *m_menu = "My Toy OS";
const char *m_decp_1 = "blah blah blah, etc...";	//67 wide
const char *m_decp_2 = "blah blah blah, etc...";
const char *m_decp_3 = "blah blah blah, etc...";
const char *m_decp_4 = "blah blah blah, etc...";
const char *m_decp_5 = "blah blah blah, etc...";
const char *m_decp_6 = "blah blah blah, etc...";
I am sure there is a smarter way to do this, and I would love to see it if possible.

My Print Function looks like:

Code: Select all

void print(int color, char *string, int x, int y)
{
   char *vidmem=(char *)0xB8000;
   unsigned int i = (160 * y + 2 * x);
   while(*string!=0)
   {
      vidmem[i]=*string;
      string++;
      i++;
      vidmem[i]=color;
      i++;
   };
}
I hope you guys can help me out with this problem, perhaps even show me a better way to write the print function and then use it in main. All help is much appreciated. Thanks in advance.

Posted: Mon Jan 07, 2008 12:55 am
by blound
I would suggest reading this:
http://www.osdever.net/bkerndev/Docs/printing.htm

also in your main file you pasted you declare all the variables at the top then redeclare and assign them after main...

in your print function i am not sure where the 160 comes from ( did you mean 80) ?

also the way you deal with vidmem is incorrect unless there is something really subtle going on..

just read the link i pasted and it will clear everything up

Posted: Mon Jan 07, 2008 2:27 am
by JamesM
OK, so I can't actually see from a first glance what is wrong with your print function.

But, are you certain the print function is at fault? You use a different const char* string for each line - why not try printing the *same* string on each line? then if it is something stupid like your bootloader not loading in enough of the kernel (and missing out some of the .rodata section) then you'll know about it.

I would also suggest rewriting print() as:

Code: Select all

void print(unsigned char color, char *string, int x, int y)
{
  char *vidmem = (char *)0xB8000;
  unsigned int i = 160*y + 2*x;
  while (*string)
  {
    vidmem[i++] = *string++;
    vidmem[i++] = color;
  } // Notice no semicolon
}
Cheers,

James

Posted: Mon Jan 07, 2008 4:01 am
by jal
JamesM wrote:I would also suggest rewriting print() as:

Code: Select all

void print(unsigned char color, char *string, int x, int y)
{
  char *vidmem = (char *)0xB8000;
  unsigned int i = 160*y + 2*x;
  while (*string)
  {
    vidmem[i++] = *string++;
    vidmem[i++] = color;
  } // Notice no semicolon
}
I would even suggest a for-loop and putting the offset in the pointer:

Code: Select all

void print(unsigned char color, char *string, int x, int y)
{
  char *vidptr = (char *)(0xB8000 + 160*y + 2*x);
  int i;

  for (i = 0; string[i] != 0; i++)
  {
    vidptr++ = string[i];
    vidptr++ = color;
  }
}

JAL

Posted: Mon Jan 07, 2008 4:57 pm
by lurner
Jal: thanks for the reply, but your suggestion failed to compile.

JamesM: I did what you said and it did print just fine.

Code: Select all

const char *m_menu;
void main() {
  int i;
  for(i=0; i<25; i++) {
  print(0x70, m_menu,53/2,i);	//print title in middle of screen 
  }
}
const char *m_menu = "My Toy OS";
I also, used your suggested print code. So, what can I do to fix this, because I would like to be able to print to the screen more than one sentence, but don't know how to do that and still be able to make newlines, etc?

Thanks in advance.

Posted: Tue Jan 08, 2008 12:19 am
by lurner
Hey guys, I fixed my problem, added a newline functionality to my printf function, so this is is solved. Thanks a lot for your help.

Posted: Tue Jan 08, 2008 2:45 am
by jal
lurner wrote:Jal: thanks for the reply, but your suggestion failed to compile.
Ah, always the problem when typing code by heart. Next time I'll make sure it compiles :).


JAL

Posted: Tue Jan 08, 2008 11:25 am
by Combuster
jal wrote:Next time I'll make sure it compiles :).
IMHO The key to providing code is that others learn from it. Not copying it without understanding it. Hence an occasional compile error should not stop anybody from taking a lesson out of it.