Page 1 of 1

Basic printf function

Posted: Thu Jan 16, 2003 4:02 pm
by beyondsociety
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.

Re:Basic printf function

Posted: Thu Jan 16, 2003 5:00 pm
by jrfritz
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.

Re:Basic printf function

Posted: Thu Jan 16, 2003 5:47 pm
by beyondsociety
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?

Re:Basic printf function

Posted: Thu Jan 16, 2003 6:39 pm
by jrfritz
You need a putch function...(that code works in PM, I alway program my OS stuff for PM)

Re:Basic printf function

Posted: Thu Jan 16, 2003 7:30 pm
by beyondsociety
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: Select all

/* 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: Select all

#include <printf.h>

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

Re:Basic printf function

Posted: Thu Jan 16, 2003 8:58 pm
by jrfritz
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)....

Re:Basic printf function

Posted: Thu Jan 16, 2003 9:41 pm
by beyondsociety
I added the changes and when I compile it with gcc, I get these errors:

Code: Select all

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: Select all

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 */
 }
}

Re:Basic printf function

Posted: Thu Jan 16, 2003 10:49 pm
by jrfritz
You need to link without libarys...

Re:Basic printf function

Posted: Thu Jan 16, 2003 10:51 pm
by beyondsociety
How do I do that?

Re:Basic printf function

Posted: Fri Jan 17, 2003 3:21 am
by whyme_t
For no standard library use gcc with the switch, -nostdinc.

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

Code: Select all

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

Re:Basic printf function

Posted: Fri Jan 17, 2003 11:45 pm
by beyondsociety
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: Select all

/** 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;
}