He there,
I'm writing a pm os. I'm busy implementing functions linke printf. The problem is I detected a strange 'bug'? When I use the following code:
void main()
{
...
printk("Hello world!");
...
}
void printk(char *msg)
{
while (*msg!=0)
{
putc(*msg);
}
}
The string: ello world! is printed. And when I change printk to:
void printk(char *msg)
{
msg--;
....
The string Hello world! is printed.... I'm using gcc. Does gcc give the wrong pointers? Or does my data gets translated 1 byte, or am I missing something?
Thanks in advance,
Lont
String pointer error
RE:String pointer error
First, I assume that in copying the code in here, you forgot to add the line that increments msg, since it doesn't go into an infinite loop printing the same character over and over.
Second, I have never had this problem. I just checked my source, and I have no such hack, nor do I see why it should be needed. There is obviously something going on that's not visible in the code snippet you posted. It's probably something simple: you are incrementing before printing, for example.
Something else to try:
Put a pointer to the string in a global variable, something like this:
const char *str = "Hello World!";
int main()
{
printk(str);
}
It should produce the same results. If not, then something very strange is going on. You can verify that it actually starts processing at the right memory address.
Gnome.
Second, I have never had this problem. I just checked my source, and I have no such hack, nor do I see why it should be needed. There is obviously something going on that's not visible in the code snippet you posted. It's probably something simple: you are incrementing before printing, for example.
Something else to try:
Put a pointer to the string in a global variable, something like this:
const char *str = "Hello World!";
int main()
{
printk(str);
}
It should produce the same results. If not, then something very strange is going on. You can verify that it actually starts processing at the right memory address.
Gnome.
RE:String pointer error
printk, as written in your post, is an infinite loop (*msg is never incremented anywhere). Could you post your actual printk?
RE:String pointer error
He everyone,
I know I made a typo. And thanx for your help, but I found the problem. Well, I found the solution to it. In my linker script I told the linker where the data segment would be loaded into memory depending on the size of the code segment. But somewhere the code was aligned (or I miscalculated). And the data start got translated 1 byte.
Now I removed the line where I specified the start of the data and let the Linker decide that. Now everything seems to work fine.
There is just a small problem, that my stack segment has to start at adres 0x00000000. I think it is an other linker thingy, or I made an error somewhere in my GDT entry. Anyway I will fix it when I heave the time...
After all my problems let me tell you, LD is a very versatile linker with many options and many ways in which mistakes can be made. But I love it.
Cheers,
Lont
I know I made a typo. And thanx for your help, but I found the problem. Well, I found the solution to it. In my linker script I told the linker where the data segment would be loaded into memory depending on the size of the code segment. But somewhere the code was aligned (or I miscalculated). And the data start got translated 1 byte.
Now I removed the line where I specified the start of the data and let the Linker decide that. Now everything seems to work fine.
There is just a small problem, that my stack segment has to start at adres 0x00000000. I think it is an other linker thingy, or I made an error somewhere in my GDT entry. Anyway I will fix it when I heave the time...
After all my problems let me tell you, LD is a very versatile linker with many options and many ways in which mistakes can be made. But I love it.
Cheers,
Lont