printf, etc()

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Whatever5k

printf, etc()

Post by Whatever5k »

I need basic functions like printf(), etc.
Where can I get public (GNU?) printf-functions. Be aware of that I would pe pleased to have printf()-functions that I can easily implement in my kernel. I had several printf()-functions which had problems with some headers, etc.

regards!
K.J.

Re: printf, etc()

Post by K.J. »

Goto:
http://www.execpc.com/~geezer/osd/libc/index.htm

and scroll down to Problematic functions in the C library. There are three files there. Get them all and you have a printf function.

K.J.
Whatever5k

Re: printf, etc()

Post by Whatever5k »

There's a problem. stdarg.h includes the header file _va_list.h which I do not have.
And this is not a printf() function but a do_printf (different syntax)
Tim

Re: printf, etc()

Post by Tim »

There's a problem. stdarg.h includes the header file _va_list.h which I do not have.
Your compiler should provide the <stdarg.h> header file somewhere which provides definitions of va_list, va_begin, va_arg and va_end. Copy that file into the directory where you keep your own library header files.
And this is not a printf() function but a do_printf (different syntax)
It shouldn't be too hard to work out how to make a printf() function which wraps do_printf(). (Hint: write vprintf() first, then write printf() in terms of vprintf().)
Whatever5k

Re: printf, etc()

Post by Whatever5k »

Allright, I used the libc stdarg.h and now the do_printf() function seems to work... .
I now will have to get the printf() function working... .
Whatever5k

Re: printf, etc()

Post by Whatever5k »

Jippie, I nearly have my printf() function... .
I'll just have to do some tricky stuff, that's all... .
I took the screen.c by df, thanks to him!
ratgod

Re: printf, etc()

Post by ratgod »

I'm new to OSDEV and C but im picking it up as I go along, I'm using John Fine's Bootloader and as a kernel im using the one the guy wrote in this topic > http://www.mega-tokyo.com/cgi-bin/yabb/ ... 1012029411 <

it works ok, then I put the do_prinf files in with it, at first it complained about there being files missing, since i dunno how to specify my own include directory I changed all the #include <~~~> to #include "~~~" and have the files it needs in the same directory as the kernel source.
 whenever I use the function do_printf with do_printf("This is a test"); it tells me i dont have enougth arguments and refuses to compile, when I call the function printf with the same arguments the linker bombs out with this:
[glow=red,2,300]test.o(.text+0x14a):test.c: undefined reference to `_printf'[/glow]

heres my compiling procedure:
[glow=red,2,300]gcc -c test.c -o test.o
nasm -f coff test.asm -o test1.o
ld --oformat binary -e 0xFF800000 test1.o test.o
[/glow]

note: my kernel is called test.c at the moment and the test.asm  just seems to call the main routine in the C file.

I'm using: Nasm, DJGPP on a AMD-486-133 running Win98
Tim

Re: printf, etc()

Post by Tim »

This linker message implies that you haven't got a printf function. do_printf is the core of printf, vprintf, sprintf, vsprintf, fprintf and vfprintf; each of the printf's are just wrappers around it. You have to write each of these yourself (starting with printf).
K.J.

Re: printf, etc()

Post by K.J. »

Here's my printf function(you have to supply what line # to start displaying the text on):

unsigned int k_printf(char *message, unsigned int line)
{
     char *vidmem = (char *) 0xb8000;
     unsigned int i=0;

     i=(line*80*2);

     while(*message!=0) // 24h
     {
           if(*message==0x2F)
           {
                 *message++;
                 if(*message==0x6e)
                 {
                       line++;
                       i=(line*80*2);
                       *message++;
                       if(*message==0)
                       {
                             return(1);
                       };
                 };
           };
           vidmem=*message;
           *message++;
           i++;
           vidmem=0x7;
           i++;
     };

     return(1);
};


Then call it like this:
k_printf("Hello World!", 0); // first line

It can handle the "/n" for a newline but can't print out a variable like a real printf function.

Whatever5k: if you have complete a printf function, could please email it to me([email protected])?

K.J.
ratgod

Re: printf, etc()

Post by ratgod »

Well, i looked in the doprintf.c and found this procedure/function:
[glow=red,2,300]int printf(const char *fmt, ...)
{
     va_list args;
     int ret_val;

     va_start(args, fmt);
     ret_val = vprintf(fmt, args);
     va_end(args);
     return ret_val;
}[/glow]
thats what i was trying to call.
eitherway k_printf didnt seem to work for me, i used it within the main C file and as an external included file.
I can write to the screen though using:
[glow=red,2,300]  char *vidmem = (char *) 0xb8000;
 vidmem[0]='T';
[/glow]

Like I said before, I'm only learning so any help would be apreciated.
Tim

Re: printf, etc()

Post by Tim »

So what's the problem then? You've got do_printf and printf... Are you aware that you need to put doprintf.o on the linker command line (which means compiling doprintf.c)?

IIRC Chris Giese's do_printf function requires another function (which you write) to actually put the text on the screen. That function is one of the parameters to do_printf. It should be obvious from looking at the do_printf source.
ratgod

Re: printf, etc()

Post by ratgod »

firstly the k_printf function works now, I used a linker script and now it works.
secondly, I compiled doprintf.c and got a warning about memcpy confliction, but i got the doprintf.o from it anyway :)
I now try and link it with my programs using the following line:
[glow=red,2,300]ld -T kernel.lk --oformat binary -e 0xFF800000 kernel1.o kernel.o inc\doprintf.o[/glow]
and i get this as a reply:
[glow=green,2,300]kernel.o(.text+0x1c8):kernel.c: undefined reference to `_printf'
inc\doprintf.o(.text+0x3ee):doprintf.c: undefined reference to `_strlen'
[/glow]

note: doprintf.o and all source and headers for it are in the inc directory of my kernels directory. and im using [glow=red,2,300]#include "inc\_printf.h"[/glow] to call it.

Also, I would apreciate it if someone could tell me how to specify an include directory so i didnt have to change #include <~~~> to #include "~~~" all the time.

Thanks in advance.
Tim

Re: printf, etc()

Post by Tim »

I now try and link it with my programs using the following line:
[glow=red,2,300]ld -T kernel.lk --oformat binary -e 0xFF800000 kernel1.o kernel.o inc\doprintf.o[/glow]
and i get this as a reply:
[glow=green,2,300]kernel.o(.text+0x1c8):kernel.c: undefined reference to `_printf'
inc\doprintf.o(.text+0x3ee):doprintf.c: undefined reference to `_strlen'
[/glow]
Do you actually have a printf function in any of your source files? doprintf.c doesn't include it; you have to write your own. You'll also need to write the strlen function.

To specify an include directory with gcc, use the -I<dir> option.
ratgod

Re: printf, etc()

Post by ratgod »

I got my doprintf from here
http://www.execpc.com/~geezer/osd/code/ ... doprintf.c which is on this page : http://www.execpc.com/~geezer/osd/snippets.htm
within doprintf.c there seems to be a function:
[glow=red,2,300]int printf(const char *fmt, ...)[/glow]
which is near the bottom on the file.

Any ideas?
Tim

Re: printf, etc()

Post by Tim »

Read the source code. The bottom 7 functions are enclosed within a #if 0/#endif block, so they will be ignored by the compiler. Remove the #if 0/#endif (and the main() function) and all will be well.

Note the comment above vprintf_help:

Code: Select all

/*****************************************************************************
PRINTF
You must write your own putchar()
*****************************************************************************/
int vprintf_help(unsigned c, void **ptr)
{
      putchar(c);
      return 0 ;
}
Post Reply