Strange Behaviour in C

Programming, for all ages and all languages.
PlayOS

Strange Behaviour in C

Post 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.
Tom

Re:Strange Behaviour in C

Post by Tom »

Does your linker or compiler or GDT lock your variable?
PlayOS

Re:Strange Behaviour in C

Post by PlayOS »

Hi,

I am not exactly sure what you mean, could you elaborate on that a bit.

Thanks.
Tom

Re:Strange Behaviour in C

Post by Tom »

Is your char* in a readonly mem location in your selector?

Or you linker/compiler sets the code read only?
PlayOS

Re:Strange Behaviour in C

Post 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.
Tom

Re:Strange Behaviour in C

Post by Tom »

Actually...I think it's your linker script or command line...

Could I see that?(all your command lines/scripts)
PlayOS

Re:Strange Behaviour in C

Post 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.
Tom

Re:Strange Behaviour in C

Post by Tom »

Then how does it convert to binary?
PlayOS

Re:Strange Behaviour in C

Post 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.
Tom

Re:Strange Behaviour in C

Post 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 ???
PlayOS

Re:Strange Behaviour in C

Post 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.
Tom

Re:Strange Behaviour in C

Post by Tom »

KJ's web site under tutorials under simple C kernel has one....
PlayOS

Re:Strange Behaviour in C

Post by PlayOS »

OK, I will go check it out now.

thanks alot.
PlayOS

Re:Strange Behaviour in C

Post by PlayOS »

Unfortunately nothing changed, this is very weird. ???
Tom

Re:Strange Behaviour in C

Post by Tom »

Hmmm....
Try this

char curchar; curchar = 'A'; *video = curchar;
Post Reply