Global variable will not initialize 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
ngriff
Posts: 5
Joined: Mon May 16, 2011 8:20 pm

Global variable will not initialize correctly

Post 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
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:

Re: Global variable will not initialize correctly

Post by Combuster »

How does the FAQ not apply to you?
"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 ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Global variable will not initialize correctly

Post by Solar »

Hint: You're looking for Strings do not work, most likely.
Every good solution is obvious once you've found it.
ngriff
Posts: 5
Joined: Mon May 16, 2011 8:20 pm

Re: Global variable will not initialize correctly

Post 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.
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:

Re: Global variable will not initialize correctly

Post by Combuster »

I noticed you don't set the attribute byte. It is not unlikely that you are printing black-on-black text.
"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 ]
ngriff
Posts: 5
Joined: Mon May 16, 2011 8:20 pm

Re: Global variable will not initialize correctly

Post by ngriff »

I would agree except that the cursor is not repositioned, and the clr() function sets the attribute byte to 0x0F.
ngriff
Posts: 5
Joined: Mon May 16, 2011 8:20 pm

Re: Global variable will not initialize correctly

Post 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.
ngriff
Posts: 5
Joined: Mon May 16, 2011 8:20 pm

Re: Global variable will not initialize correctly

Post 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.
Post Reply