Page 1 of 1

gcc pointer problem in osdev

Posted: Tue Nov 10, 2009 10:31 pm
by wangfei
Hi, I have a strange question....

In a function:

Code: Select all

void fun(int a){
    int *p = &a;
    putd(*p);
}
Variable a is one argument of the function, so when I let pointer p = address of a, gcc generate an instruction:

Code: Select all

lea edi, ss:[xx+xx]
Next line, I want to find a with p, gcc generate another instruction:

Code: Select all

mov eax, ds:[edi+x]
When I get the address, LEA instruction put the effective address of a into edi, segment register is SS; but when I want the value, MOV instruction use a segment register DS........ :(

So if DS != SS, the answer is wrong.... now I have to set DS = SS, this confuse me very much. :(

Does gcc generate a wrong code ?
Is that some gcc argument or other ways can solve this problem?

Re: gcc pointer problem in osdev

Posted: Tue Nov 10, 2009 10:39 pm
by thepowersgang
GCC assumes that it is working in 32-bit protected mode, with a flat memory model.
This means that SS == DS in it's "mind" so that code is perfectly correct, and on all but the most esoteric systems will work perfectly.

Re: gcc pointer problem in osdev

Posted: Tue Nov 10, 2009 11:53 pm
by Solar
The problem here is that the language C does not provide any mechanisms to adress a segmented architecture. Compiler builders could go two ways about it: Extend the language in a non-standard, non-portable manner to include segments, or work on the assumption of a "flat", unsegmented memory model.

Most compilers chose the latter.

Re: gcc pointer problem in osdev

Posted: Wed Nov 11, 2009 11:09 pm
by wangfei
oh, I see.....

So I must set DD=SS, and the limit of SS determined start address of stack....

Thanks you two!!

Re: gcc pointer problem in osdev

Posted: Thu Nov 12, 2009 4:47 pm
by Owen
In fact, GCC requires DS == ES == SS; and that the bases of CS and DS match

Re: gcc pointer problem in osdev

Posted: Thu Nov 12, 2009 6:19 pm
by iocoder
also, you can use gcc in the case of (SS != DS), but you will put a lot of assembly code between C lines and this would be confusing.