I need some help with text function

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
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

I need some help with text function

Post by Pyrofan1 »

I have these functions

Code: Select all

//NOTE: x and y are global variables
void Putch(unsigned char c, unsigned char forecolour, unsigned char backcolour)
{
     unsigned short attrib = ((backcolour << 4) | (forecolour & 0x0F)) << 8;
     unsigned short *where;
     where = 0xB8000 + (y * 80 + x);
     *where = c | (attrib << 8);
}

void Puts(char *string,unsigned char forecolor,unsigned char backcolor)
{
	int len=strlen(string);
	int count;

	for(count=0;count<len;count++)
	{
		Putch(*string,forecolor,backcolor);
		x++;
		string++;
	}
}

void moveCursor(int x1,int y1)
{
	x=x1;
	y=y1;
}
this is my code

Code: Select all

clear_screen(BSOD); //BSOD is a color
moveCursor(2,2);
Puts("I rule",1,15);

moveCursor(2,5);
Puts("Microsoft sucks!!!",1,15);
This code outputs
Irl
Mco tscs!
The letters are all in different colors, so these functions are obviously not working. How can I fix them.
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

Your using implicit cast of an integer into a short: 0xB8000 is not a pointer unless you cast it to one, so adding something to it won't get scaled like adding to a pointer would, as you are just doing normal integer arithmetics.

Easiest way to fix is to add an explicit cast. Another problem is that you are shifting "attrib" left by 8 bits twice, which I also corrected below.

Code: Select all

//NOTE: x and y are global variables
void Putch(unsigned char c, unsigned char forecolour, unsigned char backcolour)
{
     unsigned short attrib = ((backcolour << 4) | (forecolour & 0x0F)) << 8;
     unsigned short *where;
     where = ((unsigned short*)0xB8000) + (y * 80 + x);
     *where = c | attrib;
}
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
bsunisol
Member
Member
Posts: 40
Joined: Fri Apr 06, 2007 3:00 pm
Location: Germany, Berlin and near Hamburg

Post by bsunisol »

and you have to add 2 for the x-position.
remember that every character-byte is followed by a attribute-byte:

0x0B8000+0 = CHAR
0x0B8000+1 = ATTRIB
0x0B8000+2 = CHAR
0x0B8000+3 = ATTRIB
0x0B8000+4 = CHAR
0x0B8000+5 = ATTRIB
Post Reply