Basic printf function

Programming, for all ages and all languages.
Post Reply
beyondsociety

Basic printf function

Post 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.
jrfritz

Re:Basic printf function

Post 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.
beyondsociety

Re:Basic printf function

Post 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?
jrfritz

Re:Basic printf function

Post by jrfritz »

You need a putch function...(that code works in PM, I alway program my OS stuff for PM)
beyondsociety

Re:Basic printf function

Post 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);
}
jrfritz

Re:Basic printf function

Post 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)....
beyondsociety

Re:Basic printf function

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

Re:Basic printf function

Post by jrfritz »

You need to link without libarys...
beyondsociety

Re:Basic printf function

Post by beyondsociety »

How do I do that?
whyme_t

Re:Basic printf function

Post 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
beyondsociety

Re:Basic printf function

Post 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;
}
Post Reply