OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 3:21 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: GCC wrong .data addresses
PostPosted: Thu Mar 22, 2018 5:18 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
AlexKuz wrote:
This code doesn't work:
Code:
unsigned short* vmem = (unsigned short*)0xb8000;

void lmain()
{
   for (unsigned int i = 0; i < 80 * 24; i++)
      vmem[i] = 0;

   while (1);
}

But this code works perfectly:
Code:
unsigned short* vmem;

void lmain()
{
   vmem = (unsigned short*)0xb8000;

   for (unsigned int i = 0; i < 80 * 24; i++)
      vmem[i] = 0;

   while (1);
}

I disagree with the term "it works perfectly". The first set, specifically:
Code:
unsigned short* vmem = (unsigned short*)0xb8000;

Places the value of 0xB8000 at offset [vmem] within your binary file.
Then when you do:
Code:
   vmem[i] = 0;

The processor does:
Code:
   mov  DS:[vmem+i*2], 0

However, DS may not be where you think it is.

Now, the reason why you think
Code:
   vmem = (unsigned short*)0xb8000;

works perfectly is because the code is actually using a word sized memory position, somewhere in DS:, probably not where you think vmem is, even though the code looks correct.

Without looking any further, I would almost guaranty that you do not have your segments/selectors set up correctly. i.e.: DS doesn't pointer where you think it does at the start of your C code.

Compile it as is, but place
Code:
  _asm xchg bx,bx  // or what ever your compiler needs

at the beginning. Then load it into the Bochs Debugger and hit 'c'ontinue. The code will run until it hits that instruction. Then you can see what the DS, ES, CS, etc. segment/selectors are.

If you don't use Bochs, make your emulator of choice crash or have an infinite loop, then stop the emulator. The emulator should print out the segment register values at end of emulation.

Anyway, some how find out what these values are and you will find out they are not what you think they should be.

Ben


Top
 Profile  
 
 Post subject: Re: GCC wrong .data addresses
PostPosted: Fri Mar 23, 2018 8:36 am 
Offline

Joined: Thu Mar 22, 2018 7:16 am
Posts: 8
I set the DS register to 0x10 which is data segment in my GDT.
Maybe it is the problem in my GDT?
I've just debugged my code and all segment registers were 0x10(except cs, it was 0x08).


Top
 Profile  
 
 Post subject: Re: GCC wrong .data addresses
PostPosted: Fri Mar 23, 2018 1:28 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
AlexKuz wrote:
I set the DS register to 0x10 which is data segment in my GDT.
Maybe it is the problem in my GDT?
I've just debugged my code and all segment registers were 0x10(except cs, it was 0x08).

Those are just selectors, to select an entry in the GDT. You need to check your GDT *and* check the "origin" or starting offset that GCC is placing on your binary file. I use PE files so that the starting address is really easy to find.

Ben


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: FrankRay78, SemrushBot [Bot] and 160 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