Page 1 of 1

Global variable will not initialize correctly

Posted: Mon May 16, 2011 9:03 pm
by ngriff
Hi,

I have a very simple k_main() that clears the screen and prints "kernel loaded" when it is called... the print() and clr() functions are in a separate file. The function is called correctly and the clr() function works as it should, but print() does not seem to do anything at all. What isn't working here?

Code: Select all

const char *msg = "kernel loaded.";
void k_main()
{
	clr();
	print(msg);
	idt_install();
	for(;;);
}
in the file containing clr() and print():

Code: Select all

void print(const char *_message)
{
  unsigned short cr_pos;
  unsigned long i;
  char *vidmem = (char *)0xB8000;

  // Read cursor position
  out(0x3D4, 14);
  cr_pos = in(0x3D5) << 8;
  out(0x3D4, 15);
  cr_pos |= in(0x3D5);

  vidmem += cr_pos*2;
  unsigned short cr_x = cr_pos % 80;
  unsigned short cr_y = (cr_pos - cr_x)/80;

  // Continue until we reach null character
  i = 0;
  while (_message[i] != 0)
  {
	if (_message[i] == 10)
	{
		cr_y++;
		if (cr_y == 25)
		{
			scroll();
			cr_y = 24;
		}
		cr_x = 0;
		unsigned int m = (unsigned int)vidmem;
		vidmem += (160 - ((m - 64) % 160));
		i++;
		continue;
	}
    *vidmem = _message[i];
	i++;
	vidmem += 2;
	cr_x++;
	if (cr_x == 80)
	{
		cr_y++;
		if (cr_y == 25)
		{
			scroll();
			cr_y = 24;
		}
		cr_x = 0;
	}
  }
thanks

Re: Global variable will not initialize correctly

Posted: Tue May 17, 2011 12:37 am
by Combuster
How does the FAQ not apply to you?

Re: Global variable will not initialize correctly

Posted: Tue May 17, 2011 2:21 am
by Solar
Hint: You're looking for Strings do not work, most likely.

Re: Global variable will not initialize correctly

Posted: Tue May 17, 2011 4:38 am
by ngriff
actually, that is not the problem:

in link.ld:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(_start)
phys = 0x1000;
SECTIONS
{
  .text : AT(phys)
  {
    code = .;
    *(.text)
    *(.rodata*)
    *(.rdata*)
  }

...
and i can see the string in the binary also.

Re: Global variable will not initialize correctly

Posted: Tue May 17, 2011 5:35 am
by Combuster
I noticed you don't set the attribute byte. It is not unlikely that you are printing black-on-black text.

Re: Global variable will not initialize correctly

Posted: Tue May 17, 2011 11:12 am
by ngriff
I would agree except that the cursor is not repositioned, and the clr() function sets the attribute byte to 0x0F.

Re: Global variable will not initialize correctly

Posted: Tue May 17, 2011 1:07 pm
by ngriff
sorry, i didn't post a last bit of code in the print() function which calls mov_cr():

Code: Select all

void mov_cr(unsigned short x, unsigned short y)
{
  unsigned short position;

  position = 80*y + x;

  // Set new cursor position
  out(0x3D4, 15);
  out(0x3D5, (unsigned char)(position));
  out(0x3D4, 14);
  out(0x3D5, (unsigned char)(position >> 8));
}
I dont think the printing code is the problem, though. I tried setting the variable directly in the print function:

Code: Select all

void print(const char *_message)
{
  _message = "kernel loaded";
...
and now I get a capital S and a space instead. I have no idea where that came from.

Re: Global variable will not initialize correctly

Posted: Wed May 18, 2011 7:17 pm
by ngriff
good call. I eventually realized that I had a really stupid mistake in my link script, which caused the pointer address to be incorrect.