C++ bug :(

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.
User avatar
matthias
Member
Member
Posts: 158
Joined: Fri Oct 22, 2004 11:00 pm
Location: Vlaardingen, Holland
Contact:

C++ bug :(

Post by matthias »

I'm developing my OS in C++, but know I've a serious problem :(.

I've set all gpp options the good way. Everything works well my main()
function is loaded and I can do function calls. But here lies the problem :(
I created a class with C++. It's a class that acts like a console. You can
write to the screen with it and do much more fun stuff.

My problem is that when my kernel does a function call to a member function of my console class, in this case: console::put(char c), but the parameter given by my main() function is not the same in put(), so when I trie to write a character to the screen nothing happens. Say I create a console object in my main() function, I initialize my console with con.init(), then I do con.put('c') but in the put() function the parameter isn't 'c' anymore :(. I know the code for writing characters to the screen is good because I tried it in C and it worked. Now, my question is:

Why doesn't my parameter (character) go to my put() function in a normal way, so that I can print it on the screen?
The source of my problems is in the source.
[AlAdDiN]
Member
Member
Posts: 107
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re: C++ bug :(

Post by [AlAdDiN] »

have u implemented correctly a malloc()/free()/realloc() functions ?
coze C++ use them to do the new/delete operators ...
so if there is no memory allocator implemented by ur OS ur class cannot be allocated in memory

remember that when developping an OS u must rewrite all needed libraries
-----------------------
There are 10 types of people in this world... those that understand binary, and those that don't.
rexlunae
Member
Member
Posts: 134
Joined: Sun Oct 24, 2004 11:00 pm
Location: North Dakota, where the buffalo roam

Re: C++ bug :(

Post by rexlunae »

matthias wrote: the source of my problems is in the source
Your signature says it all. It would be easier to help you with a little source.
bregma
Member
Member
Posts: 25
Joined: Tue Oct 26, 2004 11:00 pm
Location: the back woods
Contact:

Re: C++ bug :(

Post by bregma »

matthias wrote:... then I do con.put('c') but in the put() function the parameter isn't 'c' anymore.
Sounds like an integral promotion / sign extension problem. C++ is a bit different than C when it comes to these issues.

I would suggest
  1. post your code (the function and the calling code) and
  2. tell us the value you expect and what you actually get.
--
smw
Legend
Member
Member
Posts: 195
Joined: Tue Nov 02, 2004 12:00 am
Contact:

Re: C++ bug :(

Post by Legend »

[AlAdDiN] wrote:have u implemented correctly a malloc()/free()/realloc() functions ?
coze C++ use them to do the new/delete operators ...
so if there is no memory allocator implemented by ur OS ur class cannot be allocated in memory

remember that when developping an OS u must rewrite all needed libraries
You can place objects on the stack, in that case you don't need new/delete and then no malloc/free ...
*post*
User avatar
matthias
Member
Member
Posts: 158
Joined: Fri Oct 22, 2004 11:00 pm
Location: Vlaardingen, Holland
Contact:

Re: C++ bug :(

Post by matthias »

bregma wrote:
matthias wrote:... then I do con.put('c') but in the put() function the parameter isn't 'c' anymore.
Sounds like an integral promotion / sign extension problem. C++ is a bit different than C when it comes to these issues.

I would suggest
  1. post your code (the function and the calling code) and
  2. tell us the value you expect and what you actually get.
--
smw
OK, I'm going to do that, I will put it on osdev.org tomorrow :)
The source of my problems is in the source.
User avatar
matthias
Member
Member
Posts: 158
Joined: Fri Oct 22, 2004 11:00 pm
Location: Vlaardingen, Holland
Contact:

Re: C++ bug :(

Post by matthias »

Ok, here is my code, I hope you can help me.

:::: console.hpp ::::

Code: Select all


#ifndef CONSOLE_HPP_INCLUDED
#define CONSOLE_HPP_INCLUDED

#include <types.hpp>
#include <io.hpp>

#define COLUMNS		80
#define LINES		25
#define VIDEO		0xB8000
#define VIDPORT		0x3D4

class console
{
public:

	void init();

	void put(char c);
	void put(char c, int x, int y);

	void write(char* text);
	void write(char* text, int x, int y);

	void set_pos(int x, int y);
	void set_active(bool active);
	void set_ram_addr(uint32 addr);
	void set_attribute(char attrib);

	int get_xpos();
	int get_ypos();
	bool is_active();
	uint32 get_ram_addr();
	char get_attribute();

private:

	inline void setpos();
	void scroll();

	int cursor_xpos;
	int cursor_ypos;
	int pos;

	char* ram_addr;

	bool activated;
	char attribute;
};

#endif


:::: end console.hpp::::

:::: console.cpp ::::

Code: Select all


#include <console.hpp>

inline void console::setpos()
{
	// set place of cursor

    	pos = (cursor_xpos + COLUMNS * cursor_ypos) * 2;
    	outb_p(14, VIDPORT);
    	outb_p(0xff & (pos >> 9), VIDPORT + 1);
    	outb_p(15, VIDPORT);
    	outb_p(0xff & (pos >> 1), VIDPORT + 1);
}

void console::scroll(void)
{
    	int i;

    	for (i = 0; i < (LINES - 1) * COLUMNS * 2; i++)
	{
		*(ram_addr + i) = *(ram_addr + i + 2 * COLUMNS);
	}

    	for (i = 0; i < COLUMNS * 2; i += 2) 
	{
		*(ram_addr + (LINES - 1) * COLUMNS * 2 + i) = 32;
		*(ram_addr + (LINES - 1) * COLUMNS * 2 + i + 1) = attribute;
    	}
}

void console::init()
{
	// for now we set ram to 0xb8000

	ram_addr = (char*)0xb8000;

	cursor_xpos = 0;
	cursor_ypos = 0;
	attribute = 7; // default

	for(int i = 0; i < COLUMNS * LINES * 2; i += 2) 
	{
		*(ram_addr + i) = 0;
		*(ram_addr + i + 1) = 7;
    	}

	setpos();
}

void console::put(char c)
{
	*ram_addr = c;

	switch (c) 
	{
		case '\n':
		{
			cursor_xpos = 0;

			if (++cursor_ypos >= LINES) 
			{
				scroll();
				cursor_ypos--;
			}
			setpos();
		}
		break;

		case '\r':
		{
			cursor_xpos = 0;
		}
		break;

		default:
		{
			if(c >= 32 && c <= 126)
			{
				*(ram_addr + (cursor_xpos + cursor_ypos * COLUMNS) * 2) = c;
				*(ram_addr + (cursor_xpos + cursor_ypos * COLUMNS) * 2 + 1) = attribute;
			
				if (++cursor_xpos >= COLUMNS) 
				{
					cursor_xpos = 0;
					if (++cursor_ypos >= LINES) 
					{
						scroll();
						cursor_ypos--;
					}
				}
				setpos();
			}
		}
		break;
    }
}

void console::put(char c, int x, int y)
{
	if(c >= 32 && c <= 126)
	{
		*(ram_addr + (x + y * COLUMNS) * 2) = c;
		*(ram_addr + (x + y * COLUMNS) * 2 + 1) = attribute;
	}
}

void console::write(char* text)
{
	while(*text != '\0')
	{
		put(*text);
		text++;
	}
}

void console::write(char* text, int x, int y)
{
	int old_x = cursor_xpos;
	int old_y = cursor_ypos;

	cursor_xpos = x;
	cursor_ypos = y;

	write(text);

	cursor_xpos = old_x;
	cursor_ypos = old_y;

	setpos();
}

void console:: set_pos(int x, int y)
{
	cursor_xpos = x;
	cursor_ypos = y;
}

void console:: set_active(bool active)
{
	activated = active;
}

void console:: set_ram_addr(uint32 addr)
{
	ram_addr = (char*)addr;
}

void console::set_attribute(char attrib)
{
	attribute = attrib;
}

int console::get_xpos()
{
	return cursor_xpos;
}

int console::get_ypos()
{
	return cursor_ypos;
}

bool console::is_active()
{
	return activated;
}

uint32 console::get_ram_addr()
{
	return (uint32)ram_addr;
}

char console::get_attribute()
{
	return attribute;
}

:::: end console.cpp ::::

:::: main.cpp ::::

Code: Select all

#include <console.hpp>

int main()
{
	console c;
	c.init();
	c.put('c'); // print c on the screen

	for(;;); // hang :)
}
:::: end main.cpp ::::

I expect that 'c' will be printed to the screen, but this isn't the case. So I hope you can help me. :)[/code]
The source of my problems is in the source.
User avatar
matthias
Member
Member
Posts: 158
Joined: Fri Oct 22, 2004 11:00 pm
Location: Vlaardingen, Holland
Contact:

Re: C++ bug :(

Post by matthias »

anyone?
The source of my problems is in the source.
rexlunae
Member
Member
Posts: 134
Joined: Sun Oct 24, 2004 11:00 pm
Location: North Dakota, where the buffalo roam

Re: C++ bug :(

Post by rexlunae »

I'll look at his more later, (I'm not at my own computer), but you should check to make sure your video card is not in monochrome mode. I had a problem with that at one point.
User avatar
matthias
Member
Member
Posts: 158
Joined: Fri Oct 22, 2004 11:00 pm
Location: Vlaardingen, Holland
Contact:

Re: C++ bug :(

Post by matthias »

I'm sure it is not in monochrome mode. I tested this code with some tweaking in C and it worked.
The source of my problems is in the source.
Anton
Member
Member
Posts: 30
Joined: Thu Oct 21, 2004 11:00 pm
Location: Moscow, Russian Federation

Re: C++ bug :(

Post by Anton »

matthias wrote:anyone?
Could you please answer the following questions :

1)Under what environoment are you running the code?(protected mode, real mode)
2)You say that you tested the code-under what conditions(protected mode, real mode, ... language i guess is C)
3)What optimizations do you have turned on?(Try to turn them all off:) )
Anton.
[AlAdDiN]
Member
Member
Posts: 107
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re: C++ bug :(

Post by [AlAdDiN] »

void console::write(char* text, int x, int y)
{
int old_x = cursor_xpos;
int old_y = cursor_ypos;

cursor_xpos = x;
cursor_ypos = y;

write(text);

cursor_xpos = old_x;
cursor_ypos = old_y;

setpos();
}
hmmm .... i can't understand something in ur code .... where is setpos() function body ???

if it is in another package can u give us the source code ?
-----------------------
There are 10 types of people in this world... those that understand binary, and those that don't.
User avatar
matthias
Member
Member
Posts: 158
Joined: Fri Oct 22, 2004 11:00 pm
Location: Vlaardingen, Holland
Contact:

Re: C++ bug :(

Post by matthias »

hmmm .... i can't understand something in ur code .... where is setpos() function body ???

if it is in another package can u give us the source code ?
I've put all the code there, so I don't know what you mean.[/quote]
The source of my problems is in the source.
User avatar
matthias
Member
Member
Posts: 158
Joined: Fri Oct 22, 2004 11:00 pm
Location: Vlaardingen, Holland
Contact:

Re: C++ bug :(

Post by matthias »

Code: Select all

inline void console::setpos()
{
	// set place of cursor

    	pos = (cursor_xpos + COLUMNS * cursor_ypos) * 2;
    	outb_p(14, VIDPORT);
    	outb_p(0xff & (pos >> 9), VIDPORT + 1);
    	outb_p(15, VIDPORT);
    	outb_p(0xff & (pos >> 1), VIDPORT + 1);
}
It's there but I've put it here as well. :)[/code]
The source of my problems is in the source.
Anton
Member
Member
Posts: 30
Joined: Thu Oct 21, 2004 11:00 pm
Location: Moscow, Russian Federation

Re: C++ bug :(

Post by Anton »

[quote="matthias"][/quote]

I think i gave you the solution(or at least some hints), but i don't see any reply from you. :(
Last edited by Anton on Thu Dec 02, 2004 12:00 am, edited 2 times in total.
Post Reply