Page 2 of 2

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 3:21 am
by mangaluve
Okay thanks! But I wondered, too, if I use the address segment:offset in ASM, that address consists of two parts. But when I use a pointer in C, for instance
unsigned char *ptr = (unsigned char *)0x80004, how will that be interpreted by the computer? Because the computer wants addresses of the form A:B, but it just gets a "number". What happens? Which physical address will be targeted?

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 3:45 am
by Brendan
Hi,
mangaluve wrote:Okay thanks! But I wondered, too, if I use the address segment:offset in ASM, that address consists of two parts. But when I use a pointer in C, for instance
unsigned char *ptr = (unsigned char *)0x80004, how will that be interpreted by the computer? Because the computer wants addresses of the form A:B, but it just gets a "number". What happens?
In assembly, if you do something like "mov eax,[0x12345678]" then the CPU assumes you're using the DS segment. In a similar way, if you do "mov eax,[ebp+0x12345678]" then the CPU assumes you're using the SS segment.

The CPU will normally assume you want to use the DS segment, unless the EBP or ESP register is part of the address (in this case it'll assume you want to use the SS segment), and unless it's a string instruction ("rep movsd", "rep insd", etc) where the CPU will assume the source address is in DS:RSI/ESI/SI and the destination address is in ES:RDX/EDX/DX. An assembly language programmer can explicitly override the default segment with segment override prefixes, except for ES:RDX/EDX/DX. For example, "gs: rep movsd" will tell the CPU to use the GS segment register instead of the DS segment register.

The compiler just lets the CPU assume everything, and doesn't use any segment override prefixes for any instructions it generates. This means that SS, DS and ES must refer to the same area of memory.

There are some instructions where you must provide explicit segments - for example "call far 0x1234:0x56789ABC". Compilers aren't capable of generating these instructions. Basically, the compiler ignores segmentation completely.


Cheers,

Brendan

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 3:58 am
by mangaluve
Thanks, I got it much better now :)

But what if I write
mov eax, [08:123456]
Won't any register (DS and so on) be used then, so this overrides it completly? I don't really get what these registers are for, are they just kind of pointers to "default-segments"? And in ASM, by using A:B, I can override that?

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 4:55 am
by Brendan
Hi,
mangaluve wrote:But what if I write
mov eax, [08:123456]
That's an invalid instruction. You'd have to do something like:

Code: Select all

    mov ax,08
    mov ds,ax
    mov eax, [123456]
Or:

Code: Select all

    mov ax,08
    mov fs,ax
    mov eax, [fs:123456]

Cheers,

Brendan

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 5:33 am
by mangaluve
Thanks.. but the point is, if I do that, I can choose the segment myself? But in C I cannot do that, so the default segment will be used (depending on the instruction)

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 5:35 am
by JamesM
mangaluve wrote:Thanks.. but the point is, if I do that, I can choose the segment myself? But in C I cannot do that, so the default segment will be used (depending on the instruction)
Correct.

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 5:43 am
by mangaluve
Thanks! So paging seems better to use. But I would like to set that up in C. Then I must first use segmented memory, jump to C, and switch to paging. Or how should I do it?

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 6:15 am
by JamesM
mangaluve wrote:Thanks! So paging seems better to use. But I would like to set that up in C. Then I must first use segmented memory, jump to C, and switch to paging. Or how should I do it?
Read the wiki.

Re: Using C code in segmented mode, question

Posted: Tue Feb 24, 2009 7:35 am
by Combuster
Higher Half With GDT

Also, What's not been mentioned is that you can use segmentation with modern compilers. The limitation is that DS=ES=SS, yet what they are exactly is not defined.

Which explains why above GDT trick works, and how you can make use of the phenomenon "small address spaces", where paging and segmentation are used together to limit the accessible range from 4GB to something small, so that you can switch tasks without having the need of flushing the entire TLB on every switch.