Page 1 of 1

Weird execution problem.

Posted: Thu Mar 10, 2011 9:45 pm
by Davidm44
So, I'm having another problem that may be similar to the last one I had.

Explanation:

So I have this function here.

Code: Select all

void ConsoleWrite(char* string,unsigned char color)
{

	unsigned char* videoMemory = (unsigned char*)0xB8000;
	int x = 0;
	int y = 0;

	int i;
	for(i=0;i<strlen(string);i++)
	{	
		if(string[i] == '\n')
		{
			//y++;
			//x=0;
		}

		videoMemory[y*80 + x++] = string[i];
		videoMemory[y*80 + x++] = color;
		
		if(x >= 80)
		{
			x=0;
			y++;
		}

		MoveCursor(x/2,y);
	}

	return;
	
}
As you can see there are 2 commented lines, whenever I remove the comments, the first parameter is basically a 0 and when it points to the elements to print the string it prints random stuff. When I have it commented like that, it prints out normally like it should.

A similar problem, if I declare a function ( not even call it ), this functions execution gets messed up in the same way.

I'm going to assume it's my linker script that doesn't tell the linker how to divide the sections accordingly but I don't really know, I'm still a newbie at this.

Posting a RAR with my source, shell script for assembling/compiling & my linker script, hopefully you guys can take a look and maybe provide a bit of help.

PS: Ignore the useless stuff in boot.asm, I know it's useless, I'm just trying to get this odd problem out of the way before I do anything else.

Thanks.

Re: Weird execution problem.

Posted: Thu Mar 10, 2011 10:00 pm
by Chandra
Add i++ just after y++ and x=0.
Problem solved.

Re: Weird execution problem.

Posted: Thu Mar 10, 2011 10:11 pm
by Davidm44
Chandra wrote:Add i++ just after y++ and x=0.
Problem solved.
The problem is, that part of the code shouldn't be executing, so that disregards your fix.

In my main.c, the string I'm trying to print is "DAVID". But for some reason, un-commenting the code screws everything up.

Re: Weird execution problem.

Posted: Thu Mar 10, 2011 11:02 pm
by Chandra
Hmm... things seems clearer now. Try replacing 'char *string' with 'const char *string' and see if really does make some difference. One thing I noticed is, that portion of code is somehow executing, so you may want to put it at last and see if it still executes. It would help to see your linker script but my browser sucks.

And oh! the previous fix cannot be disregarded. Do you realize if your code was actually working and if your string had a \n, then your code would try to print that \n as well. Anyway, sorry for the fix that doesn't apply at the moment.

Re: Weird execution problem.

Posted: Thu Mar 10, 2011 11:12 pm
by blobmiester
1. You don't want to be writing '\n' to video memory.
2. There are two bytes per character. In example, the formula for indexing into video memory (declared as an array of unsigned char*) is:

Code: Select all

videomemory[(row * rows + column) * 2 + 0] = character;
videomemory[(row * rows + column) * 2 + 1] = attribute;
3. Don't do a 'i < strlen(x)'. Instead check if the current character is a null terminator. Much faster.

Check the wiki for excellent tutorials that explain what I've mentioned above.

Re: Weird execution problem.

Posted: Fri Mar 11, 2011 5:47 am
by Davidm44
blobmiester wrote:1. You don't want to be writing '\n' to video memory.
2. There are two bytes per character. In example, the formula for indexing into video memory (declared as an array of unsigned char*) is:

Code: Select all

videomemory[(row * rows + column) * 2 + 0] = character;
videomemory[(row * rows + column) * 2 + 1] = attribute;
3. Don't do a 'i < strlen(x)'. Instead check if the current character is a null terminator. Much faster.

Check the wiki for excellent tutorials that explain what I've mentioned above.
My string prints correctly if I remove a part of my code so I doubt thats the problem, also, I'm recieving a null pointer when passing a string as an argument. I don't see how that helps.

Re: Weird execution problem.

Posted: Fri Mar 11, 2011 6:10 am
by Combuster
It sounds like you have a much more fundamental problem. Some potential things:
- Do you overwrite bits of memory you should not?
- Do you load enough sectors in memory?
- Did you check everything ended up in the final binary at the right place? (disassemble!)

Also, learn to use bochs debugger, so you can see where the faulty condition actually starts.

Re: Weird execution problem.

Posted: Fri Mar 11, 2011 12:47 pm
by Davidm44
Combuster wrote:It sounds like you have a much more fundamental problem. Some potential things:
- Do you overwrite bits of memory you should not?
- Do you load enough sectors in memory?
- Did you check everything ended up in the final binary at the right place? (disassemble!)

Also, learn to use bochs debugger, so you can see where the faulty condition actually starts.
I do have bochs debugger setup already, the only thing that's different is the address for the string is 0, so its printing whatever's there.. >_>

I'll try the other fixes and post results.

Edit: Thank you so much Combuster, it fixed the problem regarding giving me a null pointer, but now there's something going on with MoveCursor, when I remove the call to it, everything works fine. If not, it doesn't print anything. Not sure whats going on. ;__;

Currently debugging but I can't find whats wrong.