print 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.
pini

print function

Post by pini »

I wrote a C 'putchar' function and used it to write a 'print' function.
The first one works correctly, but the second one is making me crazy :

I think that the problem is in the call : print("My string");

On a P166 and on a 386, it works very well
On a PIII667 and on a P90, it prints nothing.

After hex-editing my elf-compiled file, I found out that the strings that are supposed to be print out are stored sequentially in the file. This means that there is a \0 character (marking the end of a string) between each pair of strings.

In my print function, I am using this kind of code :

while(*string!=0)
{
putchar(*string);
string++;
}

where string is a pointer to an unsigned char.

May the problem come from the fact that the call is using a false address (I mean giving the address of the character before (ie : \0) the first one in my string) ?

Can anyone give me a piece of help ?
Whatever5k

Re:print function

Post by Whatever5k »

Please show us the whole code of print() and putchar().
And try this here:

Code: Select all

while(*string != '\0')
...
pini

Re:print function

Post by pini »

Here you are :

[pre]void putchar(unsigned char chr)
{
unsigned char x;
unsigned char y;
unsigned char*dest;
unsigned short position;

x=ttytable[currenttty].column;
y=ttytable[currenttty].row;
switch(chr)
{
case 0xA:
x=0;
y++;
if(y>24)
   {
    scrollup(1);
    y=24;
   }
break;
case 0x8:
if(x>0)
   x--;
else
   {
    if(y>0)
    {
    x=79;
    y--;
    }
    else
    break;
   }
dest=(unsigned char*)(VIDRAM+160*y+2*x);
position=80*y+x;
ttytable[currenttty].screen[position].ascii=0x0;
ttytable[currenttty].screen[position].attribute=ttytable[currenttty].attribute;
*dest=ttytable[currenttty].screen[position].ascii;
*(dest+1)=ttytable[currenttty].screen[position].attribute;
break;
default:
dest=(unsigned char*)(VIDRAM+160*y+2*x);
position=80*y+x;
ttytable[currenttty].screen[position].ascii=chr;
ttytable[currenttty].screen[position].attribute=ttytable[currenttty].attribute;
*dest=ttytable[currenttty].screen[position].ascii;
*(dest+1)=ttytable[currenttty].screen[position].attribute;
x++;
break;
}
if(x>79)
{
x=0;
y++;
if(y>24)
   {
    scrollup(1);
    y=24;
   }
}
gotoxy(x,y);
return;
}


void print(unsigned char*string)
{
while(*string!=0)
{
putchar(*string);
string++;
}
return;
}[/pre]
pini

Re:print function

Post by pini »

It didn't change anything to replace 0 by '\0'

I have found that replacing
[pre]print("hello");[/pre]
by
[pre]print(str);[/pre]
where str is definded as follow :
[pre]unsigned char str[6]={'H','e','l','l','o',0};[/pre]
makes my print function work correctly on my four test machines.
jrfritz

Re:print function

Post by jrfritz »

Not a good idea to make you printf like that.

Instead...check if you like my code and if you do, use that. it's in fstdio.h in the include directory.

download it at:

http://sourceforge.net/projects/fritzos
pini

Re:print function

Post by pini »

Ok, thanks for your help.
But I still can't understand why it works on two computer and why not on two others... It's a little "brainstorming"...
pini

Re:print function

Post by pini »

Hum...
Here are the results of my last test :
I replaced
[pre]
while(*string!='\0')
{
putchar(*string);
string++;
}
[/pre]
by
[pre]
for(i=0;i<=5;i++)
putchar(string[]);
putchar('\n');
[/pre]

The result of this is that only the new line appears on the screen.
Does this mean that there is a problem in the address of string? Because there may be a '\0' just before my string in the compiled file, but my string is not entirely composed of null characters...
Unexpected

Re:print function

Post by Unexpected »

I change my Print function, and now it doesn't work well... When I Print a text like txt[]="Simple" I got a colored and mixed text.
I don't know whats wrong?
Here is code:

Code: Select all

char* video = (char*)0xB8000;
static long curX, curY;
static unsigned char textattrib=0x07;

void PutCh(char ch)
{
     unsigned int i = 0;

     i = (curY*80*2) + curX;

   if (ch != 0)
   {
      if (ch == '\n')
      {
         curY++;
         curX = 0;
      }
      else
      {
         video[i] = ch;
         i++;
         video[i] = textattrib;
         curX++;
      }
      if (curY > 25) curY = 0;
          Update_Cursor();
   }
}

void Print(char *msg)
{
   while (*msg != "\0")
   {
          PutCh(*msg);
          *msg++;
   }
}
jrfritz

Re:print function

Post by jrfritz »

I'm looking...but FritzOS has a good well tested Printf.

I'll post if I find a prob.
jrfritz

Re:print function

Post by jrfritz »

Could I see your update_cursor function?
Unexpected

Re:print function

Post by Unexpected »

Here:

Code: Select all

void GotoXY(int x, int y)
{
   unsigned short position = (y*80+x);

   outportb(0x3D4, 0x0F);
   outportb(0x3D5, (unsigned char)(position&0xFF));

   outportb(0x3D4, 0x0E);
   outportb(0x3D5, (unsigned char)((position>>8)&0xFF));
   curX = x;
   curY = y;
}

void Update_Cursor()
{
   GotoXY(curX, curY);
}
I dont understand.. how text became a colored?
This is very simple:
video = ch;
i++;
video = textattrib;
curX++;
So whats wrong?
Unexpected

Re:print function

Post by Unexpected »

P.S. - There is one thing... For example if I want to print a text "Hello", my functions prints colored "Hlo". Thats means - first symbol is printed correctly, but second is used as textattrib :/ ???
jrfritz

Re:print function

Post by jrfritz »

Hmmm...I can't find anything.

Must be some sortof hard to fine typo... :(

try this:

Code: Select all

unsigned int k_printf(char *message, unsigned int line) // the message and then the line #
{
        char *vidmem = (char *) 0xb8000;
        unsigned int i=0;

        i=(line*80*2);

        while(*message!=0)
        {
                if(*message==0x2F) // check for the next line stuff
                {
                        *message++;
                        if(*message==0x6e)
                        {
                                line++;
                                i=(line*80*2);
                                *message++;
                                if(*message==0)
                                {
                                        return(1);
                                };
                        };
                        *message--; // got to go back, this is just a forward slash
                };
                vidmem[i]=*message;
                *message++;
                i++;
                vidmem[i]=WHITE_TXT;
                i++;
        };

        return(1);
};
jrfritz

Re:print function

Post by jrfritz »

Worked?
Unexpected

Re:print function

Post by Unexpected »

No... :-\
i = (curY*80*2) + curX;
vidmem = 'A';
i++;
vidmem = 0x07;
curX++;
And I have 'A'(writed 2 times in same place) and background color - blue, text - red...
Can you write a simple WORKING putch() function plz?
Post Reply