gcc pointer problem in osdev

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
wangfei
Posts: 2
Joined: Tue Nov 10, 2009 10:11 pm

gcc pointer problem in osdev

Post 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?
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: gcc pointer problem in osdev

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: gcc pointer problem in osdev

Post 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.
Every good solution is obvious once you've found it.
wangfei
Posts: 2
Joined: Tue Nov 10, 2009 10:11 pm

Re: gcc pointer problem in osdev

Post by wangfei »

oh, I see.....

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

Thanks you two!!
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: gcc pointer problem in osdev

Post by Owen »

In fact, GCC requires DS == ES == SS; and that the bases of CS and DS match
User avatar
iocoder
Member
Member
Posts: 208
Joined: Sun Oct 18, 2009 5:47 pm
Libera.chat IRC: iocoder
Location: Alexandria, Egypt | Ottawa, Canada
Contact:

Re: gcc pointer problem in osdev

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