I need to write a printk function that prints not only array of chars but numbers
(e.g.printk("the number is %d",number);)
for my kernel
but i don't know it would be helpfull if you can show me some code or info on how to do it(but not linux).
printk function
Re:printk function
Do you want to write this function in asm oder in c/c++? If you want to write it in c/c++ it's very easy. the previous poster is right: You first have to write a function that converts a integer in a string. I suggest a function like this:
void int2str(int number, char *strbuffer);
In this function you first have to decide if the number is positive or negative. If it's negative your first char in strbuffer is a '-'.
The biggest number a unsigned int can be is 4294967295 (2^32 - 1). So you have to calculate what number % 1000000000 makes (9 zeros). This can be 0, 1, 2, 3 or 4. Add to this '0' (The ascii-code of '0' is not 0) and place this char in strbuffer. Now you calculate (number - (number % 1000000000)). This makes the number one digit smaller. For example if your number is 4123456789 you subtract 4*1000000000. This makes 123456789. Now you can go on and test the first number again: number % 100000000. (8 zeros). And so you go on until you have all digits.
I think this description is a bit confusing. I'm sorry. Here's a example code (it's not very good programming style, but it works):
With this function you can convert numbers in strings. Now you can write your printk.
void printk(char* string, long number);
It checks every char in the string it was called with. If it finds a '%' it has to check if the next char is a 'd'. If it had found a "%d" it has to convert the second parameter in a string (using a temp_buffer) and then can print the temp_buffer. Now it has to go on printing the string.
This printk function can for example be called with:
printk("Teh value of TestVar is %d", TestVar);
This methode is not very fast and nasty coded but if you only want the kernel to print a error message like the following it is useful:
printk("Exception Nr. %d occured! System halted!",ExceptionNumber);
I hope this helps you although my English is not very good and the int2str-code is a fast hack of me.
void int2str(int number, char *strbuffer);
In this function you first have to decide if the number is positive or negative. If it's negative your first char in strbuffer is a '-'.
The biggest number a unsigned int can be is 4294967295 (2^32 - 1). So you have to calculate what number % 1000000000 makes (9 zeros). This can be 0, 1, 2, 3 or 4. Add to this '0' (The ascii-code of '0' is not 0) and place this char in strbuffer. Now you calculate (number - (number % 1000000000)). This makes the number one digit smaller. For example if your number is 4123456789 you subtract 4*1000000000. This makes 123456789. Now you can go on and test the first number again: number % 100000000. (8 zeros). And so you go on until you have all digits.
I think this description is a bit confusing. I'm sorry. Here's a example code (it's not very good programming style, but it works):
Code: Select all
void int2str(long num,char *string)
{
long tmp = num;
long tmp2 = 0;
int first_zeros = 1;
int i = 0;
int pos = 0;
int mod_op = 1000000000;
if(tmp < 0)
{
string[pos] = '-';
pos++;
}
for(i = 0;i<10;i++)
{
tmp2 = tmp % mod_op;
tmp = (tmp - tmp2) / mod_op;
if((tmp != 0)||(first_zeros == 0))
{
first_zeros = 0;
string[pos] = ((char)tmp ) + '0';
pos++;
}
tmp = tmp2;
mod_op = mod_op / 10;
}
if(first_zeros == 1)
{
string[pos] = '0';
pos++;
}
string[pos] = 0;
}
void printk(char* string, long number);
It checks every char in the string it was called with. If it finds a '%' it has to check if the next char is a 'd'. If it had found a "%d" it has to convert the second parameter in a string (using a temp_buffer) and then can print the temp_buffer. Now it has to go on printing the string.
This printk function can for example be called with:
printk("Teh value of TestVar is %d", TestVar);
This methode is not very fast and nasty coded but if you only want the kernel to print a error message like the following it is useful:
printk("Exception Nr. %d occured! System halted!",ExceptionNumber);
I hope this helps you although my English is not very good and the int2str-code is a fast hack of me.
Re:printk function
There is a good printf in FritzOS at my site..in the fstdio.h file..it prints hex, ints, etc just like the one in the stdio.h in DOS and windows.
http://sourceforge.net/projects/fritzos
http://sourceforge.net/projects/fritzos
Re:printk function
one of the first things that i belive i will do after i get a basic working idt with IRQ's and the such is replicated the system calls made by the clib. I cant imagen myslef sitting for weeks if not months rewriting to whole damn standard libary. I'd rather be writing the sytem calls instead.