Segment descriptor and endianness

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
HyperAssembler
Member
Member
Posts: 36
Joined: Thu Sep 04, 2014 5:24 pm
Location: !SIGSEGV

Segment descriptor and endianness

Post by HyperAssembler »

Hi guys!
I'm trying to write functions that set up gdt in c.
My definition of segment descriptor is:

Code: Select all

typedef struct
{
    uint16 limit_low;
    uint16 base_low;
    uint8 base_middle;
    uint8 access;
    uint8 limit_mid_flag;
    uint8 base_high;
} gdt_descriptor;
My problem is about the first uint16 field.
I know 80386 is little-endian. So when writing a 16-bit value to mem(let's say 0x1234), the actual data in ram would be

Code: Select all

Addr Data
0x0 0x34
0x1 0x12
Suppose I have a limit of 0x123456, I assign 0x1234 to limit_low and if you look at the binary of bits 0-15 of segment descriptor, it should be

Code: Select all

Bit   Data
0-7   0x34 
8-15  0x12.
But intel seems to expect this:

Code: Select all

Bit   Data
0-7   0x12 
8-15  0x34.
I've seen fair amount of tutorials. Some of them do 0x12 0x34 and the others do 0x34 0x12.
So I'm confused here:
Should it be 0x12 0x34 or 0x34 0x12(little-endian form)?
Thanks!
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Segment descriptor and endianness

Post by Brendan »

Hi,
HyperAssembler wrote:Should it be 0x12 0x34 or 0x34 0x12(little-endian form)?
Um, what?

The lowest 16-bits of 0x123456 would be 0x3456 (not 0x1234); which would be stored in little-endian as the bytes 0x56, 0x34.


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