Page 1 of 1

Garabage Characters From K_PrintF

Posted: Wed Jun 11, 2003 1:09 pm
by TheChuckster
I am having problems with my k_printf function which is:

Code: Select all

unsigned int k_printf(char *message, unsigned int line) // PrintF for green on black.
{
   char *vidmem = (char *) 0xb8000; // Address in memory where Video RAM begins.
   unsigned int i=0;

   i=(line*80*2); // 80 Characters in Current Line, 2 Bytes per Character

   while(*message!='\0') // Stop at null.
   {
      if(*message=='\n') // Check for a new line.
      {
         line++; // Increase current line.
         i=(line*80*2); // Reset i.
         *message++; // Walk through the message variable with pointer.
      } else {
         vidmem[i]=*message; // Adjust current address in video memory to store current character.
         *message++; // Walk through the message variable with pointer.
         i++; // Increase current byte by one.
         vidmem[i]=GREEN_TXT; // Adjust current address in video memory to store current hex color code.
         i++; // Increase current byte by one.
      };
   };

   return(1); // Executed successfully.
};
Here is my the part of k_main that calls it:

Code: Select all

        unsigned char i;
   for(;;)
   {
      i = getch();
      k_printf(&i,3);
   };
It is polling and receiving input from the keyboard.

When I run this, what I type is displayed perfectly, but after it are three garbage characters. What is the cause of this?

Re:Garabage Characters From K_PrintF

Posted: Wed Jun 11, 2003 1:31 pm
by Tim
You haven't nul-terminated the string you're giving to k_printf. The garbage characters are whatever happens to be in memory following the variable i; it only stops printing because there happens to be a zero byte in memory afterwards.

Re:Garabage Characters From K_PrintF

Posted: Wed Jun 11, 2003 1:40 pm
by TheChuckster
That just made it worse.

Code: Select all

   for(;;)
   {
      i = getch() && "\0";
      k_printf(&i,3);
   };
It caused the WHOLE string to be garbage characters.

Re:Garabage Characters From K_PrintF

Posted: Wed Jun 11, 2003 1:45 pm
by TheChuckster
And before you correct my mistake,

Code: Select all

   for(;;)
   {
      i = getch() && '\0';
      k_printf(&i,3);
   };
prints nothing at all.

Re:Garabage Characters From K_PrintF

Posted: Wed Jun 11, 2003 2:29 pm
by Pype.Clicker
just a piece of advice: try to get a tutorial on C strings.

getch() return a *single* character (at least in standard environment. Don't reuse standard name if you don't follow standard or noone will be able to help you).

a single character is a small integer (0x41 is for 'A', 0x20 for space, etc). A string is a pointer to an array of character, terminated with a null character '\0'.

so

Code: Select all

char this_is_a_string[]={'H','e','l','l','o',0};
char *this_is_also_a_string="Hello";
are two way to declare the string "Hello" in C.

as "&&" is the logical AND operator, getch && '\0' is an always false expression (as '\0' is 0 thus false, and "anything AND false == false")

in no way getch && '\0' is a concatenation. there's no concat operator in C.

getch() && "\0" is "getch() didn't returned a nul character and "\0" is different from the NULL pointer" .which is ... at least hard to guess... but shouldn't compile without a bunch of warnings ...

what you want to do is :

Code: Select all

char tmpstr[2]={0,0};
tmpstr[0]=getch();
printk(tmpstr);

Re:Garabage Characters From K_PrintF

Posted: Wed Jun 11, 2003 3:42 pm
by TheChuckster
You're right. I should really learn C before I try to finish my kernel. I only know high level RAD crap that has spoiled me into learning bad habits like Visual Basic. :( I mastered the language though ;D -- I can write ANYTHING possible in it. [/BSing]

Re:Garabage Characters From K_PrintF

Posted: Wed Jun 11, 2003 4:12 pm
by Pype.Clicker
hehe. i was a master at QuickBAsic ages ago ... turning to C was hard because i had sooo much pre-built ideas about what should be done. If you can find it in a library, i recommend you the reading of Kernighan & Ritchie "C ansi, 2nd edition". Very well written, with a complete syntax of the language, the standard library, and so on. Practice a bit on idioms like "if (x==y) x=z", ok?"okay":"failed, for (c=ptr;*c;c++), etc. before you really start programming, and use -Wall at compile-time (will make the compiler warn you for a lot of newbie-errors).
All the compiler's error messages are nicely described in info gcc (use the buddah finger accurately on "/" to search a string in the doc, and you'll find you will win, whatever the error could try to do :p )