OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 1:58 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 2:40 am 
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:
   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:
   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:
   variable++;
   variable += 1;
   variable = variable + 1;


Has anyone seen this before? or does anyone have any advice for me?

Thanks.


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 8:18 am 
Does your linker or compiler or GDT lock your variable?


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 8:36 am 
Hi,

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

Thanks.


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 8:48 am 
Is your char* in a readonly mem location in your selector?

Or you linker/compiler sets the code read only?


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 8:52 am 
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.


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 8:53 am 
Actually...I think it's your linker script or command line...

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


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 8:55 am 
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.


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 8:58 am 
Then how does it convert to binary?


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 9:01 am 
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.


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 9:03 am 
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 ???


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 9:06 am 
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.


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 9:07 am 
KJ's web site under tutorials under simple C kernel has one....


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 9:08 am 
OK, I will go check it out now.

thanks alot.


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 9:14 am 
Unfortunately nothing changed, this is very weird. ???


Top
  
 
 Post subject: Re:Strange Behaviour in C
PostPosted: Sun Nov 17, 2002 9:19 am 
Hmmm....
Try this

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


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 61 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group