Does GCC assume that ss , ds and es point to the same seg ?

Programming, for all ages and all languages.
Post Reply
liubo
Posts: 11
Joined: Mon Jul 14, 2008 8:55 pm

Does GCC assume that ss , ds and es point to the same seg ?

Post by liubo »

Hi,
For example --------------------------------------------------------------------------------
//#include <stdio.h>
int gVar = 0;

void test(int * iPoint)
{
(*iPoint) = (*iPoint) + 1;
}

int main()
{
int sVar = 1;

test(&gVar); --> pass the data which is in the data segment
test(&sVar); --> pass the data which is in the stack segment

//printf("gVar = %d, sVar = %d\n", gVar, sVar);
return 0;
}
----------------------------------------------------------------------------------------------
Disassemble the test()
0000003D 55 push ebp
0000003E 89E5 mov ebp,esp
00000040 8B4508 mov eax,[ebp+0x8]
00000043 8B00 mov eax,[eax] --> ALWAYS use ds segment to get the value. This means if the data is in the stack segment and ss is different from ds. The data will NOT be processed properly.
00000045 8D5001 lea edx,[eax+0x1]
00000048 8B4508 mov eax,[ebp+0x8]
0000004B 8910 mov [eax],edx
0000004D 5D pop ebp
0000004E C3 ret
--------------------------------------------------------------------------------------------------

My question:
Does GCC always assume that ss and ds point to the same segment ?
If not, which GCC option can distinguished the above 2 situations ?
If so, does it mean that we must set the same value in ss and ds when we use C language?

Thanks a lot.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Does GCC assume that ss , ds and es point to the same seg ?

Post by AJ »

Hi,

Yes, GCC assumes a flat memory model - segment registers should all be the same. This is the main hurdle that stops GCC being used for a real-mode OS.

Cheers,
Adam
liubo
Posts: 11
Joined: Mon Jul 14, 2008 8:55 pm

Re: Does GCC assume that ss , ds and es point to the same seg ?

Post by liubo »

AJ wrote:Hi,

Yes, GCC assumes a flat memory model - segment registers should all be the same. This is the main hurdle that stops GCC being used for a real-mode OS.

Cheers,
Adam

Thank you Adam. You are too fast. :)
Post Reply