Page 1 of 2
Strange Behaviour in C
Posted: Sun Nov 17, 2002 2:40 am
by PlayOS
Hi,
I am at quite a loss as to why C is doing this, it is very strange. I first noticed the problem when I attempted the Kernel in C++ tutorial posted here. For some reason the variables will not increment.
It sounds weird but it is happening, look at this code for example:
Code: Select all
char* video = (char*)0xb8000;
*video = 'A';
video++;
*video = 0x1e;
this code should write a yellow on blue 'A' at 0,0 on the screen, however it comes up with a weird triangle character, this character is produced by the 0x1e, so instead of the video variable being incremented, it is just overwriting the first byte of video memory. If I write the same code like this:
Code: Select all
char* video = (char*)0xb8000;
video[0] = 'A';
video[1] = 0x1e;
then everything goes according to plan. This is really puzzling because I have never seen this before.
I cannot increment variables in any way, none of the following ways work:
Code: Select all
variable++;
variable += 1;
variable = variable + 1;
Has anyone seen this before? or does anyone have any advice for me?
Thanks.
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 8:18 am
by Tom
Does your linker or compiler or GDT lock your variable?
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 8:36 am
by PlayOS
Hi,
I am not exactly sure what you mean, could you elaborate on that a bit.
Thanks.
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 8:48 am
by Tom
Is your char* in a readonly mem location in your selector?
Or you linker/compiler sets the code read only?
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 8:52 am
by PlayOS
My data segment is a ring 0, 4gb segment, that is writable, so it could be the compiler, do you know a switch to turn it off?
Might have to google I think.
thanks.
Edit : btw, I use DJGPP tools.
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 8:53 am
by Tom
Actually...I think it's your linker script or command line...
Could I see that?(all your command lines/scripts)
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 8:55 am
by PlayOS
I have never got around to learning/using linker scripts and I just invoke gcc like this for all source files
gcc -c source.c -o source.o
This is all I do, I dont have any other problems, so far that is.
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 8:58 am
by Tom
Then how does it convert to binary?
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 9:01 am
by PlayOS
OK, Sorry, brain warp for a second there.
The whole process is like this
gcc -c src.c -o src.o
ld -Ttext=0x30000 src.o -o kernel.exe
objcopy -O binary kernel.exe kernel.bin
and thats it.
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 9:03 am
by Tom
Mabe you should make a linker script that makes data r/w or, your GDT's codesel is read only and the char* is in the codesel for some reason ???
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 9:06 am
by PlayOS
I am willing to try, do you know of a good place where I can learn about linker scripts. Maybe you could also post a simple one so I can test this.
thanks.
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 9:07 am
by Tom
KJ's web site under tutorials under simple C kernel has one....
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 9:08 am
by PlayOS
OK, I will go check it out now.
thanks alot.
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 9:14 am
by PlayOS
Unfortunately nothing changed, this is very weird. ???
Re:Strange Behaviour in C
Posted: Sun Nov 17, 2002 9:19 am
by Tom
Hmmm....
Try this
char curchar; curchar = 'A'; *video = curchar;