Using C code in segmented mode, question

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.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Using C code in segmented mode, question

Post 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?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Using C code in segmented mode, question

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Using C code in segmented mode, question

Post 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?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Using C code in segmented mode, question

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Using C code in segmented mode, question

Post 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)
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Using C code in segmented mode, question

Post 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.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Using C code in segmented mode, question

Post 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?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Using C code in segmented mode, question

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Using C code in segmented mode, question

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply