Page 1 of 1

ram detection/printing troubles

Posted: Sun Jul 10, 2011 4:14 pm
by fireshadow4126
Hello,

I'm trying to get into memory management in my OS, but I obviously need to detect the RAM first. I followed http://wiki.osdev.org/Detecting_Memory_%28x86%29, but I get this:

Image

I would suspect that some variable somewhere went uninitialized, but I can't seem to find it.
Here is my main function and uitoa (unsigned int to ascii) function:

Code: Select all

int main(multiboot_info_t* mbt, unsigned int magic)
{
	multiboot_memory_map_t* mmap = mbt->mmap_addr;
	while ((unsigned int)mmap < mbt->mmap_addr + mbt->mmap_length) {
		mmap = (multiboot_memory_map_t*) ((unsigned int)mmap + mmap->size + sizeof(unsigned int));
	}
   /* irrelevant code removed */
   unsigned char atest[20] = { 0 };
	uitoa(mmap->length_low, atest);
	puts(atest);
	puts(" + ");
	uitoa(mmap->length_high, atest);
	puts(atest);
	puts(" RAM available");
        ...
}

void uitoa(unsigned int value, unsigned char number[])
{
	unsigned char t[12];
	int i, j;
	i = 0;
	do {
		number[i++] = value % 10 + '0';
	} while ((value /= 10) > 0);
	number[i] = '\0';

	strcpy(t, number);
	for (i = 0, j = strlen(number) - 1; j >= 0; i++, j--)
	{
		number[i] = *(t + j);
	}
}
Thanks in advance!

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 4:25 pm
by bluemoon
Hints:

Code: Select all

while ((unsigned int)mmap < mbt->mmap_addr + mbt->mmap_length) {
      mmap = (multiboot_memory_map_t*) ((unsigned int)mmap + mmap->size + sizeof(unsigned int));
}
You loop thru' the table without doing anything, and you do things when mmap is outside the table.

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 4:35 pm
by fireshadow4126
That kinda made it even worse:

Image

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 4:50 pm
by bluemoon
That does not imply no bug there, but fixing one bug provide opportunity for other bugs to show up.
it mean more bugs to fix.

Since the dump shows a lot of text, you should check your puts function - try print some static text.
It seems either missing zero-terminate on your text message, or you can't stop on the puts.

The next thing to check is the uitoa() function, try feed it with some know numbers,
Does the value converted correctly? Do the string zero-terminated?

You don't need mmap at this moment until you have the above functions bug-free.

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 5:04 pm
by fireshadow4126
It's not uitoa(), because I can print a loop of 0 to 100 just fine, so I would guess that it's in puts(). I have no clue though, so here's that function, along with putch(). Note: 0xF2 is my value for Delete key, 0xF3 for Left Arrow key, 0xF4 for Right Arrow key:

Code: Select all

int printLen;

void putch(unsigned char c)
{
	unsigned short *where;
	unsigned att = attrib << 8;

	if (c == 0x08)
	{
		if (csr_x != 0)
		{
			csr_x--;
		}
	}
	else if (c == 0x09)
	{
		csr_x = (csr_x + 8) & ~(8 - 1);
	}
	else if (c == '\r')
	{
		csr_x = 0;
	}
	else if (c == '\n')
	{
		csr_y++;
		csr_x = 0;
		printLen = 0;
	}
	else if (c == 0xF2)
	{
		putch(' ');
		putch('\b');
		csr_x++;
	}
	else if (c == 0xF3)
	{
		if (csr_x > 0)
		{
			csr_x--;
		}
	}
	else if (c == 0xF4)
	{
		if (csr_x < 80)
		{
			csr_x++;
		}
	}
	else if (c >= ' ')
	{
		printLen++;
		where = textmemptr + (csr_y * 80 + csr_x);
		*where = c | att;
		csr_x++;
	}
	
	if (csr_x >= 80)
	{
		csr_x = 0;
		csr_y++;
	}
	scroll();
	move_csr();
}

void puts(unsigned char *text)
{
	int i;
	printLen = strlen(text);
	for (i = 0; i < printLen; i++)
	{
		putch(text[i]);
	}
}

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 5:18 pm
by bluemoon
Friendly advice:

Man, I have already provided some direction on debugging to help you get started.
You, if want to develop an OS, should put some effort on it but not just posting code and ask someone to analysis code and debug for you.

We don't have your exact environment to simulate the bug or test any fix, it will be very low productivity to get fix this way.
On the other hand, I (or anyone) may help you solve the problem in few minutes, but then you won't learnt anything.

Try it yourself, I'll be happy to help you get started and provide direction & insight, but no direct bugfix.

For instant, check the wiki Printing_to_Screen.
And to access video memory you need a volatile pointer.

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 5:39 pm
by fireshadow4126
bluemoon wrote:Friendly advice:

Man, I have already provided some direction on debugging to help you get started.
You, if want to develop an OS, should put some effort on it but not just posting code and ask someone to analysis code and debug for you.

We don't have your exact environment to simulate the bug or test any fix, it will be very low productivity to get fix this way.
On the other hand, I (or anyone) may help you solve the problem in few minutes, but then you won't learnt anything.

Try it yourself, I'll be happy to help you get started and provide direction & insight, but no direct bugfix.

For instant, check the wiki Printing_to_Screen.
And to access video memory you need a volatile pointer.
Sorry, please allow me to clarify: I don't want you or anyone else solving my problems for me so much as I would like someone to offer some explanation as to why the code behaves as it does, so I'll at least have some idea how to fix it myself. Anyway, what do you mean by "or you can't stop on the puts"?

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 5:54 pm
by bluemoon
The output message seems longer than what you attempt to print.
Have you tried:

Code: Select all

puts ("hello world\n");
and see if it does not print anything longer?

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 5:57 pm
by fireshadow4126
Just did that, it works fine.

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 6:14 pm
by bluemoon

Code: Select all

for (i = 0; i < printLen; i++)
{
   putch(text[i]);
}
And you have printLen++ on printable characters.
This way you are printing double the length of the message, consider this:

printLen = strlen("hello"); // 5

For the first 5 characters you advanced printLen, so the puts loop will get thru a total of 10 iteration:
On 6-th: text[5] is 0, and is not printed by putch
On 7 to 10: text is undefined, if it happens to be a printable character, it will be printed, and printLen is advanced even more to overrun more text.

Re: ram detection/printing troubles

Posted: Sun Jul 10, 2011 6:20 pm
by fireshadow4126
Thanks so much, it works now!