Can only print so many lines

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
lurner
Posts: 18
Joined: Mon Jan 07, 2008 12:09 am

Can only print so many lines

Post 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.
blound
Member
Member
Posts: 70
Joined: Sat Dec 01, 2007 1:36 pm

Post 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
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post 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
lurner
Posts: 18
Joined: Mon Jan 07, 2008 12:09 am

Post 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.
lurner
Posts: 18
Joined: Mon Jan 07, 2008 12:09 am

Post 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.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply