OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 9:27 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Basic printf function
PostPosted: Thu Jan 16, 2003 4:02 pm 
I would like to know what the necessary steps are in order to make a basic printf function for my C kernel?

I just want a basic step by step of what to do and not just code. I don't mind code as long as you also explain the steps.


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Thu Jan 16, 2003 5:00 pm 
the first steps to learn a simple one is:

make a:

void printf(char* msg)

and then you make a temporary char ( non pointer ) that holds the value of the currently parsed character, like this:

char curchar;

and make it = the current character in the msg. like this:

curchar = *msg;

now, you have a loop that keeps putch'ing the curchar while it does not = '\0' ( with quotes ) and then to move curchar to the next character, do this:

++msg;
curchar = *msg; // update curchar = next character in msg.

That should be it for a VERY simple printf to learn with.

Once you have that down...i'll help you with the % arguments.


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Thu Jan 16, 2003 5:47 pm 
Will this run on a C kernel that I can test from my pmode bootsector or do I have to add some code to this basic print function to access the video memory?


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Thu Jan 16, 2003 6:39 pm 
You need a putch function...(that code works in PM, I alway program my OS stuff for PM)


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Thu Jan 16, 2003 7:30 pm 
I tried what you said and I ran it from my bootsector. For some reason it rebooted my computer.

What could I be doing wrong?

Here's my printf.h function:
Code:
/* Basic Printf function for IBOX */

void printf(char* msg)
{
   char curchar;
   cutchar=*msg;

   ++msg;
   curchar=*msg;     /* Update curchar = next character in msg */
}


And here's my kernel.c:
Code:
#include <printf.h>

int main(void)
{
    printf("Hello");
    return(0);
}


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Thu Jan 16, 2003 8:58 pm 
Whoa WAY off target there!

Look:

void printf(char* msg)
{
char curchar;
curchar = *msg; // make the current char = the
// currently processing character

while ( curchar != '\0' )
// keep looping until the end of the message
{
purch(curchar);

++msg;
curchar = *msg;
}
}

That should work...(code from memory)....


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Thu Jan 16, 2003 9:41 pm 
I added the changes and when I compile it with gcc, I get these errors:
Code:
kernel.c:12: warning: conflicting types for built-in function 'printf'
kernel.c: In function 'printf'
kernel.c:14: 'curchar' undeclared (first use in this function)
kernel.c:14: (Each undeclared identifier is reported only once)
kernel.c:14: for each function it appears in.)


Here's my kernel.c:
Code:
main(void)
{
    printf("Hello");
    return(0);
}

/* ----------------------------------------------------------------- */
/* Basic Printf function for IBOX */

void printf(char* msg)
{
   char curchar;
   cutchar=*msg;   /* Make the current char = the currently processing character

   while(curchar !='\0')
   
   /* Keep looping until the end of the message */
{

   puch(curchar);
   
   ++msg;
   curchar=*msg;   /* Update curchar = next character in msg */
}
}


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Thu Jan 16, 2003 10:49 pm 
You need to link without libarys...


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Thu Jan 16, 2003 10:51 pm 
How do I do that?


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Fri Jan 17, 2003 3:21 am 
For no standard library use gcc with the switch, -nostdinc.

For no built in functions use gcc with the switch, -fno-builtin.

Code:
gcc -c someSourceFile.c -nostdinc -fno-builtin
Code:


Top
  
 
 Post subject: Re:Basic printf function
PostPosted: Fri Jan 17, 2003 11:45 pm 
I changed a few things.

I'm trying to get my C kernel to work with my pmode bootsector. It works fine if I load another asm file from my bootsector, but doesn't work with my kernel.

What could I be doing wrong?

Code:
/** C Functions **/
void print(char*);

k_main(void)
{
   char *test_ptr;
   test_ptr = "IBOX";

   print(test_ptr);
}

void print(char* string)
{
   unsigned char* vidmem = (unsigned char*)0xb8000;
   unsigned long index = 0;

   while(string[index])
        {
                vidmem[2*index] = string[index];
                vidmem[(2*index)+1] = 0x07;
                index++;
        }
   
        return;
}


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 102 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group