Basic printf function
Basic printf function
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.
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
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.
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
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
You need a putch function...(that code works in PM, I alway program my OS stuff for PM)
Re:Basic printf function
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:
And here's my kernel.c:
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 */
}
Code: Select all
#include <printf.h>
int main(void)
{
printf("Hello");
return(0);
}
Re:Basic printf function
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)....
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
I added the changes and when I compile it with gcc, I get these errors:
Here's my kernel.c:
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.)
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
For no standard library use gcc with the switch, -nostdinc.
For no built in functions use gcc with the switch, -fno-builtin.
For no built in functions use gcc with the switch, -fno-builtin.
Code: Select all
gcc -c someSourceFile.c -nostdinc -fno-builtin
Code: Select all
Re:Basic printf function
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?
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;
}