OSDev.org

The Place to Start for Operating System Developers
It is currently Sun May 05, 2024 8:21 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Garabage Characters From K_PrintF
PostPosted: Wed Jun 11, 2003 1:09 pm 
I am having problems with my k_printf function which is:

Code:
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:
        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?


Top
  
 
 Post subject: Re:Garabage Characters From K_PrintF
PostPosted: Wed Jun 11, 2003 1:31 pm 
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.


Top
  
 
 Post subject: Re:Garabage Characters From K_PrintF
PostPosted: Wed Jun 11, 2003 1:40 pm 
That just made it worse.

Code:
   for(;;)
   {
      i = getch() && "\0";
      k_printf(&i,3);
   };


It caused the WHOLE string to be garbage characters.


Top
  
 
 Post subject: Re:Garabage Characters From K_PrintF
PostPosted: Wed Jun 11, 2003 1:45 pm 
And before you correct my mistake,

Code:
   for(;;)
   {
      i = getch() && '\0';
      k_printf(&i,3);
   };


prints nothing at all.


Top
  
 
 Post subject: Re:Garabage Characters From K_PrintF
PostPosted: Wed Jun 11, 2003 2:29 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
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:
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:
char tmpstr[2]={0,0};
tmpstr[0]=getch();
printk(tmpstr);

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:Garabage Characters From K_PrintF
PostPosted: Wed Jun 11, 2003 3:42 pm 
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]


Top
  
 
 Post subject: Re:Garabage Characters From K_PrintF
PostPosted: Wed Jun 11, 2003 4:12 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
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 )

_________________
Image May the source be with you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Amazonbot [bot], Google [Bot] and 23 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group